代码整理

This commit is contained in:
inrgihc
2022-08-14 00:27:46 +08:00
parent 31dd8e38d1
commit 898106d409
6 changed files with 318 additions and 326 deletions

View File

@@ -3,15 +3,6 @@ FROM openjdk:8-jre-alpine
ENV TZ=Asia/Shanghai ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD dbswitch-release.tar.gz / ADD dbswitch-release.tar.gz /
EXPOSE 9088 EXPOSE 9088

0
build-docker/dbswitch/dbswitch-release/bin/startup.sh Executable file → Normal file
View File

View File

@@ -41,8 +41,11 @@ import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronExpression;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.quartz.CronExpression;
@Service @Service
public class AssignmentService { public class AssignmentService {

View File

@@ -22,6 +22,9 @@ import org.springframework.stereotype.Service;
@Service @Service
public class PatternMapperService { public class PatternMapperService {
private final String STRING_EMPTY = "<!空>";
private final String STRING_DELETE = "<!删除>";
@Resource @Resource
private DbConnectionService connectionService; private DbConnectionService connectionService;
@@ -34,18 +37,22 @@ public class PatternMapperService {
List<PreviewNameMapperResponse> result = new ArrayList<>(); List<PreviewNameMapperResponse> result = new ArrayList<>();
if (CollectionUtils.isEmpty(request.getTableNames())) { if (CollectionUtils.isEmpty(request.getTableNames())) {
for (TableDescription td : getAllTableNames(request)) { for (TableDescription td : getAllTableNames(request)) {
String targetName = PatterNameUtils.getFinalName(
td.getTableName(), request.getNameMapper());
result.add(PreviewNameMapperResponse.builder() result.add(PreviewNameMapperResponse.builder()
.originalName(td.getTableName()) .originalName(td.getTableName())
.targetName(PatterNameUtils.getFinalName(td.getTableName(), request.getNameMapper())) .targetName(StringUtils.isNotBlank(targetName) ? targetName : STRING_EMPTY)
.build()); .build());
} }
} else { } else {
if (include) { if (include) {
for (String name : request.getTableNames()) { for (String name : request.getTableNames()) {
if (StringUtils.isNotBlank(name)) { if (StringUtils.isNotBlank(name)) {
String targetName = PatterNameUtils.getFinalName(
name, request.getNameMapper());
result.add(PreviewNameMapperResponse.builder() result.add(PreviewNameMapperResponse.builder()
.originalName(name) .originalName(name)
.targetName(PatterNameUtils.getFinalName(name, request.getNameMapper())) .targetName(StringUtils.isNotBlank(targetName) ? targetName : STRING_EMPTY)
.build()); .build());
} }
} }
@@ -92,7 +99,7 @@ public class PatternMapperService {
} else { } else {
result.add(PreviewNameMapperResponse.builder() result.add(PreviewNameMapperResponse.builder()
.originalName(cd.getFieldName()) .originalName(cd.getFieldName())
.targetName("<!字段被删除>") .targetName(STRING_DELETE)
.build()); .build());
} }
} }
@@ -110,14 +117,10 @@ public class PatternMapperService {
if (null == dbConn) { if (null == dbConn) {
throw new DbswitchException(ResultCode.ERROR_RESOURCE_NOT_EXISTS, "id=" + request.getId()); throw new DbswitchException(ResultCode.ERROR_RESOURCE_NOT_EXISTS, "id=" + request.getId());
} }
IMetaDataByJdbcService service = connectionService.getMetaDataCoreService(dbConn); IMetaDataByJdbcService service = connectionService.getMetaDataCoreService(dbConn);
return service.queryTableList( return service.queryTableList(dbConn.getUrl(), dbConn.getUsername(), dbConn.getPassword(),
dbConn.getUrl(), request.getSchemaName()).stream().filter(td -> !td.isViewTable())
dbConn.getUsername(),
dbConn.getPassword(),
request.getSchemaName()
).stream()
.filter(td -> !td.isViewTable())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@@ -9,312 +9,310 @@
///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////
package com.gitee.dbswitch.core.database; package com.gitee.dbswitch.core.database;
import java.util.List; import com.gitee.dbswitch.common.type.DatabaseTypeEnum;
import java.util.Objects; import com.gitee.dbswitch.common.util.DbswitchStrUtils;
import java.util.Properties; import com.gitee.dbswitch.common.util.HivePrepareUtils;
import java.util.Set; import com.gitee.dbswitch.common.util.TypeConvertUtils;
import java.util.ArrayList; import com.gitee.dbswitch.core.model.ColumnDescription;
import java.util.HashSet; import com.gitee.dbswitch.core.model.ColumnMetaData;
import com.gitee.dbswitch.core.model.SchemaTableData;
import com.gitee.dbswitch.core.model.TableDescription;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.common.constant.DatabaseTypeEnum;
import com.gitee.dbswitch.core.model.ColumnDescription;
import com.gitee.dbswitch.core.model.ColumnMetaData;
import com.gitee.dbswitch.core.model.TableDescription;
import com.gitee.dbswitch.core.util.JdbcOperatorUtils;
/** /**
* 数据库元信息抽象基类 * 数据库元信息抽象基类
*
* @author tang
* *
* @author tang
*/ */
public abstract class AbstractDatabase implements IDatabaseInterface { public abstract class AbstractDatabase implements IDatabaseInterface {
public static final int CLOB_LENGTH = 9999999; public static final int CLOB_LENGTH = 9999999;
protected Connection connection = null; protected String driverClassName;
protected DatabaseMetaData metaData = null; protected String catalogName = null;
protected String catalogName = null;
public AbstractDatabase(String driverClassName) { public AbstractDatabase(String driverClassName) {
this.catalogName = null; try {
this.driverClassName = driverClassName;
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
try { @Override
Class.forName(driverClassName); public String getDriverClassName() {
} catch (ClassNotFoundException e) { return this.driverClassName;
throw new RuntimeException(e); }
}
}
@Override @Override
public void connect(String jdbcUrl, String username, String password) { public List<String> querySchemaList(Connection connection) {
/* Set<String> ret = new HashSet<>();
* 超时时间设置问题: https://blog.csdn.net/lsunwing/article/details/79461217 try (ResultSet schemas = connection.getMetaData().getSchemas()) {
* https://blog.csdn.net/weixin_34405332/article/details/91664781 while (schemas.next()) {
*/ ret.add(schemas.getString("TABLE_SCHEM"));
try { }
/** return new ArrayList<>(ret);
* Oracle在通过jdbc连接的时候需要添加一个参数来设置是否获取注释 } catch (SQLException e) {
*/ throw new RuntimeException(e);
Properties props = new Properties(); }
props.put("user", username); }
props.put("password", password);
props.put("remarksReporting", "true");
// 设置最大时间 @Override
DriverManager.setLoginTimeout(15); public List<TableDescription> queryTableList(Connection connection, String schemaName) {
List<TableDescription> ret = new ArrayList<>();
this.connection = DriverManager.getConnection(jdbcUrl, props); Set<String> uniqueSet = new HashSet<>();
if (Objects.isNull(this.connection)) { String[] types = new String[]{"TABLE", "VIEW"};
throw new RuntimeException("数据库连接失败,连接参数为:" + jdbcUrl); try (ResultSet tables = connection.getMetaData()
} .getTables(this.catalogName, schemaName, "%", types)) {
while (tables.next()) {
this.metaData = Objects.requireNonNull(this.connection.getMetaData()); String tableName = tables.getString("TABLE_NAME");
} catch (SQLException e) { if (uniqueSet.contains(tableName)) {
throw new RuntimeException(e); continue;
} } else {
uniqueSet.add(tableName);
}
} TableDescription td = new TableDescription();
td.setSchemaName(schemaName);
td.setTableName(tableName);
td.setRemarks(tables.getString("REMARKS"));
td.setTableType(tables.getString("TABLE_TYPE").toUpperCase());
ret.add(td);
}
return ret;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public void close() { public TableDescription queryTableMeta(Connection connection, String schemaName,
if (null != connection) { String tableName) {
try { return queryTableList(connection, schemaName).stream()
connection.close(); .filter(one -> tableName.equals(one.getTableName()))
} catch (SQLException e) { .findAny().orElse(null);
} }
connection = null;
}
}
@Override @Override
public List<String> querySchemaList() { public List<String> queryTableColumnName(Connection connection, String schemaName,
Set<String> ret = new HashSet<>(); String tableName) {
ResultSet schemas = null; Set<String> columns = new HashSet<>();
try { try (ResultSet rs = connection.getMetaData()
schemas = this.metaData.getSchemas(); .getColumns(this.catalogName, schemaName, tableName, null)) {
while (schemas.next()) { while (rs.next()) {
ret.add(schemas.getString("TABLE_SCHEM")); columns.add(rs.getString("COLUMN_NAME"));
} }
return new ArrayList<>(ret); } catch (SQLException e) {
} catch (SQLException e) { throw new RuntimeException(e);
throw new RuntimeException(e); }
} finally { return new ArrayList<>(columns);
try { }
if (null != schemas) {
schemas.close();
schemas = null;
}
} catch (SQLException e) {
}
}
} @Override
public List<ColumnDescription> queryTableColumnMeta(Connection connection, String schemaName,
String tableName) {
String sql = this.getTableFieldsQuerySQL(schemaName, tableName);
List<ColumnDescription> ret = this.querySelectSqlColumnMeta(connection, sql);
@Override // 补充一下注释信息
public List<TableDescription> queryTableList(String schemaName) { try (ResultSet columns = connection.getMetaData()
List<TableDescription> ret = new ArrayList<>(); .getColumns(this.catalogName, schemaName, tableName, null)) {
Set<String> uniqueSet = new HashSet<>(); while (columns.next()) {
ResultSet tables = null; String columnName = columns.getString("COLUMN_NAME");
try { String remarks = columns.getString("REMARKS");
tables = this.metaData.getTables(this.catalogName, schemaName, "%", new String[] { "TABLE", "VIEW" }); for (ColumnDescription cd : ret) {
while (tables.next()) { if (columnName.equals(cd.getFieldName())) {
String tableName = tables.getString("TABLE_NAME"); cd.setRemarks(remarks);
if (uniqueSet.contains(tableName)) { }
continue; }
} else { }
uniqueSet.add(tableName); } catch (SQLException e) {
} throw new RuntimeException(e);
}
return ret;
}
TableDescription td = new TableDescription(); @Override
td.setSchemaName(schemaName); public List<String> queryTablePrimaryKeys(Connection connection, String schemaName,
td.setTableName(tableName); String tableName) {
td.setRemarks(tables.getString("REMARKS")); Set<String> ret = new HashSet<>();
td.setTableType(tables.getString("TABLE_TYPE").toUpperCase()); try (ResultSet primaryKeys = connection.getMetaData()
ret.add(td); .getPrimaryKeys(this.catalogName, schemaName, tableName)) {
} while (primaryKeys.next()) {
return ret; String name = primaryKeys.getString("COLUMN_NAME");
} catch (SQLException e) { if (!ret.contains(name)) {
throw new RuntimeException(e); ret.add(name);
} finally { }
try { }
if (null != tables) { return new ArrayList<>(ret);
tables.close(); } catch (SQLException e) {
tables = null; throw new RuntimeException(e);
} }
} catch (SQLException e) { }
}
}
}
@Override @Override
public List<ColumnDescription> queryTableColumnMeta(String schemaName, String tableName) { public SchemaTableData queryTableData(Connection connection, String schemaName, String tableName,
String sql = this.getTableFieldsQuerySQL(schemaName, tableName); int rowCount) {
List<ColumnDescription> ret = this.querySelectSqlColumnMeta(sql); String fullTableName = getQuotedSchemaTableCombination(schemaName, tableName);
ResultSet columns = null; String querySQL = String.format("SELECT * FROM %s ", fullTableName);
try { SchemaTableData data = new SchemaTableData();
columns = this.metaData.getColumns(this.catalogName, schemaName, tableName, null); data.setSchemaName(schemaName);
while (columns.next()) { data.setTableName(tableName);
String columnName = columns.getString("COLUMN_NAME"); data.setColumns(new ArrayList<>());
String remarks = columns.getString("REMARKS"); data.setRows(new ArrayList<>());
for (ColumnDescription cd : ret) { try (Statement st = connection.createStatement()) {
if (columnName.equalsIgnoreCase(cd.getFieldName())) { if (getDatabaseType() == DatabaseTypeEnum.HIVE) {
cd.setRemarks(remarks); HivePrepareUtils.prepare(connection, schemaName, tableName);
} }
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (null != columns) {
columns.close();
columns = null;
}
} catch (SQLException e) {
}
}
return ret; try (ResultSet rs = st.executeQuery(querySQL)) {
} ResultSetMetaData m = rs.getMetaData();
int count = m.getColumnCount();
for (int i = 1; i <= count; i++) {
data.getColumns().add(m.getColumnLabel(i));
}
@Override int counter = 0;
public List<String> queryTablePrimaryKeys(String schemaName, String tableName) { while (rs.next() && counter++ < rowCount) {
Set<String> ret = new HashSet<>(); List<Object> row = new ArrayList<>(count);
ResultSet primarykeys = null; for (int i = 1; i <= count; i++) {
try { Object value = rs.getObject(i);
primarykeys = this.metaData.getPrimaryKeys(this.catalogName, schemaName, tableName); if (value != null && value instanceof byte[]) {
while (primarykeys.next()) { row.add(DbswitchStrUtils.toHexString((byte[]) value));
String name = primarykeys.getString("COLUMN_NAME"); } else if (value != null && value instanceof java.sql.Clob) {
if (!ret.contains(name)) { row.add(TypeConvertUtils.castToString(value));
ret.add(name); } else if (value != null && value instanceof java.sql.Blob) {
} byte[] bytes = TypeConvertUtils.castToByteArray(value);
} row.add(DbswitchStrUtils.toHexString(bytes));
return new ArrayList<>(ret); } else {
} catch (SQLException e) { row.add(null == value ? null : value.toString());
throw new RuntimeException(e); }
} finally { }
try { data.getRows().add(row);
if (null != primarykeys) { }
primarykeys.close();
primarykeys = null;
}
} catch (SQLException e) {
}
}
}
@Override return data;
public abstract List<ColumnDescription> querySelectSqlColumnMeta(String sql); }
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public void testQuerySQL(String sql) { public void testQuerySQL(Connection connection, String sql) {
String wrapperSql = this.getTestQuerySQL(sql); String wrapperSql = this.getTestQuerySQL(sql);
try (Statement statement = this.connection.createStatement();) { try (Statement statement = connection.createStatement();) {
statement.execute(wrapperSql); statement.execute(wrapperSql);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public String getQuotedSchemaTableCombination(String schemaName, String tableName) { public String getQuotedSchemaTableCombination(String schemaName, String tableName) {
return String.format(" \"%s\".\"%s\" ", schemaName, tableName); return String.format(" \"%s\".\"%s\" ", schemaName, tableName);
} }
@Override @Override
public String getFieldDefinition(ColumnMetaData v, List<String> pks, boolean useAutoInc, boolean addCr) { public String getFieldDefinition(ColumnMetaData v, List<String> pks, boolean useAutoInc,
throw new RuntimeException("AbstractDatabase Unempliment!"); boolean addCr, boolean withRemarks) {
} throw new RuntimeException("AbstractDatabase Unimplemented!");
}
@Override @Override
public String getPrimaryKeyAsString(List<String> pks) { public String getPrimaryKeyAsString(List<String> pks) {
if (!pks.isEmpty()) { if (!pks.isEmpty()) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("\""); sb.append("\"");
sb.append(StringUtils.join(pks, "\" , \"")); sb.append(StringUtils.join(pks, "\" , \""));
sb.append("\""); sb.append("\"");
return sb.toString(); return sb.toString();
} }
return ""; return "";
} }
/************************************** @Override
* internal function public List<String> getTableColumnCommentDefinition(TableDescription td,
**************************************/ List<ColumnDescription> cds) {
throw new RuntimeException("AbstractDatabase Unimplemented!");
}
protected abstract String getTableFieldsQuerySQL(String schemaName, String tableName); /**************************************
* internal function
**************************************/
protected abstract String getTestQuerySQL(String sql); protected abstract String getTableFieldsQuerySQL(String schemaName, String tableName);
protected List<ColumnDescription> getSelectSqlColumnMeta(String querySQL, DatabaseTypeEnum dbtype) { protected abstract String getTestQuerySQL(String sql);
List<ColumnDescription> ret = new ArrayList<ColumnDescription>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try { protected List<ColumnDescription> getSelectSqlColumnMeta(Connection connection, String querySQL) {
pstmt = this.connection.prepareStatement(querySQL); List<ColumnDescription> ret = new ArrayList<>();
rs = pstmt.executeQuery(); try (Statement st = connection.createStatement()) {
if (getDatabaseType() == DatabaseTypeEnum.HIVE) {
HivePrepareUtils.setResultSetColumnNameNotUnique(connection);
}
ResultSetMetaData m = rs.getMetaData(); try (ResultSet rs = st.executeQuery(querySQL)) {
int columns = m.getColumnCount(); ResultSetMetaData m = rs.getMetaData();
for (int i = 1; i <= columns; i++) { int columns = m.getColumnCount();
String name = m.getColumnLabel(i); for (int i = 1; i <= columns; i++) {
if (null == name) { String name = m.getColumnLabel(i);
name = m.getColumnName(i); if (null == name) {
} name = m.getColumnName(i);
}
ColumnDescription cd = new ColumnDescription(); ColumnDescription cd = new ColumnDescription();
cd.setFieldName(name); cd.setFieldName(name);
cd.setLabelName(name); cd.setLabelName(name);
cd.setFieldType(m.getColumnType(i)); cd.setFieldType(m.getColumnType(i));
if (0 != cd.getFieldType()) { if (0 != cd.getFieldType()) {
cd.setFieldTypeName(m.getColumnTypeName(i)); cd.setFieldTypeName(m.getColumnTypeName(i));
cd.setFiledTypeClassName(m.getColumnClassName(i)); cd.setFiledTypeClassName(m.getColumnClassName(i));
cd.setDisplaySize(m.getColumnDisplaySize(i)); cd.setDisplaySize(m.getColumnDisplaySize(i));
cd.setPrecisionSize(m.getPrecision(i)); cd.setPrecisionSize(m.getPrecision(i));
cd.setScaleSize(m.getScale(i)); cd.setScaleSize(m.getScale(i));
cd.setAutoIncrement(m.isAutoIncrement(i)); cd.setAutoIncrement(m.isAutoIncrement(i));
cd.setNullable(m.isNullable(i) != ResultSetMetaData.columnNoNulls); cd.setNullable(m.isNullable(i) != ResultSetMetaData.columnNoNulls);
} else { } else {
// 处理视图中NULL as fieldName的情况 // 处理视图中NULL as fieldName的情况
cd.setFieldTypeName("CHAR"); cd.setFieldTypeName("CHAR");
cd.setFiledTypeClassName(String.class.getName()); cd.setFiledTypeClassName(String.class.getName());
cd.setDisplaySize(1); cd.setDisplaySize(1);
cd.setPrecisionSize(1); cd.setPrecisionSize(1);
cd.setScaleSize(0); cd.setScaleSize(0);
cd.setAutoIncrement(false); cd.setAutoIncrement(false);
cd.setNullable(true); cd.setNullable(true);
} }
boolean signed = false; boolean signed = false;
try { try {
signed = m.isSigned(i); signed = m.isSigned(i);
} catch (Exception ignored) { } catch (Exception ignored) {
// This JDBC Driver doesn't support the isSigned method // This JDBC Driver doesn't support the isSigned method
// nothing more we can do here by catch the exception. // nothing more we can do here by catch the exception.
} }
cd.setSigned(signed); cd.setSigned(signed);
cd.setDbType(dbtype); cd.setDbType(getDatabaseType());
ret.add(cd); ret.add(cd);
} }
return ret; return ret;
} catch (SQLException e) { }
throw new RuntimeException(e); } catch (SQLException e) {
} finally { throw new RuntimeException(e);
JdbcOperatorUtils.closeResultSet(rs); }
JdbcOperatorUtils.closeStatement(pstmt); }
}
} }
}

View File

@@ -34,7 +34,6 @@ import com.gitee.dbswitch.dbwriter.IDatabaseWriter;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@@ -114,6 +113,10 @@ public class MigrationHandler implements Supplier<Long> {
this.targetTableName = PatterNameUtils.getFinalName(td.getTableName(), this.targetTableName = PatterNameUtils.getFinalName(td.getTableName(),
sourceProperties.getRegexTableMapper()); sourceProperties.getRegexTableMapper());
if (StringUtils.isEmpty(this.targetTableName)) {
throw new RuntimeException("表名的映射规则配置有误,不能将[" + this.sourceTableName + "]映射为空");
}
this.tableNameMapString = String.format("%s.%s --> %s.%s", this.tableNameMapString = String.format("%s.%s --> %s.%s",
td.getSchemaName(), td.getTableName(), td.getSchemaName(), td.getTableName(),
targetSchemaName, targetTableName); targetSchemaName, targetTableName);
@@ -160,16 +163,26 @@ public class MigrationHandler implements Supplier<Long> {
String targetColumnName = targetColumnDescriptions.get(i).getFieldName(); String targetColumnName = targetColumnDescriptions.get(i).getFieldName();
if (StringUtils.hasLength(targetColumnName)) { if (StringUtils.hasLength(targetColumnName)) {
columnMapperPairs.add(String.format("%s --> %s", sourceColumnName, targetColumnName)); columnMapperPairs.add(String.format("%s --> %s", sourceColumnName, targetColumnName));
mapChecker.put(sourceColumnName, targetColumnName);
} else { } else {
columnMapperPairs.add(String.format("%s --> %s", sourceColumnName, "<!Field is Deleted>")); columnMapperPairs.add(String.format(
"%s --> %s",
sourceColumnName,
String.format("<!Field(%s) is Deleted>", (i + 1))
));
} }
mapChecker.put(sourceColumnName, targetColumnName);
} }
log.info("Mapping relation : \ntable mapper :\n\t{} \ncolumn mapper :\n\t{} ", log.info("Mapping relation : \ntable mapper :\n\t{} \ncolumn mapper :\n\t{} ",
tableNameMapString, columnMapperPairs.stream().collect(Collectors.joining("\n\t"))); tableNameMapString, String.join("\n\t", columnMapperPairs));
Set<String> valueSet = new HashSet<>(mapChecker.values()); Set<String> valueSet = new HashSet<>(mapChecker.values());
if (valueSet.size() <= 0) {
throw new RuntimeException("字段映射配置有误,禁止通过映射将表所有的字段都删除!");
}
if (!valueSet.containsAll(this.targetPrimaryKeys)) {
throw new RuntimeException("字段映射配置有误,禁止通过映射将表的主键字段删除!");
}
if (mapChecker.keySet().size() != valueSet.size()) { if (mapChecker.keySet().size() != valueSet.size()) {
throw new RuntimeException("字段映射配置有误,多个字段映射到一个同名字段!"); throw new RuntimeException("字段映射配置有误,禁止将多个字段映射到一个同名字段!");
} }
IDatabaseWriter writer = DatabaseWriterFactory.createDatabaseWriter( IDatabaseWriter writer = DatabaseWriterFactory.createDatabaseWriter(
@@ -258,24 +271,16 @@ public class MigrationHandler implements Supplier<Long> {
private Long doFullCoverSynchronize(IDatabaseWriter writer) { private Long doFullCoverSynchronize(IDatabaseWriter writer) {
final int BATCH_SIZE = fetchSize; final int BATCH_SIZE = fetchSize;
List<String> sourceFields = sourceColumnDescriptions.stream() List<String> sourceFields = new ArrayList<>();
.map(ColumnDescription::getFieldName) List<String> targetFields = new ArrayList<>();
.collect(Collectors.toList()); for (int i = 0; i < targetColumnDescriptions.size(); ++i) {
List<String> targetFields = targetColumnDescriptions.stream() ColumnDescription scd = sourceColumnDescriptions.get(i);
.map(ColumnDescription::getFieldName) ColumnDescription tcd = targetColumnDescriptions.get(i);
.collect(Collectors.toList()); if (!StringUtils.isEmpty(tcd.getFieldName())) {
List<Integer> deletedFieldIndexes = new ArrayList<>(); sourceFields.add(scd.getFieldName());
for (int i = 0; i < targetFields.size(); ++i) { targetFields.add(tcd.getFieldName());
if (StringUtils.isEmpty(targetFields.get(i))) {
deletedFieldIndexes.add(i);
} }
} }
Collections.reverse(deletedFieldIndexes);
deletedFieldIndexes.forEach(i -> {
sourceFields.remove(sourceFields.get(i));
targetFields.remove(targetFields.get(i));
});
// 准备目的端的数据写入操作 // 准备目的端的数据写入操作
writer.prepareWrite(targetSchemaName, targetTableName, targetFields); writer.prepareWrite(targetSchemaName, targetTableName, targetFields);
@@ -351,26 +356,18 @@ public class MigrationHandler implements Supplier<Long> {
*/ */
private Long doIncreaseSynchronize(IDatabaseWriter writer) { private Long doIncreaseSynchronize(IDatabaseWriter writer) {
final int BATCH_SIZE = fetchSize; final int BATCH_SIZE = fetchSize;
List<String> sourceFields = sourceColumnDescriptions.stream()
.map(ColumnDescription::getFieldName) List<String> sourceFields = new ArrayList<>();
.collect(Collectors.toList()); List<String> targetFields = new ArrayList<>();
List<String> targetFields = targetColumnDescriptions.stream()
.map(ColumnDescription::getFieldName)
.collect(Collectors.toList());
List<Integer> deletedFieldIndexes = new ArrayList<>();
for (int i = 0; i < targetFields.size(); ++i) {
if (StringUtils.isEmpty(targetFields.get(i))) {
deletedFieldIndexes.add(i);
}
}
Collections.reverse(deletedFieldIndexes);
deletedFieldIndexes.forEach(i -> {
sourceFields.remove(sourceFields.get(i));
targetFields.remove(targetFields.get(i));
});
Map<String, String> columnNameMaps = new HashMap<>(); Map<String, String> columnNameMaps = new HashMap<>();
for (int i = 0; i < sourceFields.size(); ++i) { for (int i = 0; i < targetColumnDescriptions.size(); ++i) {
columnNameMaps.put(sourceFields.get(i), targetFields.get(i)); ColumnDescription scd = sourceColumnDescriptions.get(i);
ColumnDescription tcd = targetColumnDescriptions.get(i);
if (!StringUtils.isEmpty(tcd.getFieldName())) {
sourceFields.add(scd.getFieldName());
targetFields.add(tcd.getFieldName());
columnNameMaps.put(scd.getFieldName(), tcd.getFieldName());
}
} }
TaskParamEntity.TaskParamEntityBuilder taskBuilder = TaskParamEntity.builder(); TaskParamEntity.TaskParamEntityBuilder taskBuilder = TaskParamEntity.builder();