diff --git a/srt-cloud-data-integrate/src/main/java/net/srt/service/impl/DataDatabaseServiceImpl.java b/srt-cloud-data-integrate/src/main/java/net/srt/service/impl/DataDatabaseServiceImpl.java index 4d10450..fd4efad 100644 --- a/srt-cloud-data-integrate/src/main/java/net/srt/service/impl/DataDatabaseServiceImpl.java +++ b/srt-cloud-data-integrate/src/main/java/net/srt/service/impl/DataDatabaseServiceImpl.java @@ -134,6 +134,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl"); + entity.setName(project.getName() + "<>"); List nodeList = new ArrayList<>(1); TreeNodeVo dbNode = new TreeNodeVo(); nodeList.add(dbNode); diff --git a/srt-cloud-data-integrate/src/main/java/net/srt/vo/DataDatabaseVO.java b/srt-cloud-data-integrate/src/main/java/net/srt/vo/DataDatabaseVO.java index 414ea64..0d7dc3f 100644 --- a/srt-cloud-data-integrate/src/main/java/net/srt/vo/DataDatabaseVO.java +++ b/srt-cloud-data-integrate/src/main/java/net/srt/vo/DataDatabaseVO.java @@ -78,5 +78,8 @@ public class DataDatabaseVO implements Serializable { @JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN) private Date updateTime; + @Schema(description = "是否是关系型数据库") + private Integer isJdbc; + } diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/common/type/ProductTypeEnum.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/common/type/ProductTypeEnum.java index 8ef7e62..e311415 100644 --- a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/common/type/ProductTypeEnum.java +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/common/type/ProductTypeEnum.java @@ -100,6 +100,12 @@ public enum ProductTypeEnum { * MySQL数据库类型 */ DORIS(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", "jdbc:mysql://{host}:{port}/{database}?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true"), + + REDIS(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), + KAFKA(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), + ES(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), + FTP(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), + MONGODB(16, "com.mysql.jdbc.Driver","/* ping */ SELECT 1", ""), ; private Integer index; diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/DatabaseFactory.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/DatabaseFactory.java index 45b673f..c4564df 100644 --- a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/DatabaseFactory.java +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/DatabaseFactory.java @@ -10,22 +10,7 @@ package srt.cloud.framework.dbswitch.core.database; import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseDB2Impl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseDmImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseDorisImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseGbase8aImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseGreenplumImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseHiveImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseKingbaseImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseMariaDBImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseMysqlImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseOracleImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseOscarImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabasePostgresImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseSqliteImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseSqlserver2000Impl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseSqlserverImpl; -import srt.cloud.framework.dbswitch.core.database.impl.DatabaseSybaseImpl; +import srt.cloud.framework.dbswitch.core.database.impl.*; import java.util.HashMap; import java.util.Map; @@ -60,6 +45,12 @@ public final class DatabaseFactory { put(ProductTypeEnum.HIVE, DatabaseHiveImpl::new); put(ProductTypeEnum.SQLITE3, DatabaseSqliteImpl::new); put(ProductTypeEnum.DORIS, DatabaseDorisImpl::new); + + put(ProductTypeEnum.REDIS, DatabaseRedisImpl::new); + put(ProductTypeEnum.KAFKA, DatabaseKafkaImpl::new); + put(ProductTypeEnum.ES, DatabaseEsImpl::new); + put(ProductTypeEnum.FTP, DatabaseEsImpl::new); + put(ProductTypeEnum.MONGODB, DatabaseFMongodbmpl::new); } }; diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseEsImpl.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseEsImpl.java new file mode 100644 index 0000000..ce28261 --- /dev/null +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseEsImpl.java @@ -0,0 +1,274 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package srt.cloud.framework.dbswitch.core.database.impl; + +import org.apache.commons.lang3.StringUtils; +import srt.cloud.framework.dbswitch.common.constant.Const; +import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; +import srt.cloud.framework.dbswitch.core.database.IDatabaseInterface; +import srt.cloud.framework.dbswitch.core.model.ColumnDescription; +import srt.cloud.framework.dbswitch.core.model.ColumnMetaData; +import srt.cloud.framework.dbswitch.core.model.TableDescription; + +import java.sql.*; +import java.util.*; + +/** + * 支持MySQL数据库的元信息实现 + * + * @author jrl + */ +public class DatabaseEsImpl extends DatabaseMysqlImpl implements IDatabaseInterface { + + private static final String SHOW_CREATE_TABLE_SQL = "SHOW CREATE TABLE `%s`.`%s` "; + private static final String SHOW_CREATE_VIEW_SQL = "SHOW CREATE VIEW `%s`.`%s` "; + + public DatabaseEsImpl() { + super("com.mysql.jdbc.Driver"); + } + + public DatabaseEsImpl(String driverClassName) { + super(driverClassName); + } + + @Override + public ProductTypeEnum getDatabaseType() { + return ProductTypeEnum.DORIS; + } + + @Override + public List queryTableColumnMeta(Connection connection, String schemaName, + String tableName) { +// String sql = this.getTableFieldsQuerySQL(schemaName, tableName); +// List columnDescriptions = this.querySelectSqlColumnMeta(connection, sql); +// // 补充一下字段信息,获取的不准 +// String extraSql = "SELECT column_name,data_type,column_size,decimal_digits,column_comment FROM information_schema.COLUMNS WHERE table_schema='" + schemaName + "' AND table_name='" + tableName + "'"; +// try (PreparedStatement ps = connection.prepareStatement(extraSql); +// ResultSet rs = ps.executeQuery(); +// ) { +// while (rs.next()) { +// String columnName = rs.getString("column_name"); +// String dataType = rs.getString("data_type"); +// String columnSize = rs.getString("column_size"); +// String decimalDigits = rs.getString("decimal_digits"); +// String columnComment = rs.getString("column_comment"); +// if (columnName != null) { +// for (ColumnDescription cd : columnDescriptions) { +// if (columnName.equals(cd.getFieldName())) { +// cd.setFieldTypeName(dataType); +// int csize = columnSize != null ? Integer.parseInt(columnSize) : 0; +// cd.setDisplaySize(csize); +// cd.setPrecisionSize(csize); +// cd.setScaleSize(decimalDigits != null ? Integer.parseInt(decimalDigits) : 0); +// cd.setRemarks(columnComment); +// break; +// } +// } +// } +// } +// } catch (SQLException e) { +// throw new RuntimeException(schemaName + "." + tableName + " queryTableColumnMeta error!!", e); +// } +// +// return columnDescriptions; + return null; + } + + @Override + public String getFieldDefinition(ColumnMetaData v, List pks, boolean useAutoInc, + boolean addCr, boolean withRemarks) { + String fieldname = v.getName(); + int length = v.getLength(); + int precision = v.getPrecision(); + int type = v.getType(); + + String retval = " `" + fieldname + "` "; + boolean canHaveDefaultValue = true; + switch (type) { + case ColumnMetaData.TYPE_TIMESTAMP: + case ColumnMetaData.TYPE_TIME: + retval += "DATETIME"; + break; + case ColumnMetaData.TYPE_DATE: + retval += "DATE"; + break; + case ColumnMetaData.TYPE_BOOLEAN: + retval += "TINYINT"; + break; + case ColumnMetaData.TYPE_NUMBER: + case ColumnMetaData.TYPE_INTEGER: + case ColumnMetaData.TYPE_BIGNUMBER: + if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) { + retval += "BIGINT"; + } else { + // Integer values... + if (precision == 0) { + if (length > 9) { + if (length < 19) { + // can hold signed values between -9223372036854775808 and 9223372036854775807 + // 18 significant digits + retval += "BIGINT"; + } else { + retval += "DECIMAL(" + length + ")"; + } + } else { + retval += "INT"; + } + } else { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + // Floating point values... + /*if (length > 15) { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + } else { + // A double-precision floating-point number is accurate to approximately 15 + // decimal places. + // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html + retval += "DOUBLE"; + }*/ + } + } + break; + case ColumnMetaData.TYPE_STRING: + if (length * 3 <= 65533) { + retval += "VARCHAR(" + length * 3 + ")"; + } else { + retval += "TEXT"; + canHaveDefaultValue = false; + } + break; + default: + retval += "TEXT"; + canHaveDefaultValue = false; + break; + } + + if (!v.isNullable()) { + retval += " NOT NULL"; + } + + if (canHaveDefaultValue && v.getDefaultValue() != null && !"null".equals(v.getDefaultValue()) && !"NULL".equals(v.getDefaultValue())) { + if (type != ColumnMetaData.TYPE_TIMESTAMP && type != ColumnMetaData.TYPE_TIME && type != ColumnMetaData.TYPE_DATE) { + if (v.getDefaultValue().startsWith("'")) { + retval += " DEFAULT " + v.getDefaultValue(); + } else { + retval += " DEFAULT '" + v.getDefaultValue() + "'"; + } + } else { + retval += " DEFAULT CURRENT_TIMESTAMP"; + } + } + + + if (withRemarks && StringUtils.isNotBlank(v.getRemarks())) { + retval += String.format(" COMMENT '%s' ", v.getRemarks().replace("'", "\\'")); + } + + if (addCr) { + retval += Const.CR; + } + + return retval; + } + + @Override + public boolean canCreateIndex(ColumnMetaData v) { + return false; + } + + @Override + public String getPrimaryKeyAsString(List pks) { + if (null != pks && !pks.isEmpty()) { + return "`" + + StringUtils.join(pks, "` , `") + + "`"; + } + + return ""; + } + + @Override + public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List columnDescriptions) { + String sql = this.getDefaultValueSql(schemaName, tableName); + try (Statement st = connection.createStatement()) { + try (ResultSet rs = st.executeQuery(sql)) { + while (rs.next()) { + String columnName = rs.getString("Field"); + String columnDefault = rs.getString("Default"); + if (columnName != null) { + for (ColumnDescription cd : columnDescriptions) { + if (columnName.equals(cd.getFieldName())) { + cd.setDefaultValue(columnDefault); + break; + } + } + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getDefaultValueSql(String schemaName, String tableName) { + return String.format("desc `%s`.`%s`", schemaName, tableName); + } + + + @Override + public List queryTablePrimaryKeys(Connection connection, String schemaName, + String tableName) { + Set ret = new HashSet<>(); + String sql = String.format("desc `%s`.`%s`", schemaName, tableName); + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + ) { + //看下是否又none的字段,如果有,说明key模式为DUPLICATE KEY 可重复 + boolean NoneExtra = false; + while (rs.next()) { + String field = rs.getString("Field"); + String key = rs.getString("Key"); + String extra = rs.getString("Extra"); + if ("true".equalsIgnoreCase(key)) { + ret.add(field); + } else { + if ("NONE".equalsIgnoreCase(extra)) { + NoneExtra = true; + } + } + } + if (NoneExtra) { + return new ArrayList<>(); + } + return new ArrayList<>(ret); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void setColumnIndexInfo(Connection connection, String schemaName, String tableName, List columnDescriptions) { + } + + @Override + public List getTableColumnCommentDefinition(TableDescription td, + List cds) { + return Collections.emptyList(); + } + +} diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFMongodbmpl.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFMongodbmpl.java new file mode 100644 index 0000000..616e59c --- /dev/null +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFMongodbmpl.java @@ -0,0 +1,274 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package srt.cloud.framework.dbswitch.core.database.impl; + +import org.apache.commons.lang3.StringUtils; +import srt.cloud.framework.dbswitch.common.constant.Const; +import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; +import srt.cloud.framework.dbswitch.core.database.IDatabaseInterface; +import srt.cloud.framework.dbswitch.core.model.ColumnDescription; +import srt.cloud.framework.dbswitch.core.model.ColumnMetaData; +import srt.cloud.framework.dbswitch.core.model.TableDescription; + +import java.sql.*; +import java.util.*; + +/** + * 支持MySQL数据库的元信息实现 + * + * @author jrl + */ +public class DatabaseFMongodbmpl extends DatabaseMysqlImpl implements IDatabaseInterface { + + private static final String SHOW_CREATE_TABLE_SQL = "SHOW CREATE TABLE `%s`.`%s` "; + private static final String SHOW_CREATE_VIEW_SQL = "SHOW CREATE VIEW `%s`.`%s` "; + + public DatabaseFMongodbmpl() { + super("com.mysql.jdbc.Driver"); + } + + public DatabaseFMongodbmpl(String driverClassName) { + super(driverClassName); + } + + @Override + public ProductTypeEnum getDatabaseType() { + return ProductTypeEnum.DORIS; + } + + @Override + public List queryTableColumnMeta(Connection connection, String schemaName, + String tableName) { +// String sql = this.getTableFieldsQuerySQL(schemaName, tableName); +// List columnDescriptions = this.querySelectSqlColumnMeta(connection, sql); +// // 补充一下字段信息,获取的不准 +// String extraSql = "SELECT column_name,data_type,column_size,decimal_digits,column_comment FROM information_schema.COLUMNS WHERE table_schema='" + schemaName + "' AND table_name='" + tableName + "'"; +// try (PreparedStatement ps = connection.prepareStatement(extraSql); +// ResultSet rs = ps.executeQuery(); +// ) { +// while (rs.next()) { +// String columnName = rs.getString("column_name"); +// String dataType = rs.getString("data_type"); +// String columnSize = rs.getString("column_size"); +// String decimalDigits = rs.getString("decimal_digits"); +// String columnComment = rs.getString("column_comment"); +// if (columnName != null) { +// for (ColumnDescription cd : columnDescriptions) { +// if (columnName.equals(cd.getFieldName())) { +// cd.setFieldTypeName(dataType); +// int csize = columnSize != null ? Integer.parseInt(columnSize) : 0; +// cd.setDisplaySize(csize); +// cd.setPrecisionSize(csize); +// cd.setScaleSize(decimalDigits != null ? Integer.parseInt(decimalDigits) : 0); +// cd.setRemarks(columnComment); +// break; +// } +// } +// } +// } +// } catch (SQLException e) { +// throw new RuntimeException(schemaName + "." + tableName + " queryTableColumnMeta error!!", e); +// } +// +// return columnDescriptions; + return null; + } + + @Override + public String getFieldDefinition(ColumnMetaData v, List pks, boolean useAutoInc, + boolean addCr, boolean withRemarks) { + String fieldname = v.getName(); + int length = v.getLength(); + int precision = v.getPrecision(); + int type = v.getType(); + + String retval = " `" + fieldname + "` "; + boolean canHaveDefaultValue = true; + switch (type) { + case ColumnMetaData.TYPE_TIMESTAMP: + case ColumnMetaData.TYPE_TIME: + retval += "DATETIME"; + break; + case ColumnMetaData.TYPE_DATE: + retval += "DATE"; + break; + case ColumnMetaData.TYPE_BOOLEAN: + retval += "TINYINT"; + break; + case ColumnMetaData.TYPE_NUMBER: + case ColumnMetaData.TYPE_INTEGER: + case ColumnMetaData.TYPE_BIGNUMBER: + if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) { + retval += "BIGINT"; + } else { + // Integer values... + if (precision == 0) { + if (length > 9) { + if (length < 19) { + // can hold signed values between -9223372036854775808 and 9223372036854775807 + // 18 significant digits + retval += "BIGINT"; + } else { + retval += "DECIMAL(" + length + ")"; + } + } else { + retval += "INT"; + } + } else { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + // Floating point values... + /*if (length > 15) { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + } else { + // A double-precision floating-point number is accurate to approximately 15 + // decimal places. + // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html + retval += "DOUBLE"; + }*/ + } + } + break; + case ColumnMetaData.TYPE_STRING: + if (length * 3 <= 65533) { + retval += "VARCHAR(" + length * 3 + ")"; + } else { + retval += "TEXT"; + canHaveDefaultValue = false; + } + break; + default: + retval += "TEXT"; + canHaveDefaultValue = false; + break; + } + + if (!v.isNullable()) { + retval += " NOT NULL"; + } + + if (canHaveDefaultValue && v.getDefaultValue() != null && !"null".equals(v.getDefaultValue()) && !"NULL".equals(v.getDefaultValue())) { + if (type != ColumnMetaData.TYPE_TIMESTAMP && type != ColumnMetaData.TYPE_TIME && type != ColumnMetaData.TYPE_DATE) { + if (v.getDefaultValue().startsWith("'")) { + retval += " DEFAULT " + v.getDefaultValue(); + } else { + retval += " DEFAULT '" + v.getDefaultValue() + "'"; + } + } else { + retval += " DEFAULT CURRENT_TIMESTAMP"; + } + } + + + if (withRemarks && StringUtils.isNotBlank(v.getRemarks())) { + retval += String.format(" COMMENT '%s' ", v.getRemarks().replace("'", "\\'")); + } + + if (addCr) { + retval += Const.CR; + } + + return retval; + } + + @Override + public boolean canCreateIndex(ColumnMetaData v) { + return false; + } + + @Override + public String getPrimaryKeyAsString(List pks) { + if (null != pks && !pks.isEmpty()) { + return "`" + + StringUtils.join(pks, "` , `") + + "`"; + } + + return ""; + } + + @Override + public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List columnDescriptions) { + String sql = this.getDefaultValueSql(schemaName, tableName); + try (Statement st = connection.createStatement()) { + try (ResultSet rs = st.executeQuery(sql)) { + while (rs.next()) { + String columnName = rs.getString("Field"); + String columnDefault = rs.getString("Default"); + if (columnName != null) { + for (ColumnDescription cd : columnDescriptions) { + if (columnName.equals(cd.getFieldName())) { + cd.setDefaultValue(columnDefault); + break; + } + } + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getDefaultValueSql(String schemaName, String tableName) { + return String.format("desc `%s`.`%s`", schemaName, tableName); + } + + + @Override + public List queryTablePrimaryKeys(Connection connection, String schemaName, + String tableName) { + Set ret = new HashSet<>(); + String sql = String.format("desc `%s`.`%s`", schemaName, tableName); + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + ) { + //看下是否又none的字段,如果有,说明key模式为DUPLICATE KEY 可重复 + boolean NoneExtra = false; + while (rs.next()) { + String field = rs.getString("Field"); + String key = rs.getString("Key"); + String extra = rs.getString("Extra"); + if ("true".equalsIgnoreCase(key)) { + ret.add(field); + } else { + if ("NONE".equalsIgnoreCase(extra)) { + NoneExtra = true; + } + } + } + if (NoneExtra) { + return new ArrayList<>(); + } + return new ArrayList<>(ret); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void setColumnIndexInfo(Connection connection, String schemaName, String tableName, List columnDescriptions) { + } + + @Override + public List getTableColumnCommentDefinition(TableDescription td, + List cds) { + return Collections.emptyList(); + } + +} diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFTPImpl.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFTPImpl.java new file mode 100644 index 0000000..7559a6c --- /dev/null +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseFTPImpl.java @@ -0,0 +1,274 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package srt.cloud.framework.dbswitch.core.database.impl; + +import org.apache.commons.lang3.StringUtils; +import srt.cloud.framework.dbswitch.common.constant.Const; +import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; +import srt.cloud.framework.dbswitch.core.database.IDatabaseInterface; +import srt.cloud.framework.dbswitch.core.model.ColumnDescription; +import srt.cloud.framework.dbswitch.core.model.ColumnMetaData; +import srt.cloud.framework.dbswitch.core.model.TableDescription; + +import java.sql.*; +import java.util.*; + +/** + * 支持MySQL数据库的元信息实现 + * + * @author jrl + */ +public class DatabaseFTPImpl extends DatabaseMysqlImpl implements IDatabaseInterface { + + private static final String SHOW_CREATE_TABLE_SQL = "SHOW CREATE TABLE `%s`.`%s` "; + private static final String SHOW_CREATE_VIEW_SQL = "SHOW CREATE VIEW `%s`.`%s` "; + + public DatabaseFTPImpl() { + super("com.mysql.jdbc.Driver"); + } + + public DatabaseFTPImpl(String driverClassName) { + super(driverClassName); + } + + @Override + public ProductTypeEnum getDatabaseType() { + return ProductTypeEnum.DORIS; + } + + @Override + public List queryTableColumnMeta(Connection connection, String schemaName, + String tableName) { +// String sql = this.getTableFieldsQuerySQL(schemaName, tableName); +// List columnDescriptions = this.querySelectSqlColumnMeta(connection, sql); +// // 补充一下字段信息,获取的不准 +// String extraSql = "SELECT column_name,data_type,column_size,decimal_digits,column_comment FROM information_schema.COLUMNS WHERE table_schema='" + schemaName + "' AND table_name='" + tableName + "'"; +// try (PreparedStatement ps = connection.prepareStatement(extraSql); +// ResultSet rs = ps.executeQuery(); +// ) { +// while (rs.next()) { +// String columnName = rs.getString("column_name"); +// String dataType = rs.getString("data_type"); +// String columnSize = rs.getString("column_size"); +// String decimalDigits = rs.getString("decimal_digits"); +// String columnComment = rs.getString("column_comment"); +// if (columnName != null) { +// for (ColumnDescription cd : columnDescriptions) { +// if (columnName.equals(cd.getFieldName())) { +// cd.setFieldTypeName(dataType); +// int csize = columnSize != null ? Integer.parseInt(columnSize) : 0; +// cd.setDisplaySize(csize); +// cd.setPrecisionSize(csize); +// cd.setScaleSize(decimalDigits != null ? Integer.parseInt(decimalDigits) : 0); +// cd.setRemarks(columnComment); +// break; +// } +// } +// } +// } +// } catch (SQLException e) { +// throw new RuntimeException(schemaName + "." + tableName + " queryTableColumnMeta error!!", e); +// } +// +// return columnDescriptions; + return null; + } + + @Override + public String getFieldDefinition(ColumnMetaData v, List pks, boolean useAutoInc, + boolean addCr, boolean withRemarks) { + String fieldname = v.getName(); + int length = v.getLength(); + int precision = v.getPrecision(); + int type = v.getType(); + + String retval = " `" + fieldname + "` "; + boolean canHaveDefaultValue = true; + switch (type) { + case ColumnMetaData.TYPE_TIMESTAMP: + case ColumnMetaData.TYPE_TIME: + retval += "DATETIME"; + break; + case ColumnMetaData.TYPE_DATE: + retval += "DATE"; + break; + case ColumnMetaData.TYPE_BOOLEAN: + retval += "TINYINT"; + break; + case ColumnMetaData.TYPE_NUMBER: + case ColumnMetaData.TYPE_INTEGER: + case ColumnMetaData.TYPE_BIGNUMBER: + if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) { + retval += "BIGINT"; + } else { + // Integer values... + if (precision == 0) { + if (length > 9) { + if (length < 19) { + // can hold signed values between -9223372036854775808 and 9223372036854775807 + // 18 significant digits + retval += "BIGINT"; + } else { + retval += "DECIMAL(" + length + ")"; + } + } else { + retval += "INT"; + } + } else { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + // Floating point values... + /*if (length > 15) { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + } else { + // A double-precision floating-point number is accurate to approximately 15 + // decimal places. + // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html + retval += "DOUBLE"; + }*/ + } + } + break; + case ColumnMetaData.TYPE_STRING: + if (length * 3 <= 65533) { + retval += "VARCHAR(" + length * 3 + ")"; + } else { + retval += "TEXT"; + canHaveDefaultValue = false; + } + break; + default: + retval += "TEXT"; + canHaveDefaultValue = false; + break; + } + + if (!v.isNullable()) { + retval += " NOT NULL"; + } + + if (canHaveDefaultValue && v.getDefaultValue() != null && !"null".equals(v.getDefaultValue()) && !"NULL".equals(v.getDefaultValue())) { + if (type != ColumnMetaData.TYPE_TIMESTAMP && type != ColumnMetaData.TYPE_TIME && type != ColumnMetaData.TYPE_DATE) { + if (v.getDefaultValue().startsWith("'")) { + retval += " DEFAULT " + v.getDefaultValue(); + } else { + retval += " DEFAULT '" + v.getDefaultValue() + "'"; + } + } else { + retval += " DEFAULT CURRENT_TIMESTAMP"; + } + } + + + if (withRemarks && StringUtils.isNotBlank(v.getRemarks())) { + retval += String.format(" COMMENT '%s' ", v.getRemarks().replace("'", "\\'")); + } + + if (addCr) { + retval += Const.CR; + } + + return retval; + } + + @Override + public boolean canCreateIndex(ColumnMetaData v) { + return false; + } + + @Override + public String getPrimaryKeyAsString(List pks) { + if (null != pks && !pks.isEmpty()) { + return "`" + + StringUtils.join(pks, "` , `") + + "`"; + } + + return ""; + } + + @Override + public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List columnDescriptions) { + String sql = this.getDefaultValueSql(schemaName, tableName); + try (Statement st = connection.createStatement()) { + try (ResultSet rs = st.executeQuery(sql)) { + while (rs.next()) { + String columnName = rs.getString("Field"); + String columnDefault = rs.getString("Default"); + if (columnName != null) { + for (ColumnDescription cd : columnDescriptions) { + if (columnName.equals(cd.getFieldName())) { + cd.setDefaultValue(columnDefault); + break; + } + } + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getDefaultValueSql(String schemaName, String tableName) { + return String.format("desc `%s`.`%s`", schemaName, tableName); + } + + + @Override + public List queryTablePrimaryKeys(Connection connection, String schemaName, + String tableName) { + Set ret = new HashSet<>(); + String sql = String.format("desc `%s`.`%s`", schemaName, tableName); + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + ) { + //看下是否又none的字段,如果有,说明key模式为DUPLICATE KEY 可重复 + boolean NoneExtra = false; + while (rs.next()) { + String field = rs.getString("Field"); + String key = rs.getString("Key"); + String extra = rs.getString("Extra"); + if ("true".equalsIgnoreCase(key)) { + ret.add(field); + } else { + if ("NONE".equalsIgnoreCase(extra)) { + NoneExtra = true; + } + } + } + if (NoneExtra) { + return new ArrayList<>(); + } + return new ArrayList<>(ret); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void setColumnIndexInfo(Connection connection, String schemaName, String tableName, List columnDescriptions) { + } + + @Override + public List getTableColumnCommentDefinition(TableDescription td, + List cds) { + return Collections.emptyList(); + } + +} diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseKafkaImpl.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseKafkaImpl.java new file mode 100644 index 0000000..b513d1b --- /dev/null +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseKafkaImpl.java @@ -0,0 +1,274 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package srt.cloud.framework.dbswitch.core.database.impl; + +import org.apache.commons.lang3.StringUtils; +import srt.cloud.framework.dbswitch.common.constant.Const; +import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; +import srt.cloud.framework.dbswitch.core.database.IDatabaseInterface; +import srt.cloud.framework.dbswitch.core.model.ColumnDescription; +import srt.cloud.framework.dbswitch.core.model.ColumnMetaData; +import srt.cloud.framework.dbswitch.core.model.TableDescription; + +import java.sql.*; +import java.util.*; + +/** + * 支持MySQL数据库的元信息实现 + * + * @author jrl + */ +public class DatabaseKafkaImpl extends DatabaseMysqlImpl implements IDatabaseInterface { + + private static final String SHOW_CREATE_TABLE_SQL = "SHOW CREATE TABLE `%s`.`%s` "; + private static final String SHOW_CREATE_VIEW_SQL = "SHOW CREATE VIEW `%s`.`%s` "; + + public DatabaseKafkaImpl() { + super("com.mysql.jdbc.Driver"); + } + + public DatabaseKafkaImpl(String driverClassName) { + super(driverClassName); + } + + @Override + public ProductTypeEnum getDatabaseType() { + return ProductTypeEnum.DORIS; + } + + @Override + public List queryTableColumnMeta(Connection connection, String schemaName, + String tableName) { +// String sql = this.getTableFieldsQuerySQL(schemaName, tableName); +// List columnDescriptions = this.querySelectSqlColumnMeta(connection, sql); +// // 补充一下字段信息,获取的不准 +// String extraSql = "SELECT column_name,data_type,column_size,decimal_digits,column_comment FROM information_schema.COLUMNS WHERE table_schema='" + schemaName + "' AND table_name='" + tableName + "'"; +// try (PreparedStatement ps = connection.prepareStatement(extraSql); +// ResultSet rs = ps.executeQuery(); +// ) { +// while (rs.next()) { +// String columnName = rs.getString("column_name"); +// String dataType = rs.getString("data_type"); +// String columnSize = rs.getString("column_size"); +// String decimalDigits = rs.getString("decimal_digits"); +// String columnComment = rs.getString("column_comment"); +// if (columnName != null) { +// for (ColumnDescription cd : columnDescriptions) { +// if (columnName.equals(cd.getFieldName())) { +// cd.setFieldTypeName(dataType); +// int csize = columnSize != null ? Integer.parseInt(columnSize) : 0; +// cd.setDisplaySize(csize); +// cd.setPrecisionSize(csize); +// cd.setScaleSize(decimalDigits != null ? Integer.parseInt(decimalDigits) : 0); +// cd.setRemarks(columnComment); +// break; +// } +// } +// } +// } +// } catch (SQLException e) { +// throw new RuntimeException(schemaName + "." + tableName + " queryTableColumnMeta error!!", e); +// } +// +// return columnDescriptions; + return null; + } + + @Override + public String getFieldDefinition(ColumnMetaData v, List pks, boolean useAutoInc, + boolean addCr, boolean withRemarks) { + String fieldname = v.getName(); + int length = v.getLength(); + int precision = v.getPrecision(); + int type = v.getType(); + + String retval = " `" + fieldname + "` "; + boolean canHaveDefaultValue = true; + switch (type) { + case ColumnMetaData.TYPE_TIMESTAMP: + case ColumnMetaData.TYPE_TIME: + retval += "DATETIME"; + break; + case ColumnMetaData.TYPE_DATE: + retval += "DATE"; + break; + case ColumnMetaData.TYPE_BOOLEAN: + retval += "TINYINT"; + break; + case ColumnMetaData.TYPE_NUMBER: + case ColumnMetaData.TYPE_INTEGER: + case ColumnMetaData.TYPE_BIGNUMBER: + if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) { + retval += "BIGINT"; + } else { + // Integer values... + if (precision == 0) { + if (length > 9) { + if (length < 19) { + // can hold signed values between -9223372036854775808 and 9223372036854775807 + // 18 significant digits + retval += "BIGINT"; + } else { + retval += "DECIMAL(" + length + ")"; + } + } else { + retval += "INT"; + } + } else { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + // Floating point values... + /*if (length > 15) { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + } else { + // A double-precision floating-point number is accurate to approximately 15 + // decimal places. + // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html + retval += "DOUBLE"; + }*/ + } + } + break; + case ColumnMetaData.TYPE_STRING: + if (length * 3 <= 65533) { + retval += "VARCHAR(" + length * 3 + ")"; + } else { + retval += "TEXT"; + canHaveDefaultValue = false; + } + break; + default: + retval += "TEXT"; + canHaveDefaultValue = false; + break; + } + + if (!v.isNullable()) { + retval += " NOT NULL"; + } + + if (canHaveDefaultValue && v.getDefaultValue() != null && !"null".equals(v.getDefaultValue()) && !"NULL".equals(v.getDefaultValue())) { + if (type != ColumnMetaData.TYPE_TIMESTAMP && type != ColumnMetaData.TYPE_TIME && type != ColumnMetaData.TYPE_DATE) { + if (v.getDefaultValue().startsWith("'")) { + retval += " DEFAULT " + v.getDefaultValue(); + } else { + retval += " DEFAULT '" + v.getDefaultValue() + "'"; + } + } else { + retval += " DEFAULT CURRENT_TIMESTAMP"; + } + } + + + if (withRemarks && StringUtils.isNotBlank(v.getRemarks())) { + retval += String.format(" COMMENT '%s' ", v.getRemarks().replace("'", "\\'")); + } + + if (addCr) { + retval += Const.CR; + } + + return retval; + } + + @Override + public boolean canCreateIndex(ColumnMetaData v) { + return false; + } + + @Override + public String getPrimaryKeyAsString(List pks) { + if (null != pks && !pks.isEmpty()) { + return "`" + + StringUtils.join(pks, "` , `") + + "`"; + } + + return ""; + } + + @Override + public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List columnDescriptions) { + String sql = this.getDefaultValueSql(schemaName, tableName); + try (Statement st = connection.createStatement()) { + try (ResultSet rs = st.executeQuery(sql)) { + while (rs.next()) { + String columnName = rs.getString("Field"); + String columnDefault = rs.getString("Default"); + if (columnName != null) { + for (ColumnDescription cd : columnDescriptions) { + if (columnName.equals(cd.getFieldName())) { + cd.setDefaultValue(columnDefault); + break; + } + } + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getDefaultValueSql(String schemaName, String tableName) { + return String.format("desc `%s`.`%s`", schemaName, tableName); + } + + + @Override + public List queryTablePrimaryKeys(Connection connection, String schemaName, + String tableName) { + Set ret = new HashSet<>(); + String sql = String.format("desc `%s`.`%s`", schemaName, tableName); + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + ) { + //看下是否又none的字段,如果有,说明key模式为DUPLICATE KEY 可重复 + boolean NoneExtra = false; + while (rs.next()) { + String field = rs.getString("Field"); + String key = rs.getString("Key"); + String extra = rs.getString("Extra"); + if ("true".equalsIgnoreCase(key)) { + ret.add(field); + } else { + if ("NONE".equalsIgnoreCase(extra)) { + NoneExtra = true; + } + } + } + if (NoneExtra) { + return new ArrayList<>(); + } + return new ArrayList<>(ret); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void setColumnIndexInfo(Connection connection, String schemaName, String tableName, List columnDescriptions) { + } + + @Override + public List getTableColumnCommentDefinition(TableDescription td, + List cds) { + return Collections.emptyList(); + } + +} diff --git a/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseRedisImpl.java b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseRedisImpl.java new file mode 100644 index 0000000..8d65bd8 --- /dev/null +++ b/srt-cloud-framework/srt-cloud-dbswitch/src/main/java/srt/cloud/framework/dbswitch/core/database/impl/DatabaseRedisImpl.java @@ -0,0 +1,284 @@ +// Copyright tang. All rights reserved. +// https://gitee.com/inrgihc/dbswitch +// +// Use of this source code is governed by a BSD-style license +// +// Author: tang (inrgihc@126.com) +// Date : 2020/1/2 +// Location: beijing , china +///////////////////////////////////////////////////////////// +package srt.cloud.framework.dbswitch.core.database.impl; + +import org.apache.commons.lang3.StringUtils; +import srt.cloud.framework.dbswitch.common.constant.Const; +import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; +import srt.cloud.framework.dbswitch.core.database.IDatabaseInterface; +import srt.cloud.framework.dbswitch.core.model.ColumnDescription; +import srt.cloud.framework.dbswitch.core.model.ColumnMetaData; +import srt.cloud.framework.dbswitch.core.model.TableDescription; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * 支持MySQL数据库的元信息实现 + * + * @author jrl + */ +public class DatabaseRedisImpl extends DatabaseMysqlImpl implements IDatabaseInterface { + + private static final String SHOW_CREATE_TABLE_SQL = "SHOW CREATE TABLE `%s`.`%s` "; + private static final String SHOW_CREATE_VIEW_SQL = "SHOW CREATE VIEW `%s`.`%s` "; + + public DatabaseRedisImpl() { + super("com.mysql.jdbc.Driver"); + } + + public DatabaseRedisImpl(String driverClassName) { + super(driverClassName); + } + + @Override + public ProductTypeEnum getDatabaseType() { + return ProductTypeEnum.DORIS; + } + + @Override + public List queryTableColumnMeta(Connection connection, String schemaName, + String tableName) { +// String sql = this.getTableFieldsQuerySQL(schemaName, tableName); +// List columnDescriptions = this.querySelectSqlColumnMeta(connection, sql); +// // 补充一下字段信息,获取的不准 +// String extraSql = "SELECT column_name,data_type,column_size,decimal_digits,column_comment FROM information_schema.COLUMNS WHERE table_schema='" + schemaName + "' AND table_name='" + tableName + "'"; +// try (PreparedStatement ps = connection.prepareStatement(extraSql); +// ResultSet rs = ps.executeQuery(); +// ) { +// while (rs.next()) { +// String columnName = rs.getString("column_name"); +// String dataType = rs.getString("data_type"); +// String columnSize = rs.getString("column_size"); +// String decimalDigits = rs.getString("decimal_digits"); +// String columnComment = rs.getString("column_comment"); +// if (columnName != null) { +// for (ColumnDescription cd : columnDescriptions) { +// if (columnName.equals(cd.getFieldName())) { +// cd.setFieldTypeName(dataType); +// int csize = columnSize != null ? Integer.parseInt(columnSize) : 0; +// cd.setDisplaySize(csize); +// cd.setPrecisionSize(csize); +// cd.setScaleSize(decimalDigits != null ? Integer.parseInt(decimalDigits) : 0); +// cd.setRemarks(columnComment); +// break; +// } +// } +// } +// } +// } catch (SQLException e) { +// throw new RuntimeException(schemaName + "." + tableName + " queryTableColumnMeta error!!", e); +// } +// +// return columnDescriptions; + return null; + } + + @Override + public String getFieldDefinition(ColumnMetaData v, List pks, boolean useAutoInc, + boolean addCr, boolean withRemarks) { + String fieldname = v.getName(); + int length = v.getLength(); + int precision = v.getPrecision(); + int type = v.getType(); + + String retval = " `" + fieldname + "` "; + boolean canHaveDefaultValue = true; + switch (type) { + case ColumnMetaData.TYPE_TIMESTAMP: + case ColumnMetaData.TYPE_TIME: + retval += "DATETIME"; + break; + case ColumnMetaData.TYPE_DATE: + retval += "DATE"; + break; + case ColumnMetaData.TYPE_BOOLEAN: + retval += "TINYINT"; + break; + case ColumnMetaData.TYPE_NUMBER: + case ColumnMetaData.TYPE_INTEGER: + case ColumnMetaData.TYPE_BIGNUMBER: + if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) { + retval += "BIGINT"; + } else { + // Integer values... + if (precision == 0) { + if (length > 9) { + if (length < 19) { + // can hold signed values between -9223372036854775808 and 9223372036854775807 + // 18 significant digits + retval += "BIGINT"; + } else { + retval += "DECIMAL(" + length + ")"; + } + } else { + retval += "INT"; + } + } else { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + // Floating point values... + /*if (length > 15) { + retval += "DECIMAL(" + length; + if (precision > 0) { + retval += ", " + precision; + } + retval += ")"; + } else { + // A double-precision floating-point number is accurate to approximately 15 + // decimal places. + // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html + retval += "DOUBLE"; + }*/ + } + } + break; + case ColumnMetaData.TYPE_STRING: + if (length * 3 <= 65533) { + retval += "VARCHAR(" + length * 3 + ")"; + } else { + retval += "TEXT"; + canHaveDefaultValue = false; + } + break; + default: + retval += "TEXT"; + canHaveDefaultValue = false; + break; + } + + if (!v.isNullable()) { + retval += " NOT NULL"; + } + + if (canHaveDefaultValue && v.getDefaultValue() != null && !"null".equals(v.getDefaultValue()) && !"NULL".equals(v.getDefaultValue())) { + if (type != ColumnMetaData.TYPE_TIMESTAMP && type != ColumnMetaData.TYPE_TIME && type != ColumnMetaData.TYPE_DATE) { + if (v.getDefaultValue().startsWith("'")) { + retval += " DEFAULT " + v.getDefaultValue(); + } else { + retval += " DEFAULT '" + v.getDefaultValue() + "'"; + } + } else { + retval += " DEFAULT CURRENT_TIMESTAMP"; + } + } + + + if (withRemarks && StringUtils.isNotBlank(v.getRemarks())) { + retval += String.format(" COMMENT '%s' ", v.getRemarks().replace("'", "\\'")); + } + + if (addCr) { + retval += Const.CR; + } + + return retval; + } + + @Override + public boolean canCreateIndex(ColumnMetaData v) { + return false; + } + + @Override + public String getPrimaryKeyAsString(List pks) { + if (null != pks && !pks.isEmpty()) { + return "`" + + StringUtils.join(pks, "` , `") + + "`"; + } + + return ""; + } + + @Override + public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List columnDescriptions) { + String sql = this.getDefaultValueSql(schemaName, tableName); + try (Statement st = connection.createStatement()) { + try (ResultSet rs = st.executeQuery(sql)) { + while (rs.next()) { + String columnName = rs.getString("Field"); + String columnDefault = rs.getString("Default"); + if (columnName != null) { + for (ColumnDescription cd : columnDescriptions) { + if (columnName.equals(cd.getFieldName())) { + cd.setDefaultValue(columnDefault); + break; + } + } + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String getDefaultValueSql(String schemaName, String tableName) { + return String.format("desc `%s`.`%s`", schemaName, tableName); + } + + + @Override + public List queryTablePrimaryKeys(Connection connection, String schemaName, + String tableName) { + Set ret = new HashSet<>(); + String sql = String.format("desc `%s`.`%s`", schemaName, tableName); + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + ) { + //看下是否又none的字段,如果有,说明key模式为DUPLICATE KEY 可重复 + boolean NoneExtra = false; + while (rs.next()) { + String field = rs.getString("Field"); + String key = rs.getString("Key"); + String extra = rs.getString("Extra"); + if ("true".equalsIgnoreCase(key)) { + ret.add(field); + } else { + if ("NONE".equalsIgnoreCase(extra)) { + NoneExtra = true; + } + } + } + if (NoneExtra) { + return new ArrayList<>(); + } + return new ArrayList<>(ret); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void setColumnIndexInfo(Connection connection, String schemaName, String tableName, List columnDescriptions) { + } + + @Override + public List getTableColumnCommentDefinition(TableDescription td, + List cds) { + return Collections.emptyList(); + } + +} diff --git a/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.14-2.0.0-jar-with-dependencies.jar b/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.14-2.0.0-jar-with-dependencies.jar index 7550800..d963086 100644 Binary files a/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.14-2.0.0-jar-with-dependencies.jar and b/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.14-2.0.0-jar-with-dependencies.jar differ diff --git a/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.16-2.0.0-jar-with-dependencies.jar b/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.16-2.0.0-jar-with-dependencies.jar new file mode 100644 index 0000000..95cf19f Binary files /dev/null and b/srt-cloud-framework/srt-cloud-flink/build/app/flink-app-1.16-2.0.0-jar-with-dependencies.jar differ diff --git a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.14-2.0.0.jar b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.14-2.0.0.jar index 478667b..9df30ff 100644 Binary files a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.14-2.0.0.jar and b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.14-2.0.0.jar differ diff --git a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.16-2.0.0.jar b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.16-2.0.0.jar new file mode 100644 index 0000000..a8ea263 Binary files /dev/null and b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-catalog-mysql-1.16-2.0.0.jar differ diff --git a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.14-2.0.0.jar b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.14-2.0.0.jar index 7019769..f968ee4 100644 Binary files a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.14-2.0.0.jar and b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.14-2.0.0.jar differ diff --git a/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.16-2.0.0.jar b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.16-2.0.0.jar new file mode 100644 index 0000000..829a34d Binary files /dev/null and b/srt-cloud-framework/srt-cloud-flink/build/extends/flink-client-1.16-2.0.0.jar differ diff --git a/srt-cloud-system/src/main/java/net/srt/system/controller/SysAuthController.java b/srt-cloud-system/src/main/java/net/srt/system/controller/SysAuthController.java index 2656f81..0f13a2d 100644 --- a/srt-cloud-system/src/main/java/net/srt/system/controller/SysAuthController.java +++ b/srt-cloud-system/src/main/java/net/srt/system/controller/SysAuthController.java @@ -32,7 +32,6 @@ public class SysAuthController { @Operation(summary = "验证码") public Result captcha() { SysCaptchaVO captchaVO = sysCaptchaService.generate(); - return Result.ok(captchaVO); } diff --git a/srt-cloud-system/src/main/java/net/srt/system/service/impl/IsCaptcha.java b/srt-cloud-system/src/main/java/net/srt/system/service/impl/IsCaptcha.java new file mode 100644 index 0000000..c318803 --- /dev/null +++ b/srt-cloud-system/src/main/java/net/srt/system/service/impl/IsCaptcha.java @@ -0,0 +1,14 @@ +package net.srt.system.service.impl; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "cbx") +@Data +@RefreshScope +public class IsCaptcha { + private Boolean isCaptcha; +} diff --git a/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysAuthServiceImpl.java b/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysAuthServiceImpl.java index 10dea38..be832a6 100644 --- a/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysAuthServiceImpl.java +++ b/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysAuthServiceImpl.java @@ -19,6 +19,7 @@ import net.srt.system.vo.SysAccountLoginVO; import net.srt.system.vo.SysMobileLoginVO; import net.srt.system.vo.SysTokenVO; import net.srt.system.vo.SysUserVO; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -40,17 +41,21 @@ public class SysAuthServiceImpl implements SysAuthService { private final SysLogLoginService sysLogLoginService; private final SysUserService sysUserService; private final SmsApi smsApi; + private IsCaptcha isCaptcha; + @Override public SysTokenVO loginByAccount(SysAccountLoginVO login) { // 验证码效验 -// boolean flag = sysCaptchaService.validate(login.getKey(), login.getCaptcha()); -// if (!flag) { -// // 保存登录日志 -// sysLogLoginService.save(login.getUsername(), Constant.FAIL, LoginOperationEnum.CAPTCHA_FAIL.getValue()); -// -// throw new ServerException("验证码错误"); -// } + if (isCaptcha.getIsCaptcha()) { + boolean flag = sysCaptchaService.validate(login.getKey(), login.getCaptcha()); + if (!flag) { + // 保存登录日志 + sysLogLoginService.save(login.getUsername(), Constant.FAIL, LoginOperationEnum.CAPTCHA_FAIL.getValue()); + + throw new ServerException("验证码错误"); + } + } Authentication authentication; try { diff --git a/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysCaptchaServiceImpl.java b/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysCaptchaServiceImpl.java index 70283b7..bd9dc68 100644 --- a/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysCaptchaServiceImpl.java +++ b/srt-cloud-system/src/main/java/net/srt/system/service/impl/SysCaptchaServiceImpl.java @@ -9,6 +9,7 @@ import net.srt.framework.common.cache.RedisCache; import net.srt.framework.common.cache.RedisKeys; import net.srt.system.service.SysCaptchaService; import net.srt.system.vo.SysCaptchaVO; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** @@ -19,29 +20,35 @@ import org.springframework.stereotype.Service; @Service @AllArgsConstructor public class SysCaptchaServiceImpl implements SysCaptchaService { + private final RedisCache redisCache; + private IsCaptcha isCaptcha; + @Override public SysCaptchaVO generate() { - // 生成验证码key - String key = UUID.randomUUID().toString(); - - // 生成验证码 - SpecCaptcha captcha = new SpecCaptcha(150, 40); - captcha.setLen(5); - captcha.setCharType(Captcha.TYPE_DEFAULT); - String image = captcha.toBase64(); - - // 保存到缓存 - String redisKey = RedisKeys.getCaptchaKey(key); - redisCache.set(redisKey, captcha.text(), 300); - - // 封装返回数据 SysCaptchaVO captchaVO = new SysCaptchaVO(); - captchaVO.setKey(key); - captchaVO.setImage(image); - return captchaVO; + if (isCaptcha.getIsCaptcha()) { + // 生成验证码key + String key = UUID.randomUUID().toString(); + + // 生成验证码 + SpecCaptcha captcha = new SpecCaptcha(150, 40); + captcha.setLen(5); + captcha.setCharType(Captcha.TYPE_DEFAULT); + String image = captcha.toBase64(); + + // 保存到缓存 + String redisKey = RedisKeys.getCaptchaKey(key); + redisCache.set(redisKey, captcha.text(), 300); + + // 封装返回数据 + captchaVO.setKey(key); + captchaVO.setImage(image); + return captchaVO; + } + return null; } @Override diff --git a/srt-cloud-system/src/main/java/net/srt/system/vo/SysAccountLoginVO.java b/srt-cloud-system/src/main/java/net/srt/system/vo/SysAccountLoginVO.java index 6b28446..755bece 100644 --- a/srt-cloud-system/src/main/java/net/srt/system/vo/SysAccountLoginVO.java +++ b/srt-cloud-system/src/main/java/net/srt/system/vo/SysAccountLoginVO.java @@ -26,4 +26,9 @@ public class SysAccountLoginVO implements Serializable { @Schema(description = "验证码") private String captcha; + + @Schema(description = "是否判断验证码") + private boolean isCaptcha; + + } diff --git a/srt-cloud-system/src/main/java/net/srt/system/vo/SysCaptchaVO.java b/srt-cloud-system/src/main/java/net/srt/system/vo/SysCaptchaVO.java index 1c018a0..a9371dd 100644 --- a/srt-cloud-system/src/main/java/net/srt/system/vo/SysCaptchaVO.java +++ b/srt-cloud-system/src/main/java/net/srt/system/vo/SysCaptchaVO.java @@ -2,6 +2,7 @@ package net.srt.system.vo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; +import org.springframework.beans.factory.annotation.Value; import java.io.Serializable; @@ -20,4 +21,7 @@ public class SysCaptchaVO implements Serializable { @Schema(description = "image base64") private String image; + + + } diff --git a/srt-cloud-system/src/main/resources/bootstrap.yml b/srt-cloud-system/src/main/resources/bootstrap.yml index f53d6d0..cf9f2ad 100644 --- a/srt-cloud-system/src/main/resources/bootstrap.yml +++ b/srt-cloud-system/src/main/resources/bootstrap.yml @@ -41,3 +41,6 @@ storage: local: # 本地上传路径 path: D://upload + +cbx: + isCaptcha: false