♻️ 重构代码

This commit is contained in:
SanLi
2020-08-26 10:56:42 +08:00
parent 4674634949
commit 5f8dfe869d
13 changed files with 167 additions and 46 deletions

View File

@@ -19,7 +19,6 @@ package cn.smallbun.screw.core.query;
import cn.smallbun.screw.core.exception.QueryException;
import cn.smallbun.screw.core.metadata.Column;
import cn.smallbun.screw.core.metadata.ColumnLength;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;

View File

@@ -18,7 +18,10 @@
package cn.smallbun.screw.core.query;
import cn.smallbun.screw.core.exception.QueryException;
import cn.smallbun.screw.core.metadata.*;
import cn.smallbun.screw.core.metadata.Column;
import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.metadata.Table;
import java.io.Serializable;
import java.util.List;

View File

@@ -24,7 +24,10 @@ import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.metadata.Table;
import cn.smallbun.screw.core.query.AbstractDatabaseQuery;
import cn.smallbun.screw.core.query.cachedb.model.*;
import cn.smallbun.screw.core.query.cachedb.model.CacheDbColumnModel;
import cn.smallbun.screw.core.query.cachedb.model.CacheDbDatabaseModel;
import cn.smallbun.screw.core.query.cachedb.model.CacheDbPrimaryKeyModel;
import cn.smallbun.screw.core.query.cachedb.model.CacheDbTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
@@ -32,7 +35,6 @@ import cn.smallbun.screw.core.util.JdbcUtils;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;

View File

@@ -152,5 +152,19 @@ public class CacheDbColumnModel implements Column {
*/
@MappingField(value = "COLUMN_SIZE")
private String columnSize;
/**
* 是否主键
*/
private String primaryKey;
/**
* 列类型(带长度)
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}

View File

@@ -28,6 +28,7 @@ import cn.smallbun.screw.core.query.mariadb.model.MariadbDatabaseModel;
import cn.smallbun.screw.core.query.mariadb.model.MariadbPrimaryKeyModel;
import cn.smallbun.screw.core.query.mariadb.model.MariadbTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.CollectionUtils;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
@@ -36,6 +37,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;
@@ -94,6 +96,7 @@ public class MariaDbDataBaseQuery extends AbstractDatabaseQuery {
* @param table {@link String} 表名
* @return {@link List} 表字段信息
*/
@SuppressWarnings("DuplicatedCode")
@Override
public List<MariadbColumnModel> getTableColumns(String table) throws QueryException {
Assert.notEmpty(table, "Table name can not be empty!");
@@ -104,20 +107,48 @@ public class MariaDbDataBaseQuery extends AbstractDatabaseQuery {
//映射
List<MariadbColumnModel> list = Mapping.convertList(resultSet,
MariadbColumnModel.class);
//通过SQL获取具体的列类型带长度
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' and A.TABLE_NAME='%s' ORDER BY A.COLUMN_NAME";
PreparedStatement statement = prepareStatement(
String.format(sql, getDataBase(), table));
resultSet = statement.executeQuery();
List<MariadbColumnModel> columns = Mapping.convertList(resultSet,
MariadbColumnModel.class);
for (MariadbColumnModel model : list) {
for (MariadbColumnModel column : columns) {
if (model.getColumnName().equals(column.getColumnName())) {
model.setColumnType(column.getColumnType());
//这里处理是为了如果是查询全部列呢?所以处理并获取唯一表名
List<String> tableNames = list.stream().map(MariadbColumnModel::getTableName)
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(columnsCaching)) {
//查询全部
if (table.equals(PERCENT_SIGN)) {
//获取全部表列信息SQL
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE, replace(substring(a.COLUMN_TYPE, LOCATE('(', a.COLUMN_TYPE) + 1), ')', '') COLUMN_LENGTH FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' ORDER BY A.COLUMN_NAME";
PreparedStatement statement = prepareStatement(
String.format(sql, getDataBase()));
resultSet = statement.executeQuery();
int fetchSize = 4284;
if (resultSet.getFetchSize() < fetchSize) {
resultSet.setFetchSize(fetchSize);
}
}
//单表查询
else {
//获取表列信息SQL 查询表名、列名、说明、数据类型
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE, replace(substring(a.COLUMN_TYPE, LOCATE('(', a.COLUMN_TYPE) + 1), ')', '') COLUMN_LENGTH FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' and A.TABLE_NAME = '%s' ORDER BY A.COLUMN_NAME";
resultSet = prepareStatement(String.format(sql, getDataBase(), table))
.executeQuery();
}
List<MariadbColumnModel> inquires = Mapping.convertList(resultSet,
MariadbColumnModel.class);
//处理列表名为key列名为值
tableNames.forEach(name -> columnsCaching.put(name, inquires.stream()
.filter(i -> i.getTableName().equals(name)).collect(Collectors.toList())));
}
//处理备注信息
list.forEach(i -> {
//从缓存中根据表名获取列信息
List<Column> columns = columnsCaching.get(i.getTableName());
columns.forEach(j -> {
//列名表名一致
if (i.getColumnName().equals(j.getColumnName())
&& i.getTableName().equals(j.getTableName())) {
//放入列类型
i.setColumnType(j.getColumnType());
}
});
});
return list;
} catch (SQLException e) {
throw ExceptionUtils.mpe(e);

View File

@@ -167,4 +167,10 @@ public class MariadbColumnModel implements Column {
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}

View File

@@ -28,6 +28,7 @@ import cn.smallbun.screw.core.query.mysql.model.MySqlDatabaseModel;
import cn.smallbun.screw.core.query.mysql.model.MySqlPrimaryKeyModel;
import cn.smallbun.screw.core.query.mysql.model.MySqlTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.CollectionUtils;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
@@ -35,8 +36,8 @@ import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;
@@ -101,6 +102,7 @@ public class MySqlDataBaseQuery extends AbstractDatabaseQuery {
* @param table {@link String} 表名
* @return {@link List} 表字段信息
*/
@SuppressWarnings("DuplicatedCode")
@Override
public List<MySqlColumnModel> getTableColumns(String table) throws QueryException {
Assert.notEmpty(table, "Table name can not be empty!");
@@ -110,19 +112,48 @@ public class MySqlDataBaseQuery extends AbstractDatabaseQuery {
resultSet = getMetaData().getColumns(getCatalog(), getSchema(), table, PERCENT_SIGN);
//映射
List<MySqlColumnModel> list = Mapping.convertList(resultSet, MySqlColumnModel.class);
//通过SQL获取具体的列类型带长度
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' and A.TABLE_NAME='%s' ORDER BY A.COLUMN_NAME";
PreparedStatement statement = prepareStatement(
String.format(sql, getDataBase(), table));
resultSet = statement.executeQuery();
List<MySqlColumnModel> columns = Mapping.convertList(resultSet, MySqlColumnModel.class);
for (MySqlColumnModel model : list) {
for (MySqlColumnModel column : columns) {
if (model.getColumnName().equals(column.getColumnName())) {
model.setColumnType(column.getColumnType());
//这里处理是为了如果是查询全部列呢?所以处理并获取唯一表名
List<String> tableNames = list.stream().map(MySqlColumnModel::getTableName)
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(columnsCaching)) {
//查询全部
if (table.equals(PERCENT_SIGN)) {
//获取全部表列信息SQL
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE, replace(substring(a.COLUMN_TYPE, LOCATE('(', a.COLUMN_TYPE) + 1), ')', '') COLUMN_LENGTH FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' ORDER BY A.COLUMN_NAME";
PreparedStatement statement = prepareStatement(
String.format(sql, getDataBase()));
resultSet = statement.executeQuery();
int fetchSize = 4284;
if (resultSet.getFetchSize() < fetchSize) {
resultSet.setFetchSize(fetchSize);
}
}
//单表查询
else {
//获取表列信息SQL 查询表名、列名、说明、数据类型
String sql = "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_TYPE, replace(substring(a.COLUMN_TYPE, LOCATE('(', a.COLUMN_TYPE) + 1), ')', '') COLUMN_LENGTH FROM INFORMATION_SCHEMA.COLUMNS A WHERE A.TABLE_SCHEMA = '%s' and A.TABLE_NAME = '%s' ORDER BY A.COLUMN_NAME";
resultSet = prepareStatement(String.format(sql, getDataBase(), table))
.executeQuery();
}
List<MySqlColumnModel> inquires = Mapping.convertList(resultSet,
MySqlColumnModel.class);
//处理列表名为key列名为值
tableNames.forEach(name -> columnsCaching.put(name, inquires.stream()
.filter(i -> i.getTableName().equals(name)).collect(Collectors.toList())));
}
//处理备注信息
list.forEach(i -> {
//从缓存中根据表名获取列信息
List<Column> columns = columnsCaching.get(i.getTableName());
columns.forEach(j -> {
//列名表名一致
if (i.getColumnName().equals(j.getColumnName())
&& i.getTableName().equals(j.getTableName())) {
//放入列类型
i.setColumnType(j.getColumnType());
}
});
});
return list;
} catch (SQLException e) {
throw ExceptionUtils.mpe(e);

View File

@@ -23,7 +23,10 @@ import cn.smallbun.screw.core.metadata.Column;
import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.query.AbstractDatabaseQuery;
import cn.smallbun.screw.core.query.oracle.model.*;
import cn.smallbun.screw.core.query.oracle.model.OracleColumnModel;
import cn.smallbun.screw.core.query.oracle.model.OracleDatabaseModel;
import cn.smallbun.screw.core.query.oracle.model.OraclePrimaryKeyModel;
import cn.smallbun.screw.core.query.oracle.model.OracleTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.CollectionUtils;
import cn.smallbun.screw.core.util.ExceptionUtils;
@@ -33,10 +36,7 @@ import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;
@@ -141,12 +141,12 @@ public class OracleDataBaseQuery extends AbstractDatabaseQuery {
if (CollectionUtils.isEmpty(columnsCaching)) {
//查询全部
if (table.equals(PERCENT_SIGN)) {
String sql = "SELECT TABLE_NAME, COLUMN_NAME, COMMENTS AS REMARKS FROM USER_COL_COMMENTS";
String sql = "SELECT ut.TABLE_NAME, ut.COLUMN_NAME,--字段名称 uc.comments AS REMARKS,--字段注释 ut.DATA_TYPE AS COLUMN_TYPE,--字典类型 ut.DATA_LENGTH AS COLUMN_LENGTH--字典长度 FROM user_tab_columns ut INNER JOIN user_col_comments uc ON ut.TABLE_NAME = uc.table_name AND ut.COLUMN_NAME = uc.column_name";
if (isDda()) {
sql = "SELECT TABLE_NAME, COLUMN_NAME, COMMENTS AS REMARKS FROM DBA_COL_COMMENTS WHERE OWNER = '"
+ getSchema() + "'";
sql = "SELECT ut.TABLE_NAME, -- 表名 ut.COLUMN_NAME,--字段名称 uc.comments AS REMARKS,--字段注释 ut.DATA_TYPE AS COLUMN_TYPE,--字典类型 ut.DATA_LENGTH AS COLUMN_LENGTH--字典长度 FROM dba_tab_columns ut INNER JOIN dba_col_comments uc ON ut.TABLE_NAME = uc.table_name AND ut.COLUMN_NAME = uc.column_name AND ut.OWNER = uc.OWNER WHERE ut.OWNER='%s'";
}
PreparedStatement statement = prepareStatement(sql);
PreparedStatement statement = prepareStatement(
String.format(sql, getDataBase()));
resultSet = statement.executeQuery();
int fetchSize = 4284;
if (resultSet.getFetchSize() < fetchSize) {
@@ -155,12 +155,12 @@ public class OracleDataBaseQuery extends AbstractDatabaseQuery {
}
//单表查询
else {
String sql = "SELECT TABLE_NAME, COLUMN_NAME, COMMENTS AS REMARKS FROM USER_COL_COMMENTS WHERE TABLE_NAME = '%s'";
String sql = "SELECT ut.TABLE_NAME, ut.COLUMN_NAME,--字段名称 uc.comments AS REMARKS,--字段注释 ut.DATA_TYPE AS COLUMN_TYPE,--字典类型 ut.DATA_LENGTH AS COLUMN_LENGTH--字典长度 FROM user_tab_columns ut INNER JOIN user_col_comments uc ON ut.TABLE_NAME = uc.table_name AND ut.COLUMN_NAME = uc.column_name WHERE ut.Table_Name = '%s'";
if (isDda()) {
sql = "SELECT TABLE_NAME, COLUMN_NAME, COMMENTS AS REMARKS FROM DBA_COL_COMMENTS WHERE TABLE_NAME = '%s' AND OWNER = '"
+ getSchema() + "'";
sql = "SELECT ut.TABLE_NAME, -- 表名 ut.COLUMN_NAME,--字段名称 uc.comments AS REMARKS,--字段注释 ut.DATA_TYPE AS COLUMN_TYPE,--字典类型 ut.DATA_LENGTH AS COLUMN_LENGTH--字典长度 FROM dba_tab_columns ut INNER JOIN dba_col_comments uc ON ut.TABLE_NAME = uc.table_name AND ut.COLUMN_NAME = uc.column_name AND ut.OWNER = uc.OWNER WHERE ut.Table_Name = '%s' and ut.OWNER='%s'";
}
resultSet = prepareStatement(String.format(sql, table)).executeQuery();
resultSet = prepareStatement(String.format(sql, table, getDataBase()))
.executeQuery();
}
List<OracleColumnModel> inquires = Mapping.convertList(resultSet,
OracleColumnModel.class);

View File

@@ -161,4 +161,15 @@ public class OracleColumnModel implements Column {
* 是否主键
*/
private String primaryKey;
/**
* 列类型(带长度)
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}

View File

@@ -23,7 +23,10 @@ import cn.smallbun.screw.core.metadata.Column;
import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.query.AbstractDatabaseQuery;
import cn.smallbun.screw.core.query.postgresql.model.*;
import cn.smallbun.screw.core.query.postgresql.model.PostgreSqlColumnModel;
import cn.smallbun.screw.core.query.postgresql.model.PostgreSqlDatabaseModel;
import cn.smallbun.screw.core.query.postgresql.model.PostgreSqlPrimaryKeyModel;
import cn.smallbun.screw.core.query.postgresql.model.PostgreSqlTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
@@ -31,7 +34,6 @@ import cn.smallbun.screw.core.util.JdbcUtils;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;

View File

@@ -160,4 +160,15 @@ public class PostgreSqlColumnModel implements Column {
* 是否主键
*/
private String primaryKey;
/**
* 列类型(带长度)
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}

View File

@@ -23,7 +23,10 @@ import cn.smallbun.screw.core.metadata.Column;
import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.query.AbstractDatabaseQuery;
import cn.smallbun.screw.core.query.sqlservice.model.*;
import cn.smallbun.screw.core.query.sqlservice.model.SqlServerColumnModel;
import cn.smallbun.screw.core.query.sqlservice.model.SqlServerDatabaseModel;
import cn.smallbun.screw.core.query.sqlservice.model.SqlServerPrimaryKeyModel;
import cn.smallbun.screw.core.query.sqlservice.model.SqlServerTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.CollectionUtils;
import cn.smallbun.screw.core.util.ExceptionUtils;
@@ -33,10 +36,7 @@ import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;
@@ -135,7 +135,7 @@ public class SqlServerDataBaseQuery extends AbstractDatabaseQuery {
//查询全部
if (table.equals(PERCENT_SIGN)) {
//获取全部表列信息SQL
String sql = "SELECT cast(a.name AS VARCHAR(500)) AS TABLE_NAME, cast(b.name AS VARCHAR(500)) AS COLUMN_NAME, cast(c.VALUE AS NVARCHAR(500)) AS REMARKS, cast(sys.types.name AS VARCHAR(500)) AS DATA_TYPE FROM (select name, object_id from sys.tables UNION all select name, object_id from sys.views) a INNER JOIN sys.columns b ON b.object_id = a.object_id LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id";
String sql = "SELECT cast(a.name AS VARCHAR(500)) AS TABLE_NAME, cast(b.name AS VARCHAR(500)) AS COLUMN_NAME, cast(c.VALUE AS NVARCHAR(500)) AS REMARKS, cast(sys.types.name AS VARCHAR(500)) + '(' + cast(b.max_length AS NVARCHAR(500)) + ')' AS DATA_TYPE, cast(b.max_length AS NVARCHAR(500)) AS COLUMN_LENGTH FROM(SELECT name, object_id FROM sys.tables UNION allSELECT name, object_id FROM sys.views) a INNER JOIN sys.columns b ON b.object_id = a.object_id LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id";
PreparedStatement statement = prepareStatement(sql);
resultSet = statement.executeQuery();
int fetchSize = 4284;
@@ -146,7 +146,7 @@ public class SqlServerDataBaseQuery extends AbstractDatabaseQuery {
//单表查询
else {
//获取表列信息SQL 查询表名、列名、说明、数据类型
String sql = "SELECT cast(a.name AS VARCHAR(500)) AS TABLE_NAME, cast(b.name AS VARCHAR(500)) AS COLUMN_NAME, cast(c.VALUE AS NVARCHAR(500)) AS REMARKS, cast(sys.types.name AS VARCHAR(500)) AS DATA_TYPE FROM (select name, object_id from sys.tables UNION all select name, object_id from sys.views) a INNER JOIN sys.columns b ON b.object_id = a.object_id LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id where a.name='%s'";
String sql = "SELECT cast(a.name AS VARCHAR(500)) AS TABLE_NAME, cast(b.name AS VARCHAR(500)) AS COLUMN_NAME, cast(c.VALUE AS NVARCHAR(500)) AS REMARKS, cast(sys.types.name AS VARCHAR(500)) + '(' + cast(b.max_length AS NVARCHAR(500)) + ')' AS DATA_TYPE, cast(b.max_length AS NVARCHAR(500)) AS COLUMN_LENGTH FROM(SELECT name, object_id FROM sys.tables UNION allSELECT name, object_id FROM sys.views) a INNER JOIN sys.columns b ON b.object_id = a.object_id LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id WHERE a.name = '%s'";
resultSet = prepareStatement(String.format(sql, table)).executeQuery();
}
List<SqlServerColumnModel> inquires = Mapping.convertList(resultSet,

View File

@@ -161,4 +161,15 @@ public class SqlServerColumnModel implements Column {
* 是否主键
*/
private String primaryKey;
/**
* 列类型(带长度)
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}