|
|
@ -2,19 +2,19 @@ package space.caoshd.otone.builder;
|
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import space.caoshd.otone.aware.ColumnCreateAware;
|
|
|
|
|
|
|
|
import space.caoshd.otone.aware.TableCreateAware;
|
|
|
|
import space.caoshd.otone.entity.ColumnInfo;
|
|
|
|
import space.caoshd.otone.entity.ColumnInfo;
|
|
|
|
import space.caoshd.otone.entity.IndexInfo;
|
|
|
|
import space.caoshd.otone.entity.IndexInfo;
|
|
|
|
import space.caoshd.otone.entity.TableInfo;
|
|
|
|
import space.caoshd.otone.entity.TableInfo;
|
|
|
|
import space.caoshd.otone.aware.ColumnCreateAware;
|
|
|
|
|
|
|
|
import space.caoshd.otone.aware.TableCreateAware;
|
|
|
|
|
|
|
|
import space.caoshd.otone.tool.PropTools;
|
|
|
|
import space.caoshd.otone.tool.PropTools;
|
|
|
|
import space.caoshd.otone.util.CfgConsts;
|
|
|
|
import space.caoshd.otone.util.CfgConsts;
|
|
|
|
import space.caoshd.otone.util.TypeUtils;
|
|
|
|
|
|
|
|
import space.caoshd.otone.util.DBUtils;
|
|
|
|
import space.caoshd.otone.util.DBUtils;
|
|
|
|
import space.caoshd.otone.util.JsonUtils;
|
|
|
|
import space.caoshd.otone.util.JsonUtils;
|
|
|
|
import space.caoshd.otone.util.PathConsts;
|
|
|
|
import space.caoshd.otone.util.PathConsts;
|
|
|
|
import space.caoshd.otone.util.SqlConsts;
|
|
|
|
import space.caoshd.otone.util.SqlConsts;
|
|
|
|
import space.caoshd.otone.util.StrUtils;
|
|
|
|
import space.caoshd.otone.util.StrUtils;
|
|
|
|
|
|
|
|
import space.caoshd.otone.util.TypeUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
@ -29,20 +29,18 @@ public class TableBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
|
|
|
|
|
|
|
|
|
|
|
|
private static final PropTools sqlProperties = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
|
|
|
|
private static final PropTools MYSQL_CONFIG = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
|
|
|
|
|
|
|
|
|
|
|
|
private static final PropTools configProperties =
|
|
|
|
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
|
|
|
|
new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final List<TableCreateAware> tableCreateAwares = new ArrayList<>();
|
|
|
|
private final List<TableCreateAware> tableCreateAwares = new ArrayList<>();
|
|
|
|
|
|
|
|
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
public TableBuilder addTableAddonAble(TableCreateAware tableCreateAware) {
|
|
|
|
public TableBuilder addTableAddonAble(TableCreateAware tableCreateAware) {
|
|
|
|
this.tableCreateAwares.add(tableCreateAware);
|
|
|
|
this.tableCreateAwares.add(tableCreateAware);
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TableBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
|
|
|
|
public TableBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
|
|
|
|
this.columnCreateAwares.add(columnCreateAware);
|
|
|
|
this.columnCreateAwares.add(columnCreateAware);
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
@ -50,9 +48,9 @@ public class TableBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
public List<TableInfo> build() {
|
|
|
|
public List<TableInfo> build() {
|
|
|
|
List<TableInfo> result = new ArrayList<>();
|
|
|
|
List<TableInfo> result = new ArrayList<>();
|
|
|
|
List<String> schemaNames = configProperties.getStrings(CfgConsts.SCHEMA_NAMES);
|
|
|
|
List<String> schemaNames = CONFIG.getStrings(CfgConsts.SCHEMA_NAMES);
|
|
|
|
if (schemaNames.isEmpty()) {
|
|
|
|
if (schemaNames.isEmpty()) {
|
|
|
|
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL);
|
|
|
|
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL);
|
|
|
|
List<Map<String, String>> tables = DBUtils.list(sql);
|
|
|
|
List<Map<String, String>> tables = DBUtils.list(sql);
|
|
|
|
for (Map<String, String> table : tables) {
|
|
|
|
for (Map<String, String> table : tables) {
|
|
|
|
result.add(createTableInfo(table));
|
|
|
|
result.add(createTableInfo(table));
|
|
|
@ -68,9 +66,9 @@ public class TableBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
public List<TableInfo> build(String schemaName) {
|
|
|
|
public List<TableInfo> build(String schemaName) {
|
|
|
|
List<TableInfo> result = new ArrayList<>();
|
|
|
|
List<TableInfo> result = new ArrayList<>();
|
|
|
|
List<String> tableNames = configProperties.getStrings(CfgConsts.TABLE_NAMES);
|
|
|
|
List<String> tableNames = CONFIG.getStrings(CfgConsts.TABLE_NAMES);
|
|
|
|
if (tableNames.isEmpty()) {
|
|
|
|
if (tableNames.isEmpty()) {
|
|
|
|
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA);
|
|
|
|
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL_SCHEMA);
|
|
|
|
List<String> params = Collections.singletonList(schemaName);
|
|
|
|
List<String> params = Collections.singletonList(schemaName);
|
|
|
|
List<Map<String, String>> tables = DBUtils.list(sql, params);
|
|
|
|
List<Map<String, String>> tables = DBUtils.list(sql, params);
|
|
|
|
for (Map<String, String> table : tables) {
|
|
|
|
for (Map<String, String> table : tables) {
|
|
|
@ -88,7 +86,7 @@ public class TableBuilder {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TableInfo build(String schemaName, String tableName) {
|
|
|
|
public TableInfo build(String schemaName, String tableName) {
|
|
|
|
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
|
|
|
|
String sql = MYSQL_CONFIG.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
|
|
|
|
List<String> params = Arrays.asList(schemaName, tableName);
|
|
|
|
List<String> params = Arrays.asList(schemaName, tableName);
|
|
|
|
Map<String, String> table = DBUtils.one(sql, params);
|
|
|
|
Map<String, String> table = DBUtils.one(sql, params);
|
|
|
|
if (Objects.isNull(table)) {
|
|
|
|
if (Objects.isNull(table)) {
|
|
|
@ -101,15 +99,15 @@ public class TableBuilder {
|
|
|
|
private TableInfo createTableInfo(Map<String, String> table) {
|
|
|
|
private TableInfo createTableInfo(Map<String, String> table) {
|
|
|
|
TableInfo result = new TableInfo();
|
|
|
|
TableInfo result = new TableInfo();
|
|
|
|
|
|
|
|
|
|
|
|
String tableSchemaLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_SCHEMA);
|
|
|
|
String tableSchemaLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_SCHEMA);
|
|
|
|
String tableSchema = table.get(tableSchemaLabel);
|
|
|
|
String tableSchema = table.get(tableSchemaLabel);
|
|
|
|
result.setTableSchema(tableSchema);
|
|
|
|
result.setTableSchema(tableSchema);
|
|
|
|
|
|
|
|
|
|
|
|
String tableCommentLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_COMMENT);
|
|
|
|
String tableCommentLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_COMMENT);
|
|
|
|
String comment = table.get(tableCommentLabel);
|
|
|
|
String comment = table.get(tableCommentLabel);
|
|
|
|
result.setComment(comment);
|
|
|
|
result.setComment(comment);
|
|
|
|
|
|
|
|
|
|
|
|
String tableNameLabel = sqlProperties.getString(SqlConsts.LABEL_TABLE_NAME);
|
|
|
|
String tableNameLabel = MYSQL_CONFIG.getString(SqlConsts.LABEL_TABLE_NAME);
|
|
|
|
String tableName = table.get(tableNameLabel);
|
|
|
|
String tableName = table.get(tableNameLabel);
|
|
|
|
result.setTableName(tableName);
|
|
|
|
result.setTableName(tableName);
|
|
|
|
|
|
|
|
|
|
|
@ -125,16 +123,19 @@ public class TableBuilder {
|
|
|
|
result.setDecimalExists(columnInfo.stream()
|
|
|
|
result.setDecimalExists(columnInfo.stream()
|
|
|
|
.anyMatch(column -> TypeUtils.JAVA_TYPE_DECIMAL.equals(column.getJavaDataType())));
|
|
|
|
.anyMatch(column -> TypeUtils.JAVA_TYPE_DECIMAL.equals(column.getJavaDataType())));
|
|
|
|
|
|
|
|
|
|
|
|
List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo(result.getTableSchema(),
|
|
|
|
List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo(
|
|
|
|
|
|
|
|
result.getTableSchema(),
|
|
|
|
result.getTableName()
|
|
|
|
result.getTableName()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, List<ColumnInfo>> indexInfo = new HashMap<>();
|
|
|
|
Map<String, List<ColumnInfo>> indexInfo = new HashMap<>();
|
|
|
|
indexInfoList.forEach(index -> {
|
|
|
|
indexInfoList.forEach(index -> {
|
|
|
|
List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent(index.getIndexName(),
|
|
|
|
List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent(
|
|
|
|
|
|
|
|
index.getIndexName(),
|
|
|
|
k -> new ArrayList<>()
|
|
|
|
k -> new ArrayList<>()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName(columnInfo,
|
|
|
|
ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName(
|
|
|
|
|
|
|
|
columnInfo,
|
|
|
|
index.getColumnName()
|
|
|
|
index.getColumnName()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
indexColumnInfoList.add(columnInfoByColumnName);
|
|
|
|
indexColumnInfoList.add(columnInfoByColumnName);
|
|
|
@ -168,7 +169,7 @@ public class TableBuilder {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
|
|
|
|
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
|
|
|
|
List<String> tablePrefixes = configProperties.getStrings(CfgConsts.TABLE_PREFIXIES);
|
|
|
|
List<String> tablePrefixes = CONFIG.getStrings(CfgConsts.TABLE_PREFIXIES);
|
|
|
|
|
|
|
|
|
|
|
|
for (String tablePrefix : tablePrefixes) {
|
|
|
|
for (String tablePrefix : tablePrefixes) {
|
|
|
|
tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);
|
|
|
|
tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);
|
|
|
|