pull/1/head
chenbingxuan 2023-12-09 11:34:19 +08:00
parent b0f1661735
commit e7314a4dcc
22 changed files with 1470 additions and 48 deletions

View File

@ -134,6 +134,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
@Override @Override
public void testOnline(DataDatabaseVO vo) { public void testOnline(DataDatabaseVO vo) {
ProductTypeEnum productTypeEnum = ProductTypeEnum.getByIndex(vo.getDatabaseType()); ProductTypeEnum productTypeEnum = ProductTypeEnum.getByIndex(vo.getDatabaseType());
IMetaDataByJdbcService metaDataService = new MetaDataByJdbcServiceImpl(productTypeEnum); IMetaDataByJdbcService metaDataService = new MetaDataByJdbcServiceImpl(productTypeEnum);
if (StringUtil.isBlank(vo.getJdbcUrl())) { if (StringUtil.isBlank(vo.getJdbcUrl())) {
vo.setJdbcUrl(productTypeEnum.getUrl() vo.setJdbcUrl(productTypeEnum.getUrl()
@ -141,12 +142,16 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
.replace("{port}", vo.getDatabasePort()) .replace("{port}", vo.getDatabasePort())
.replace("{database}", vo.getDatabaseName())); .replace("{database}", vo.getDatabaseName()));
} }
metaDataService.testQuerySQL( if (vo.getIsJdbc()==1){
vo.getJdbcUrl(), metaDataService.testQuerySQL(
vo.getUserName(), vo.getJdbcUrl(),
vo.getPassword(), vo.getUserName(),
productTypeEnum.getTestSql() vo.getPassword(),
); productTypeEnum.getTestSql()
);
}else {
//TODO 连接
}
if (vo.getId() != null) { if (vo.getId() != null) {
//更新状态 //更新状态
baseMapper.changeStatusById(vo.getId(), YesOrNo.YES.getValue()); baseMapper.changeStatusById(vo.getId(), YesOrNo.YES.getValue());
@ -275,7 +280,7 @@ public class DataDatabaseServiceImpl extends BaseServiceImpl<DataDatabaseDao, Da
entity.setJdbcUrl(project.getDbUrl()); entity.setJdbcUrl(project.getDbUrl());
entity.setUserName(project.getDbUsername()); entity.setUserName(project.getDbUsername());
entity.setPassword(project.getDbPassword()); entity.setPassword(project.getDbPassword());
entity.setName(project.getName() + "<中台库>"); entity.setName(project.getName() + "<>");
List<TreeNodeVo> nodeList = new ArrayList<>(1); List<TreeNodeVo> nodeList = new ArrayList<>(1);
TreeNodeVo dbNode = new TreeNodeVo(); TreeNodeVo dbNode = new TreeNodeVo();
nodeList.add(dbNode); nodeList.add(dbNode);

View File

@ -78,5 +78,8 @@ public class DataDatabaseVO implements Serializable {
@JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN) @JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN)
private Date updateTime; private Date updateTime;
@Schema(description = "是否是关系型数据库")
private Integer isJdbc;
} }

View File

@ -100,6 +100,12 @@ public enum ProductTypeEnum {
* MySQL * 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"), 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; private Integer index;

View File

@ -10,22 +10,7 @@
package srt.cloud.framework.dbswitch.core.database; package srt.cloud.framework.dbswitch.core.database;
import srt.cloud.framework.dbswitch.common.type.ProductTypeEnum; 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.*;
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 java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -60,6 +45,12 @@ public final class DatabaseFactory {
put(ProductTypeEnum.HIVE, DatabaseHiveImpl::new); put(ProductTypeEnum.HIVE, DatabaseHiveImpl::new);
put(ProductTypeEnum.SQLITE3, DatabaseSqliteImpl::new); put(ProductTypeEnum.SQLITE3, DatabaseSqliteImpl::new);
put(ProductTypeEnum.DORIS, DatabaseDorisImpl::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);
} }
}; };

View File

@ -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<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
// String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
// List<ColumnDescription> 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<String> 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<String> pks) {
if (null != pks && !pks.isEmpty()) {
return "`" +
StringUtils.join(pks, "` , `") +
"`";
}
return "";
}
@Override
public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List<ColumnDescription> 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<String> queryTablePrimaryKeys(Connection connection, String schemaName,
String tableName) {
Set<String> 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<ColumnDescription> columnDescriptions) {
}
@Override
public List<String> getTableColumnCommentDefinition(TableDescription td,
List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@ -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<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
// String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
// List<ColumnDescription> 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<String> 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<String> pks) {
if (null != pks && !pks.isEmpty()) {
return "`" +
StringUtils.join(pks, "` , `") +
"`";
}
return "";
}
@Override
public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List<ColumnDescription> 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<String> queryTablePrimaryKeys(Connection connection, String schemaName,
String tableName) {
Set<String> 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<ColumnDescription> columnDescriptions) {
}
@Override
public List<String> getTableColumnCommentDefinition(TableDescription td,
List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@ -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<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
// String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
// List<ColumnDescription> 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<String> 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<String> pks) {
if (null != pks && !pks.isEmpty()) {
return "`" +
StringUtils.join(pks, "` , `") +
"`";
}
return "";
}
@Override
public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List<ColumnDescription> 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<String> queryTablePrimaryKeys(Connection connection, String schemaName,
String tableName) {
Set<String> 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<ColumnDescription> columnDescriptions) {
}
@Override
public List<String> getTableColumnCommentDefinition(TableDescription td,
List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@ -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<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
// String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
// List<ColumnDescription> 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<String> 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<String> pks) {
if (null != pks && !pks.isEmpty()) {
return "`" +
StringUtils.join(pks, "` , `") +
"`";
}
return "";
}
@Override
public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List<ColumnDescription> 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<String> queryTablePrimaryKeys(Connection connection, String schemaName,
String tableName) {
Set<String> 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<ColumnDescription> columnDescriptions) {
}
@Override
public List<String> getTableColumnCommentDefinition(TableDescription td,
List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@ -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<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
// String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
// List<ColumnDescription> 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<String> 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<String> pks) {
if (null != pks && !pks.isEmpty()) {
return "`" +
StringUtils.join(pks, "` , `") +
"`";
}
return "";
}
@Override
public void setColumnDefaultValue(Connection connection, String schemaName, String tableName, List<ColumnDescription> 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<String> queryTablePrimaryKeys(Connection connection, String schemaName,
String tableName) {
Set<String> 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<ColumnDescription> columnDescriptions) {
}
@Override
public List<String> getTableColumnCommentDefinition(TableDescription td,
List<ColumnDescription> cds) {
return Collections.emptyList();
}
}

View File

@ -32,7 +32,6 @@ public class SysAuthController {
@Operation(summary = "验证码") @Operation(summary = "验证码")
public Result<SysCaptchaVO> captcha() { public Result<SysCaptchaVO> captcha() {
SysCaptchaVO captchaVO = sysCaptchaService.generate(); SysCaptchaVO captchaVO = sysCaptchaService.generate();
return Result.ok(captchaVO); return Result.ok(captchaVO);
} }

View File

@ -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;
}

View File

@ -19,6 +19,7 @@ import net.srt.system.vo.SysAccountLoginVO;
import net.srt.system.vo.SysMobileLoginVO; import net.srt.system.vo.SysMobileLoginVO;
import net.srt.system.vo.SysTokenVO; import net.srt.system.vo.SysTokenVO;
import net.srt.system.vo.SysUserVO; import net.srt.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -40,17 +41,21 @@ public class SysAuthServiceImpl implements SysAuthService {
private final SysLogLoginService sysLogLoginService; private final SysLogLoginService sysLogLoginService;
private final SysUserService sysUserService; private final SysUserService sysUserService;
private final SmsApi smsApi; private final SmsApi smsApi;
private IsCaptcha isCaptcha;
@Override @Override
public SysTokenVO loginByAccount(SysAccountLoginVO login) { public SysTokenVO loginByAccount(SysAccountLoginVO login) {
// 验证码效验 // 验证码效验
// boolean flag = sysCaptchaService.validate(login.getKey(), login.getCaptcha()); if (isCaptcha.getIsCaptcha()) {
// if (!flag) { boolean flag = sysCaptchaService.validate(login.getKey(), login.getCaptcha());
// // 保存登录日志 if (!flag) {
// sysLogLoginService.save(login.getUsername(), Constant.FAIL, LoginOperationEnum.CAPTCHA_FAIL.getValue()); // 保存登录日志
// sysLogLoginService.save(login.getUsername(), Constant.FAIL, LoginOperationEnum.CAPTCHA_FAIL.getValue());
// throw new ServerException("验证码错误");
// } throw new ServerException("验证码错误");
}
}
Authentication authentication; Authentication authentication;
try { try {

View File

@ -9,6 +9,7 @@ import net.srt.framework.common.cache.RedisCache;
import net.srt.framework.common.cache.RedisKeys; import net.srt.framework.common.cache.RedisKeys;
import net.srt.system.service.SysCaptchaService; import net.srt.system.service.SysCaptchaService;
import net.srt.system.vo.SysCaptchaVO; import net.srt.system.vo.SysCaptchaVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@ -19,29 +20,35 @@ import org.springframework.stereotype.Service;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
public class SysCaptchaServiceImpl implements SysCaptchaService { public class SysCaptchaServiceImpl implements SysCaptchaService {
private final RedisCache redisCache; private final RedisCache redisCache;
private IsCaptcha isCaptcha;
@Override @Override
public SysCaptchaVO generate() { 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(); 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 @Override

View File

@ -26,4 +26,9 @@ public class SysAccountLoginVO implements Serializable {
@Schema(description = "验证码") @Schema(description = "验证码")
private String captcha; private String captcha;
@Schema(description = "是否判断验证码")
private boolean isCaptcha;
} }

View File

@ -2,6 +2,7 @@ package net.srt.system.vo;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import java.io.Serializable; import java.io.Serializable;
@ -20,4 +21,7 @@ public class SysCaptchaVO implements Serializable {
@Schema(description = "image base64") @Schema(description = "image base64")
private String image; private String image;
} }

View File

@ -41,3 +41,6 @@ storage:
local: local:
# 本地上传路径 # 本地上传路径
path: D://upload path: D://upload
cbx:
isCaptcha: false