From 18728a44ceb9c3281443ea3f96f25b59c402a817 Mon Sep 17 00:00:00 2001 From: Parker Date: Mon, 1 Feb 2021 18:27:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E4=BC=98=E5=8C=96-=E8=A7=A3=E5=86=B3=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93=E7=97=9B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/creater/enums/DataBaseType.java | 63 ++++++++ .../strategy/sync/MySQLSyncBuilder.java | 5 +- .../creater/strategy/sync/SyncStrategy.java | 3 +- .../{SQLSyncUtil.java => SqlSyncUtil.java} | 11 +- .../creater/importable/ImportTableUtil.java | 138 ++++++++++++++---- .../creater/importable/constants/DbType.java | 19 --- .../importable/entity/DatabaseTable.java | 3 +- .../service/DatabaseTableService.java | 3 +- .../MySQLDatabaseTableServiceImpl.java | 6 +- .../table/service/impl/TableServiceImpl.java | 2 +- .../table/web/TableRestController.java | 4 +- .../src/main/resources/creater.yaml | 5 +- 12 files changed, 198 insertions(+), 64 deletions(-) create mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/enums/DataBaseType.java rename opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/{SQLSyncUtil.java => SqlSyncUtil.java} (85%) delete mode 100644 opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/enums/DataBaseType.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/enums/DataBaseType.java new file mode 100644 index 00000000..452ec393 --- /dev/null +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/enums/DataBaseType.java @@ -0,0 +1,63 @@ +package org.opsli.core.creater.enums; + +/** + * 数据库类型 + * + * @author Mybatis-plus + */ +public enum DataBaseType { + + /** 数据库类型 */ + MYSQL("mysql", "MySql数据库"), + MARIADB("mariadb", "MariaDB数据库"), + ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"), + ORACLE_12C("oracle12c", "Oracle12c+数据库"), + DB2("db2", "DB2数据库"), + H2("h2", "H2数据库"), + HSQL("hsql", "HSQL数据库"), + SQLITE("sqlite", "SQLite数据库"), + POSTGRE_SQL("postgresql", "Postgre数据库"), + SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"), + SQL_SERVER("sqlserver", "SQLServer数据库"), + DM("dm", "达梦数据库"), + XU_GU("xugu", "虚谷数据库"), + KINGBASE_ES("kingbasees", "人大金仓数据库"), + PHOENIX("phoenix", "Phoenix HBase数据库"), + GAUSS("zenith", "Gauss 数据库"), + CLICK_HOUSE("clickhouse", "clickhouse 数据库"), + GBASE("gbase", "南大通用数据库"), + OSCAR("oscar", "神通数据库"), + SYBASE("sybase", "Sybase ASE 数据库"), + OCEAN_BASE("oceanbase", "OceanBase 数据库"), + FIREBIRD("Firebird", "Firebird 数据库"), + OTHER("other", "其他数据库"); + + private final String db; + private final String desc; + + public static DataBaseType getDbType(String dbType) { + DataBaseType[] var1 = values(); + int var2 = var1.length; + + for (DataBaseType type : var1) { + if (type.db.equalsIgnoreCase(dbType)) { + return type; + } + } + + return OTHER; + } + + public String getDb() { + return this.db; + } + + public String getDesc() { + return this.desc; + } + + private DataBaseType(final String db, final String desc) { + this.db = db; + this.desc = desc; + } +} \ No newline at end of file diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java index cf407197..7bb3fd12 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/MySQLSyncBuilder.java @@ -18,6 +18,7 @@ package org.opsli.core.creater.strategy.sync; import org.apache.commons.lang3.StringUtils; import org.opsli.common.enums.DictType; import org.opsli.common.utils.Props; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.core.creater.exception.CreaterException; import org.opsli.core.creater.msg.CreaterMsg; import org.opsli.core.creater.strategy.sync.mysql.entity.FieldTypeAttribute; @@ -66,8 +67,8 @@ public class MySQLSyncBuilder implements SyncStrategy { private ITableService iTableService; @Override - public String getType() { - return "mysql"; + public DataBaseType getType() { + return DataBaseType.MYSQL; } /** diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/SyncStrategy.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/SyncStrategy.java index 271ad8c8..56d30fc2 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/SyncStrategy.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/SyncStrategy.java @@ -15,6 +15,7 @@ */ package org.opsli.core.creater.strategy.sync; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel; /** @@ -30,7 +31,7 @@ public interface SyncStrategy { * 获得分类 * @return */ - String getType(); + DataBaseType getType(); /** * 执行 同步操作 diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SQLSyncUtil.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SqlSyncUtil.java similarity index 85% rename from opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SQLSyncUtil.java rename to opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SqlSyncUtil.java index 81dd1b05..a3285554 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SQLSyncUtil.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/core/creater/strategy/sync/util/SqlSyncUtil.java @@ -17,6 +17,7 @@ package org.opsli.core.creater.strategy.sync.util; import cn.hutool.core.util.ClassUtil; import lombok.extern.slf4j.Slf4j; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.core.creater.strategy.sync.SyncStrategy; import org.opsli.core.utils.SpringContextHolder; import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel; @@ -38,11 +39,11 @@ import java.util.concurrent.ConcurrentMap; */ @Slf4j @Configuration -public class SQLSyncUtil { +public class SqlSyncUtil { /** 处理方法集合 */ - private static final ConcurrentMap HANDLER_MAP = new ConcurrentHashMap<>(); + private static final ConcurrentMap HANDLER_MAP = new ConcurrentHashMap<>(); @Bean @@ -74,7 +75,11 @@ public class SQLSyncUtil { return; } - SyncStrategy syncStrategy = HANDLER_MAP.get(model.getJdbcType()); + // 获得数据库类型 枚举 + String jdbcType = model.getJdbcType(); + DataBaseType dbType = DataBaseType.getDbType(jdbcType); + + SyncStrategy syncStrategy = HANDLER_MAP.get(dbType); if(syncStrategy != null){ syncStrategy.execute(model); } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java index ac9ebe7e..551cd5aa 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/ImportTableUtil.java @@ -15,20 +15,25 @@ */ package org.opsli.modulars.creater.importable; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ClassUtil; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; -import org.opsli.common.utils.Props; -import org.opsli.core.creater.strategy.sync.SyncStrategy; +import org.apache.commons.lang3.StringUtils; +import org.opsli.core.autoconfigure.DbSourceProperties; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.core.utils.SpringContextHolder; import org.opsli.modulars.creater.importable.entity.DatabaseColumn; import org.opsli.modulars.creater.importable.entity.DatabaseTable; import org.opsli.modulars.creater.importable.service.DatabaseTableService; -import org.opsli.modulars.creater.table.wrapper.CreaterTableAndColumnModel; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Modifier; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -44,55 +49,130 @@ import java.util.concurrent.ConcurrentMap; @Configuration public class ImportTableUtil{ - /** 数据库名 */ - public static final String DB_NAME; - /** 数据库类型 */ - public static final String DB_TYPE; /** 处理方法集合 */ - private static final ConcurrentMap HANDLER_MAP = new ConcurrentHashMap<>(); - + private static final ConcurrentMap HANDLER_MAP = new ConcurrentHashMap<>(); + /** 数据库类型 */ + private static final Map DB_TYPE_MAP; + /** 指定 master 数据库 */ + private static final String ASSIGN_DB = "master"; static { - Props props = new Props("creater.yaml"); - DB_NAME = props.getStr("opsli.db-name","opsli-boot"); - DB_TYPE = props.getStr("opsli.db-type"); + DB_TYPE_MAP = Maps.newHashMap(); + DB_TYPE_MAP.put("com.mysql.jdbc.Driver", DataBaseType.MYSQL); + DB_TYPE_MAP.put("com.mysql.cj.jdbc.Driver", DataBaseType.MYSQL); + DB_TYPE_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", DataBaseType.SQL_SERVER); + DB_TYPE_MAP.put("com.microsoft.sqlserver.jdbc.SQLServerDriver", DataBaseType.SQL_SERVER); + DB_TYPE_MAP.put("com.sybase.jdbc.SybDriver", DataBaseType.SYBASE); + DB_TYPE_MAP.put("oracle.jdbc.driver.OracleDriver", DataBaseType.ORACLE); + DB_TYPE_MAP.put("org.postgresql.Driver", DataBaseType.POSTGRE_SQL); + DB_TYPE_MAP.put("com.ibm.db2.jdbc.app.DB2.Driver", DataBaseType.DB2); + } + + /** 数据库信息 */ + private static DbSourceProperties dbSourceProperties; + + /** + * 获得 数据库类型 + * @return 数据库类型 + */ + public static DataBaseType getDbType(){ + if(dbSourceProperties != null){ + Map dataSourceInfoMap = dbSourceProperties.getDataSourceInfoMap(); + DbSourceProperties.DataSourceInfo dataSourceInfo = dataSourceInfoMap.get(ASSIGN_DB); + if(dataSourceInfo != null){ + return DB_TYPE_MAP.get(dataSourceInfo.getDriverClassName()); + } + } + return null; } /** * 获得当前数据库中表 - * @return + * @return List */ public static List findTables() { - DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); - if(databaseTableService == null){ - return null; - } - return databaseTableService.findTables(DB_NAME); + return ImportTableUtil.findTables(null); } /** * 获得当前数据库中表 - * @param tableName - * @return + * @param tableName 表名 + * @return List */ public static List findTables(String tableName) { - DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); - if(databaseTableService == null){ + Map dataSourceInfoMap = + dbSourceProperties.getDataSourceInfoMap(); + // 非法判断 + if(CollUtil.isEmpty(dataSourceInfoMap)){ return null; } - return databaseTableService.findTables(DB_NAME, tableName); + + + List databaseTables = Lists.newArrayList(); + for (Map.Entry dataSourceInfoEntry + : dataSourceInfoMap.entrySet()) { + // 如果master不为空 且 多数据源不为主数据源 则不进行处理 + if(!StringUtils.equals(ASSIGN_DB, dataSourceInfoEntry.getKey())){ + continue; + } + + // 数据源 + String key = dataSourceInfoEntry.getKey(); + DbSourceProperties.DataSourceInfo dataSource = dataSourceInfoEntry.getValue(); + + // 根据类型获得查询器 + DataBaseType dataBaseType = DB_TYPE_MAP.get(dataSource.getDriverClassName()); + DatabaseTableService databaseTableService = HANDLER_MAP.get(dataBaseType); + if(databaseTableService == null){ + continue; + } + + // 获得当前库下表集合 + List tables; + if(StringUtils.isNotEmpty(tableName)){ + tables = databaseTableService.findTables(dataSource.getDbName(), tableName); + }else{ + tables = databaseTableService.findTables(dataSource.getDbName()); + } + + // 如果不为空则存入集合 + if(CollUtil.isNotEmpty(tables)){ + for (DatabaseTable table : tables) { + table.setDbSource(key); + } + databaseTables.addAll(tables); + } + } + + return databaseTables; } /** * 获得表字段 - * @param tableName - * @return + * @param tableName 表名 + * @return List */ public static List findColumns(String tableName) { - DatabaseTableService databaseTableService = HANDLER_MAP.get(DB_TYPE); + + Map dataSourceInfoMap = + dbSourceProperties.getDataSourceInfoMap(); + // 非法判断 + if(CollUtil.isEmpty(dataSourceInfoMap)){ + return null; + } + + DbSourceProperties.DataSourceInfo dataSource = dataSourceInfoMap.get(ASSIGN_DB); + if(dataSource == null){ + return null; + } + + // 根据类型获得查询器 + DataBaseType dataBaseType = DB_TYPE_MAP.get(dataSource.getDriverClassName()); + DatabaseTableService databaseTableService = HANDLER_MAP.get(dataBaseType); if(databaseTableService == null){ return null; } - return databaseTableService.findColumns(DB_NAME, tableName); + + return databaseTableService.findColumns(dataSource.getDbName(), tableName); } @@ -119,4 +199,8 @@ public class ImportTableUtil{ } } + @Autowired + public void setDbSourceProperties(DbSourceProperties dbSourceProperties) { + ImportTableUtil.dbSourceProperties = dbSourceProperties; + } } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java deleted file mode 100644 index a877d042..00000000 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/constants/DbType.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.opsli.modulars.creater.importable.constants; - -/** - * @BelongsProject: opsli-boot - * @BelongsPackage: org.opsli.modulars.creater.importable.constants - * @Author: Parker - * @CreateTime: 2020-11-19 15:27 - * @Description: 数据库类型 - */ -public interface DbType { - - String DB_MYSQL = "mysql"; - - String DB_ORACLE = "oracle"; - - String DB_DB2 = "db2"; - - String DB_SQLSERVER = "sqlserver"; -} diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java index 4eac9338..0467d939 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/entity/DatabaseTable.java @@ -28,6 +28,8 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = false) public class DatabaseTable { + /** 数据源 */ + private String dbSource; /** 数据库 */ private String dbName; @@ -38,7 +40,6 @@ public class DatabaseTable { /** 描述 */ private String tableComments; - // ======================================== } diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java index c1d80625..e70d8545 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/DatabaseTableService.java @@ -15,6 +15,7 @@ */ package org.opsli.modulars.creater.importable.service; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.modulars.creater.importable.entity.DatabaseColumn; import org.opsli.modulars.creater.importable.entity.DatabaseTable; @@ -33,7 +34,7 @@ public interface DatabaseTableService { * 获得类型 * @return */ - String getType(); + DataBaseType getType(); /** * 获得当前库中 所有表 diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java index 491adc2e..088e5efd 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/importable/service/MySQLDatabaseTableServiceImpl.java @@ -17,7 +17,7 @@ package org.opsli.modulars.creater.importable.service; import org.apache.commons.lang3.StringUtils; import org.opsli.common.utils.Props; -import org.opsli.modulars.creater.importable.constants.DbType; +import org.opsli.core.creater.enums.DataBaseType; import org.opsli.modulars.creater.importable.entity.DatabaseColumn; import org.opsli.modulars.creater.importable.entity.DatabaseTable; import org.opsli.modulars.creater.importable.mapper.MySQLDatabaseTableMapper; @@ -52,8 +52,8 @@ public class MySQLDatabaseTableServiceImpl implements DatabaseTableService { private ITableService iTableService; @Override - public String getType() { - return DbType.DB_MYSQL; + public DataBaseType getType() { + return DataBaseType.MYSQL; } @Override diff --git a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java index 8ea02857..759af939 100644 --- a/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java +++ b/opsli-modulars/opsli-modulars-creater/src/main/java/org/opsli/modulars/creater/table/service/impl/TableServiceImpl.java @@ -243,7 +243,7 @@ public class TableServiceImpl extends CrudServiceImpl