From 20c00acf5ad7fcbc64f9795f6b281ea3c82e5ac1 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Mon, 1 Apr 2024 19:03:50 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20mariadb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG-BETA.md | 3 + PLANS.md | 1 + modules/server/pom.xml | 6 + .../dromara/jpom/JpomServerApplication.java | 2 + .../org/dromara/jpom/system/db/InitDb.java | 3 + .../main/resources/application-mariadb.yml | 21 ++ .../src/main/resources/application-mysql.yml | 2 +- .../main/resources/application-postgresql.yml | 2 +- .../server/src/main/resources/application.yml | 6 +- .../resources/config_default/application.yml | 2 +- modules/storage-module/pom.xml | 1 + .../java/org/dromara/jpom/db/DbExtConfig.java | 6 +- .../org/dromara/jpom/dialect/DialectUtil.java | 1 + .../storage-module-mariadb/README.md | 3 + .../storage-module-mariadb/pom.xml | 41 ++++ .../storage/MariadbStorageServiceImpl.java | 95 +++++++++ .../jpom/storage/MariadbTableBuilderImpl.java | 200 ++++++++++++++++++ .../org.dromara.jpom.db.IStorageService | 1 + ....dromara.jpom.db.IStorageSqlBuilderService | 1 + .../sql-view/execute.mariadb.v1.0.sql | 88 ++++++++ .../resources/sql-view/execute.mysql.v1.0.sql | 0 .../storage-module-postgresql/README.md | 7 + .../sql-view/execute.postgresql.v1.0.sql | 0 23 files changed, 486 insertions(+), 6 deletions(-) create mode 100644 modules/server/src/main/resources/application-mariadb.yml create mode 100644 modules/storage-module/storage-module-mariadb/README.md create mode 100644 modules/storage-module/storage-module-mariadb/pom.xml create mode 100644 modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbStorageServiceImpl.java create mode 100644 modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbTableBuilderImpl.java create mode 100644 modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageService create mode 100644 modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageSqlBuilderService create mode 100644 modules/storage-module/storage-module-mariadb/src/main/resources/sql-view/execute.mariadb.v1.0.sql rename modules/{server => storage-module/storage-module-mysql}/src/main/resources/sql-view/execute.mysql.v1.0.sql (100%) create mode 100644 modules/storage-module/storage-module-postgresql/README.md rename modules/{server => storage-module/storage-module-postgresql}/src/main/resources/sql-view/execute.postgresql.v1.0.sql (100%) diff --git a/CHANGELOG-BETA.md b/CHANGELOG-BETA.md index 797e81004..82f6e5439 100644 --- a/CHANGELOG-BETA.md +++ b/CHANGELOG-BETA.md @@ -2,6 +2,9 @@ ## 2.11.3.1 +### 🐣 新增功能 + +1. 【server】新增 数据库支持 *mariadb* ### 🐞 解决BUG、优化功能 diff --git a/PLANS.md b/PLANS.md index bf61e5dd6..4b481fd1c 100644 --- a/PLANS.md +++ b/PLANS.md @@ -19,6 +19,7 @@ 15. 隧道节点 16. docker-compose sh 17. 监控通知模块优化支持更多(飞书) zx +18. 数据库支持 mariadb ## 2.10.x diff --git a/modules/server/pom.xml b/modules/server/pom.xml index e843aa2fc..040f1affa 100644 --- a/modules/server/pom.xml +++ b/modules/server/pom.xml @@ -128,6 +128,12 @@ ${project.version} + + org.dromara.jpom.storage-module + storage-module-mariadb + ${project.version} + + me.zhyd.oauth JustAuth diff --git a/modules/server/src/main/java/org/dromara/jpom/JpomServerApplication.java b/modules/server/src/main/java/org/dromara/jpom/JpomServerApplication.java index 697b4fb27..9e2090386 100644 --- a/modules/server/src/main/java/org/dromara/jpom/JpomServerApplication.java +++ b/modules/server/src/main/java/org/dromara/jpom/JpomServerApplication.java @@ -61,6 +61,8 @@ public class JpomServerApplication { * --h2-migrate-mysql --h2-user=jpom --h2-pass=jpom 将 h2 数据库迁移到 mysql *

* --h2-migrate-postgresql --h2-user=jpom --h2-pass=jpom 将 h2 数据库迁移到 postgresql + *

+ * --h2-migrate-mariadb --h2-user=jpom --h2-pass=jpom 将 h2 数据库迁移到 mariadb * * @param args 参数 * @throws Exception 异常 diff --git a/modules/server/src/main/java/org/dromara/jpom/system/db/InitDb.java b/modules/server/src/main/java/org/dromara/jpom/system/db/InitDb.java index 84dcb0a77..6f7411f26 100644 --- a/modules/server/src/main/java/org/dromara/jpom/system/db/InitDb.java +++ b/modules/server/src/main/java/org/dromara/jpom/system/db/InitDb.java @@ -306,6 +306,9 @@ public class InitDb implements DisposableBean, ILoadEvent { Opt.ofNullable(environment.getProperty("h2-migrate-postgresql")).ifPresent(s -> { migrateOpr.accept(DbExtConfig.Mode.POSTGRESQL); }); + Opt.ofNullable(environment.getProperty("h2-migrate-mariadb")).ifPresent(s -> { + migrateOpr.accept(DbExtConfig.Mode.MARIADB); + }); } private void importH2Sql(Environment environment, String importH2Sql) { diff --git a/modules/server/src/main/resources/application-mariadb.yml b/modules/server/src/main/resources/application-mariadb.yml new file mode 100644 index 000000000..cffae7223 --- /dev/null +++ b/modules/server/src/main/resources/application-mariadb.yml @@ -0,0 +1,21 @@ +# +# Copyright (c) 2019 Of Him Code Technology Studio +# Jpom is licensed under Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +# See the Mulan PSL v2 for more details. +# + +jpom: + db: + # 数据库默认 支持 :H2、MYSQL、MARIADB、POSTGRESQL + mode: MARIADB + url: jdbc:mysql://127.0.0.1:3309/jpom + # 数据库账号 默认 jpom + #user-name: jpom + user-name: root + # 数据库密码 默认 jpom 如果自行配置请保证密码强度 + # user-pwd: jpom + user-pwd: jpom123456 diff --git a/modules/server/src/main/resources/application-mysql.yml b/modules/server/src/main/resources/application-mysql.yml index f0f1b6b6e..39f629f21 100644 --- a/modules/server/src/main/resources/application-mysql.yml +++ b/modules/server/src/main/resources/application-mysql.yml @@ -10,7 +10,7 @@ jpom: db: - # 数据库默认 支持 :H2、MYSQL + # 数据库默认 支持 :H2、MYSQL、MARIADB、POSTGRESQL mode: MYSQL url: jdbc:mysql://127.0.0.1:3306/jpom # 数据库账号 默认 jpom diff --git a/modules/server/src/main/resources/application-postgresql.yml b/modules/server/src/main/resources/application-postgresql.yml index b0a30bd88..9e58a66a0 100644 --- a/modules/server/src/main/resources/application-postgresql.yml +++ b/modules/server/src/main/resources/application-postgresql.yml @@ -10,7 +10,7 @@ jpom: db: - # 数据库默认 支持 :H2、MYSQL + # 数据库默认 支持 :H2、MYSQL、MARIADB、POSTGRESQL mode: POSTGRESQL url: jdbc:postgresql://localhost:5432/jpom # 数据库账号 默认 jpom diff --git a/modules/server/src/main/resources/application.yml b/modules/server/src/main/resources/application.yml index eb96a8f7e..fabc742b1 100644 --- a/modules/server/src/main/resources/application.yml +++ b/modules/server/src/main/resources/application.yml @@ -72,7 +72,7 @@ jpom: # 查看日志时初始读取最后多少行(默认10,0不读取) init-read-line: 10 db: - # 数据库默认 支持 :H2、MYSQL + # 数据库默认 支持 :H2、MYSQL、MARIADB、POSTGRESQL mode: H2 # 日志存储条数,将自动清理旧数据,配置小于等于零则不清理 log-storage-count: 10000 @@ -153,7 +153,9 @@ server: spring: profiles: - active: mysql-1 +# active: mysql-1 + active: mariadb + web: resources: static-locations: classpath:/dist/ diff --git a/modules/server/src/main/resources/config_default/application.yml b/modules/server/src/main/resources/config_default/application.yml index 871c1facd..9fac5b93e 100644 --- a/modules/server/src/main/resources/config_default/application.yml +++ b/modules/server/src/main/resources/config_default/application.yml @@ -72,7 +72,7 @@ jpom: # 查看日志时初始读取最后多少行(默认10,0不读取) init-read-line: 10 db: - # 数据库默认 支持 :H2、MYSQL + # 数据库默认 支持 :H2、MYSQL、MARIADB、POSTGRESQL mode: H2 # 日志存储条数,将自动清理旧数据,配置小于等于零则不清理 log-storage-count: 10000 diff --git a/modules/storage-module/pom.xml b/modules/storage-module/pom.xml index 81ae33933..5d342577e 100644 --- a/modules/storage-module/pom.xml +++ b/modules/storage-module/pom.xml @@ -25,6 +25,7 @@ storage-module-h2 storage-module-mysql storage-module-postgresql + storage-module-mariadb 4.0.0 2.11.3.0 diff --git a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/DbExtConfig.java b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/DbExtConfig.java index e92619056..f5f3ab19d 100644 --- a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/DbExtConfig.java +++ b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/db/DbExtConfig.java @@ -147,6 +147,10 @@ public class DbExtConfig implements InitializingBean { /** * postgresql */ - POSTGRESQL + POSTGRESQL, + /** + * Mariadb + */ + MARIADB } } diff --git a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/dialect/DialectUtil.java b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/dialect/DialectUtil.java index d1fbdab7f..188ef0b26 100644 --- a/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/dialect/DialectUtil.java +++ b/modules/storage-module/storage-module-common/src/main/java/org/dromara/jpom/dialect/DialectUtil.java @@ -86,6 +86,7 @@ public class DialectUtil { case H2: return getH2Dialect(); case MYSQL: + case MARIADB: return getMySqlDialect(); case POSTGRESQL: return getPostgresqlDialect(); diff --git a/modules/storage-module/storage-module-mariadb/README.md b/modules/storage-module/storage-module-mariadb/README.md new file mode 100644 index 000000000..0afcb27d1 --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/README.md @@ -0,0 +1,3 @@ +```shell +docker run -d --name jpom-mariadb -e MYSQL_ROOT_PASSWORD=jpom123456 -p 3309:3306 mariadb +``` \ No newline at end of file diff --git a/modules/storage-module/storage-module-mariadb/pom.xml b/modules/storage-module/storage-module-mariadb/pom.xml new file mode 100644 index 000000000..1eff5152f --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.dromara.jpom.storage-module + jpom-storage-module-parent + 2.11.3.0 + ../pom.xml + + + storage-module-mariadb + + + 8 + 8 + UTF-8 + + + + + org.dromara.jpom.storage-module + storage-module-common + ${project.version} + + + + org.mariadb.jdbc + mariadb-java-client + + + + org.dromara.jpom + common + provided + ${project.version} + + + + diff --git a/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbStorageServiceImpl.java b/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbStorageServiceImpl.java new file mode 100644 index 000000000..bb6683dec --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbStorageServiceImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2019 Of Him Code Technology Studio + * Jpom is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +package org.dromara.jpom.storage; + +import cn.hutool.core.lang.Opt; +import cn.hutool.db.ds.DSFactory; +import cn.hutool.setting.Setting; +import lombok.extern.slf4j.Slf4j; +import org.dromara.jpom.db.DbExtConfig; +import org.dromara.jpom.db.IStorageService; +import org.dromara.jpom.system.JpomRuntimeException; +import org.springframework.util.Assert; + +/** + * @author bwcx_jzy + * @since 2024/4/1 + */ +@Slf4j +public class MariadbStorageServiceImpl implements IStorageService { + + private String dbUrl; + private DSFactory dsFactory; + + @Override + public String dbUrl() { + Assert.hasText(this.dbUrl, "还没有初始化数据库"); + return dbUrl; + } + + @Override + public int getFetchSize() { + return Integer.MIN_VALUE; + } + + @Override + public DbExtConfig.Mode mode() { + return DbExtConfig.Mode.MARIADB; + } + + @Override + public DSFactory init(DbExtConfig dbExtConfig) { + Assert.isNull(this.dsFactory, "不要重复初始化数据库"); + Assert.hasText(dbExtConfig.getUrl(), "没有配置数据库连接"); + Setting setting = dbExtConfig.toSetting(); + this.dsFactory = DSFactory.create(setting); + this.dbUrl = dbExtConfig.getUrl(); + return this.dsFactory; + } + + @Override + public DSFactory create(DbExtConfig dbExtConfig, String url, String user, String pass) { + Setting setting = this.createSetting(dbExtConfig, url, user, pass); + return DSFactory.create(setting); + } + + @Override + public Setting createSetting(DbExtConfig dbExtConfig, String url, String user, String pass) { + String url2 = Opt.ofBlankAble(url).orElse(dbExtConfig.getUrl()); + String user2 = Opt.ofBlankAble(user).orElse(dbExtConfig.getUserName()); + String pass2 = Opt.ofBlankAble(pass).orElse(dbExtConfig.getUserPwd()); + Setting setting = dbExtConfig.toSetting(); + setting.set("user", user2); + setting.set("pass", pass2); + setting.set("url", url2); + return setting; + } + + public DSFactory getDsFactory() { + Assert.notNull(this.dsFactory, "还没有初始化数据库"); + return dsFactory; + } + + + @Override + public JpomRuntimeException warpException(Exception e) { + return new JpomRuntimeException("数据库异常", e); + } + + + @Override + public void close() throws Exception { + log.info("mysql db destroy"); + if (this.dsFactory != null) { + dsFactory.destroy(); + this.dsFactory = null; + } + } +} diff --git a/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbTableBuilderImpl.java b/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbTableBuilderImpl.java new file mode 100644 index 000000000..8729fd8e8 --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/src/main/java/org/dromara/jpom/storage/MariadbTableBuilderImpl.java @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2019 Of Him Code Technology Studio + * Jpom is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +package org.dromara.jpom.storage; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import org.dromara.jpom.db.*; +import org.springframework.util.Assert; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author bwcx_jzy + * @since 2024/4/1 + */ +public class MariadbTableBuilderImpl implements IStorageSqlBuilderService { + + @Override + public DbExtConfig.Mode mode() { + return DbExtConfig.Mode.MARIADB; + } + + @Override + public String generateIndexSql(List row) { + StringBuilder stringBuilder = new StringBuilder(); + for (TableViewIndexData viewIndexData : row) { + String indexType = viewIndexData.getIndexType(); + switch (indexType) { + case "ADD-UNIQUE": { + // ALTER TABLE `jpom`.`PROJECT_INFO` + //DROP INDEX `workspaceId`, + //ADD UNIQUE INDEX `workspaceId`(`workspaceId` ASC, `strike` ASC, `modifyUser`) USING BTREE; + String field = viewIndexData.getField(); + List fields = StrUtil.splitTrim(field, "+"); + Assert.notEmpty(fields, "索引未配置字段"); + stringBuilder.append("call drop_index_if_exists('").append(viewIndexData.getTableName()).append("','").append(viewIndexData.getName()).append("')").append(";").append(StrUtil.LF); + stringBuilder.append(this.delimiter()).append(StrUtil.LF); + stringBuilder.append("ALTER TABLE ").append(viewIndexData.getTableName()).append(" ADD UNIQUE INDEX ").append(viewIndexData.getName()).append(" (").append(CollUtil.join(fields, StrUtil.COMMA)).append(")"); + break; + } + case "ADD": { + // ALTER TABLE `jpom`.`PROJECT_INFO` + //DROP INDEX `workspaceId`, + //ADD UNIQUE INDEX `workspaceId`(`workspaceId` ASC, `strike` ASC, `modifyUser`) USING BTREE; + String field = viewIndexData.getField(); + List fields = StrUtil.splitTrim(field, "+"); + Assert.notEmpty(fields, "索引未配置字段"); + stringBuilder.append("call drop_index_if_exists('").append(viewIndexData.getTableName()).append("','").append(viewIndexData.getName()).append("')").append(";").append(StrUtil.LF); + stringBuilder.append(this.delimiter()).append(StrUtil.LF); + stringBuilder.append("ALTER TABLE ").append(viewIndexData.getTableName()).append(" ADD INDEX ").append(viewIndexData.getName()).append(" (").append(CollUtil.join(fields, StrUtil.COMMA)).append(")"); + break; + } + default: + throw new IllegalArgumentException("不支持的类型:" + indexType); + } + stringBuilder.append(";").append(StrUtil.LF); + stringBuilder.append(this.delimiter()).append(StrUtil.LF); + + } + return stringBuilder.toString(); + } + + @Override + public String generateAlterTableSql(List row) { + StringBuilder stringBuilder = new StringBuilder(); + for (TableViewAlterData viewAlterData : row) { + String alterType = viewAlterData.getAlterType(); + switch (alterType) { + case "DROP": + // ALTER TABLE NODE_INFO DROP COLUMN IF EXISTS `cycle`; + stringBuilder.append("CALL drop_column_if_exists('").append(viewAlterData.getTableName()).append("', '").append(viewAlterData.getName()).append("')"); + break; + case "ADD": + // ALTER TABLE PROJECT_INFO ADD IF NOT EXISTS triggerToken VARCHAR (100) comment '触发器token'; + String columnSql = this.generateColumnSql(viewAlterData, true); + stringBuilder.append("CALL add_column_if_not_exists('").append(viewAlterData.getTableName()).append("','").append(viewAlterData.getName()).append("','").append(columnSql).append("')"); + break; + case "ALTER": + // alter table table1 modify column column1 decimal(10,1) DEFAULT NULL COMMENT '注释'; + stringBuilder.append("ALTER TABLE ").append(viewAlterData.getTableName()).append(" modify column "); + stringBuilder.append(this.generateColumnSql(viewAlterData)); + break; + case "DROP-TABLE": + stringBuilder.append("drop table if exists ").append(viewAlterData.getTableName()); + break; + default: + throw new IllegalArgumentException("不支持的类型:" + alterType); + } + stringBuilder.append(";").append(StrUtil.LF); + stringBuilder.append(this.delimiter()).append(StrUtil.LF); + + } + return stringBuilder.toString(); + } + + /** + * CREATE TABLE IF NOT EXISTS USEROPERATELOGV1 + * ( + * id VARCHAR(50) not null comment 'id', + * reqId VARCHAR(50) COMMENT '请求ID', + * CONSTRAINT USEROPERATELOGV1_PK PRIMARY KEY (id) + * ); + * COMMENT ON TABLE USEROPERATELOGV1 is '操作日志'; + * + * @param name 表名 + * @param desc 描述 + * @param row 字段信息 + * @return sql + */ + @Override + public String generateTableSql(String name, String desc, List row) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("CREATE TABLE IF NOT EXISTS ").append(name).append(StrUtil.LF); + stringBuilder.append("(").append(StrUtil.LF); + for (TableViewData tableViewData : row) { + stringBuilder.append(StrUtil.TAB).append(this.generateColumnSql(tableViewData)).append(StrUtil.COMMA).append(StrUtil.LF); + } + // 主键 + List primaryKeys = row.stream() + .filter(tableViewData -> tableViewData.getPrimaryKey() != null && tableViewData.getPrimaryKey()) + .map(TableViewRowData::getName) + .collect(Collectors.toList()); + Assert.notEmpty(primaryKeys, "表没有主键"); + stringBuilder.append(StrUtil.TAB).append("PRIMARY KEY (").append(CollUtil.join(primaryKeys, StrUtil.COMMA)).append(")").append(StrUtil.LF); + stringBuilder.append(") ").append("COMMENT=").append("'").append(desc).append("';"); + return stringBuilder.toString(); + } + + @Override + public String generateColumnSql(TableViewRowData tableViewRowData) { + return generateColumnSql(tableViewRowData, false); + } + + private String generateColumnSql(TableViewRowData tableViewRowData, boolean encode) { + // id VARCHAR(50) not null default '' comment 'id' + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("`").append(tableViewRowData.getName()).append("`").append(StrUtil.SPACE); + String type = tableViewRowData.getType(); + Assert.hasText(type, "未正确配置数据类型"); + type = type.toUpperCase(); + switch (type) { + case "LONG": + stringBuilder.append("BIGINT").append(StrUtil.SPACE); + break; + case "STRING": + stringBuilder.append("VARCHAR(").append(ObjectUtil.defaultIfNull(tableViewRowData.getLen(), 255)).append(")").append(StrUtil.SPACE); + break; + case "TEXT": + stringBuilder.append("TEXT").append(StrUtil.SPACE); + break; + case "INTEGER": + stringBuilder.append("int").append(StrUtil.SPACE); + break; + case "TINYINT": + stringBuilder.append("TINYINT").append(StrUtil.SPACE); + break; + case "FLOAT": + stringBuilder.append("float").append(StrUtil.SPACE); + break; + case "DOUBLE": + stringBuilder.append("double").append(StrUtil.SPACE); + break; + default: + throw new IllegalArgumentException("不支持的数据类型:" + type); + } + // + Boolean notNull = tableViewRowData.getNotNull(); + if (notNull != null && notNull) { + stringBuilder.append("not null").append(StrUtil.SPACE); + } + // + String defaultValue = tableViewRowData.getDefaultValue(); + if (StrUtil.isNotEmpty(defaultValue)) { + stringBuilder.append("default '").append(defaultValue).append("'").append(StrUtil.SPACE); + } + stringBuilder.append("comment '").append(tableViewRowData.getComment()).append("'"); + // + String columnSql = stringBuilder.toString(); + if (encode) { + columnSql = StrUtil.replace(columnSql, "'", "\\'"); + } + int length = StrUtil.length(columnSql); + Assert.state(length <= 180, "sql 语句太长啦"); + return columnSql; + } + + @Override + public String delimiter() { + return "-- mariadb delimiter"; + } +} diff --git a/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageService b/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageService new file mode 100644 index 000000000..0b26db7e3 --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageService @@ -0,0 +1 @@ +org.dromara.jpom.storage.MariadbStorageServiceImpl diff --git a/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageSqlBuilderService b/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageSqlBuilderService new file mode 100644 index 000000000..260d0be7b --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/src/main/resources/META-INF/services/org.dromara.jpom.db.IStorageSqlBuilderService @@ -0,0 +1 @@ +org.dromara.jpom.storage.MariadbTableBuilderImpl diff --git a/modules/storage-module/storage-module-mariadb/src/main/resources/sql-view/execute.mariadb.v1.0.sql b/modules/storage-module/storage-module-mariadb/src/main/resources/sql-view/execute.mariadb.v1.0.sql new file mode 100644 index 000000000..fa55237c5 --- /dev/null +++ b/modules/storage-module/storage-module-mariadb/src/main/resources/sql-view/execute.mariadb.v1.0.sql @@ -0,0 +1,88 @@ +-- +-- Copyright (c) 2019 Of Him Code Technology Studio +-- Jpom is licensed under Mulan PSL v2. +-- You can use this software according to the terms and conditions of the Mulan PSL v2. +-- You may obtain a copy of Mulan PSL v2 at: +-- http://license.coscl.org.cn/MulanPSL2 +-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +-- See the Mulan PSL v2 for more details. +-- + +DROP FUNCTION IF EXISTS column_exists; + +-- mariadb delimiter + +CREATE FUNCTION column_exists1( + tname VARCHAR(64), + cname VARCHAR(64) +) + RETURNS BOOLEAN + READS SQL DATA +BEGIN + RETURN 0 < (SELECT COUNT(*) + FROM `INFORMATION_SCHEMA`.`COLUMNS` + WHERE `TABLE_SCHEMA` = SCHEMA() + AND `TABLE_NAME` = tname + AND `COLUMN_NAME` = cname); +END + +-- mariadb delimiter + +DROP PROCEDURE IF EXISTS drop_column_if_exists; + +-- mariadb delimiter + +CREATE PROCEDURE drop_column_if_exists( + tname VARCHAR(64), + cname VARCHAR(64) +) +BEGIN + IF column_exists1(tname, cname) + THEN + SET @drop_column_if_exists = CONCAT('ALTER TABLE `', tname, '` DROP COLUMN `', cname, '`'); + PREPARE drop_query FROM @drop_column_if_exists; + EXECUTE drop_query; + END IF; +END + +-- mariadb delimiter + +DROP PROCEDURE IF EXISTS add_column_if_not_exists; + +-- mariadb delimiter + +CREATE PROCEDURE add_column_if_not_exists( + tname VARCHAR(64), + cname VARCHAR(64), + columninfo VARCHAR(200) +) +BEGIN + IF column_exists1(tname, cname) + THEN + SET @add_column_sql = ''; + else + SET @add_column_sql = CONCAT('ALTER TABLE `', tname, '` ADD COLUMN ', columninfo); + PREPARE execute_query FROM @add_column_sql; + EXECUTE execute_query; + END IF; +END + +-- mariadb delimiter + +DROP PROCEDURE IF EXISTS drop_index_if_exists; + +-- mariadb delimiter + +create procedure drop_index_if_exists( + p_tablename varchar(200), + p_idxname VARCHAR(200) +) +begin + select count(*) into @cnt from information_schema.statistics where `TABLE_SCHEMA` = SCHEMA() and table_name = p_tablename and index_name = p_idxname; + if @cnt > 0 then + set @str = concat('drop index ', p_idxname, ' on ', p_tablename); + PREPARE execute_query FROM @str; + EXECUTE execute_query; + end if; + +end; diff --git a/modules/server/src/main/resources/sql-view/execute.mysql.v1.0.sql b/modules/storage-module/storage-module-mysql/src/main/resources/sql-view/execute.mysql.v1.0.sql similarity index 100% rename from modules/server/src/main/resources/sql-view/execute.mysql.v1.0.sql rename to modules/storage-module/storage-module-mysql/src/main/resources/sql-view/execute.mysql.v1.0.sql diff --git a/modules/storage-module/storage-module-postgresql/README.md b/modules/storage-module/storage-module-postgresql/README.md new file mode 100644 index 000000000..adc758f2e --- /dev/null +++ b/modules/storage-module/storage-module-postgresql/README.md @@ -0,0 +1,7 @@ +```shell +docker run --name jpom-postgres \ +-e POSTGRES_USER=jpom \ +-e POSTGRES_PASSWORD=jpom123456 \ +-e POSTGRES_DB=jpom \ +-p 5432:5432 -d postgres +``` \ No newline at end of file diff --git a/modules/server/src/main/resources/sql-view/execute.postgresql.v1.0.sql b/modules/storage-module/storage-module-postgresql/src/main/resources/sql-view/execute.postgresql.v1.0.sql similarity index 100% rename from modules/server/src/main/resources/sql-view/execute.postgresql.v1.0.sql rename to modules/storage-module/storage-module-postgresql/src/main/resources/sql-view/execute.postgresql.v1.0.sql