Skip to content

Commit

Permalink
♻️ 重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
leshalv committed Aug 26, 2020
1 parent 1995f9c commit 96734a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineFileType;
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.metadata.Table;
import cn.smallbun.screw.core.metadata.model.ColumnModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package cn.smallbun.screw.core.process;

import cn.smallbun.screw.core.Configuration;
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 cn.smallbun.screw.core.metadata.model.ColumnModel;
import cn.smallbun.screw.core.metadata.model.DataModel;
import cn.smallbun.screw.core.metadata.model.TableModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public List<? extends Table> getTables() throws QueryException {
try {
//查询
resultSet = getMetaData().getTables(getCatalog(), getSchema(), null,
new String[]{"TABLE"});
new String[] { "TABLE" });
//映射
return Mapping.convertList(resultSet, CacheDbTableModel.class);
} catch (SQLException e) {
Expand All @@ -111,18 +111,21 @@ public List<? extends Column> getTableColumns(String table) throws QueryExceptio
//查询
resultSet = getMetaData().getColumns(getCatalog(), getSchema(), table, PERCENT_SIGN);
//映射
final List<CacheDbColumnModel> list = Mapping.convertList(resultSet, CacheDbColumnModel.class);
final List<CacheDbColumnModel> list = Mapping.convertList(resultSet,
CacheDbColumnModel.class);
//这里处理是为了如果是查询全部列呢?所以处理并获取唯一表名
List<String> tableNames = list.stream().map(CacheDbColumnModel::getTableName)
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
.collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(columnsCaching)) {
//查询全部
if (table.equals(PERCENT_SIGN)) {
//获取全部表列信息SQL
String sql = MessageFormat.format("select TABLE_NAME as \"TABLE_NAME\",COLUMN_NAME as \"COLUMN_NAME\"," +
"case when CHARACTER_MAXIMUM_LENGTH is null then DATA_TYPE || '''' " +
"else DATA_TYPE || ''(''||CHARACTER_MAXIMUM_LENGTH ||'')'' end as \"COLUMN_TYPE\" " +
"from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ''{0}''",
String sql = MessageFormat
.format(
"select TABLE_NAME as \"TABLE_NAME\",COLUMN_NAME as \"COLUMN_NAME\","
+ "case when CHARACTER_MAXIMUM_LENGTH is null then DATA_TYPE || '''' "
+ "else DATA_TYPE || ''(''||CHARACTER_MAXIMUM_LENGTH ||'')'' end as \"COLUMN_TYPE\" "
+ "from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ''{0}''",
getSchema());
PreparedStatement statement = prepareStatement(sql);
resultSet = statement.executeQuery();
Expand All @@ -134,19 +137,21 @@ public List<? extends Column> getTableColumns(String table) throws QueryExceptio
//单表查询
else {
//获取表列信息SQL 查询表名、列名、说明、数据类型
String sql = MessageFormat.format("select TABLE_NAME as \"TABLE_NAME\",COLUMN_NAME as \"COLUMN_NAME\"," +
"case when CHARACTER_MAXIMUM_LENGTH is null then DATA_TYPE || ''''" +
"else DATA_TYPE || ''(''||CHARACTER_MAXIMUM_LENGTH ||'')'' end as \"COLUMN_TYPE\" " +
"from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ''{0}'' and TABLE_NAME = " +
"''{1}''",
String sql = MessageFormat
.format(
"select TABLE_NAME as \"TABLE_NAME\",COLUMN_NAME as \"COLUMN_NAME\","
+ "case when CHARACTER_MAXIMUM_LENGTH is null then DATA_TYPE || ''''"
+ "else DATA_TYPE || ''(''||CHARACTER_MAXIMUM_LENGTH ||'')'' end as \"COLUMN_TYPE\" "
+ "from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ''{0}'' and TABLE_NAME = "
+ "''{1}''",
getSchema(), table);
resultSet = prepareStatement(sql).executeQuery();
}
List<CacheDbColumnModel> inquires = Mapping.convertList(resultSet,
CacheDbColumnModel.class);
CacheDbColumnModel.class);
//处理列,表名为key,列名为值
tableNames.forEach(name -> columnsCaching.put(name, inquires.stream()
.filter(i -> i.getTableName().equals(name)).collect(Collectors.toList())));
.filter(i -> i.getTableName().equals(name)).collect(Collectors.toList())));
}
//处理备注信息
list.forEach(i -> {
Expand All @@ -155,7 +160,7 @@ public List<? extends Column> getTableColumns(String table) throws QueryExceptio
columns.forEach(j -> {
//列名表名一致
if (i.getColumnName().equals(j.getColumnName())
&& i.getTableName().equals(j.getTableName())) {
&& i.getTableName().equals(j.getTableName())) {
//放入列类型
i.setColumnType(j.getColumnType());
i.setColumnLength(j.getColumnLength());
Expand Down Expand Up @@ -218,7 +223,7 @@ public List<? extends PrimaryKey> getPrimaryKeys() throws QueryException {
String sql = "select TABLE_CATALOG ,TABLE_NAME as \"TABLE_NAME\",TABLE_SCHEMA as \"TABLE_SCHEM\",COLUMN_NAME as \"COLUMN_NAME\",ORDINAL_POSITION as \"KEY_SEQ\" from INFORMATION_SCHEMA.COLUMNS where PRIMARY_KEY='YES' and TABLE_SCHEMA='%s'";
// 拼接参数
resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase()))
.executeQuery();
.executeQuery();
return Mapping.convertList(resultSet, CacheDbPrimaryKeyModel.class);
} catch (SQLException e) {
throw new QueryException(e);
Expand Down

0 comments on commit 96734a7

Please sign in to comment.