|
@@ -12,6 +12,7 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
+import java.sql.Connection;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
@@ -40,20 +41,27 @@ public class KingBase2GBaseService {
|
|
|
|
|
|
|
|
|
try {
|
|
|
- ResultSet set = thisDatasource.getConnection().getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
|
|
|
+ Connection conn = thisDatasource.getConnection();
|
|
|
+ ResultSet set = conn.getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
|
|
|
if (set == null) {
|
|
|
+ conn.close();
|
|
|
throw new IllegalArgumentException("本数据库中不存在对应的数据库表【{}】,请检查");
|
|
|
}
|
|
|
+ conn.close();
|
|
|
} catch (SQLException e) {
|
|
|
log.error("本数据库中不存在对应的数据库表【{}】,请检查", tableName);
|
|
|
throw new IllegalArgumentException("本数据库中不存在对应的数据库表【{}】,请检查", e);
|
|
|
|
|
|
}
|
|
|
try {
|
|
|
- ResultSet set = otherDataSource.getConnection().getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
|
|
|
+ Connection conn = otherDataSource.getConnection();
|
|
|
+ ResultSet set = conn.getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
|
|
|
+
|
|
|
if (set == null) {
|
|
|
+ conn.close();
|
|
|
throw new IllegalArgumentException("目标数据库中不存在对应的数据库表【{}】,请检查");
|
|
|
}
|
|
|
+ conn.close();
|
|
|
} catch (SQLException e) {
|
|
|
log.error("目标数据库中不存在对应的数据库表【{}】,请检查", tableName);
|
|
|
throw new IllegalArgumentException("目标数据库中不存在对应的数据库表【{}】,请检查", e);
|
|
@@ -62,21 +70,22 @@ public class KingBase2GBaseService {
|
|
|
JdbcTemplate otherJdbc = new JdbcTemplate(otherDataSource);
|
|
|
|
|
|
|
|
|
- List<Map<String ,Object>> data = thisJdbc.query("select * from "+tableName,new UpperMapRowMapper());
|
|
|
+ List<Map<String, Object>> data = thisJdbc.query("select * from " + tableName, new UpperMapRowMapper());
|
|
|
if (data.isEmpty()) {
|
|
|
- log.warn("{}表中数据为空,不尽兴转换,请确认实际为空真实性",tableName);
|
|
|
+ log.warn("{}表中数据为空,不尽兴转换,请确认实际为空真实性", tableName);
|
|
|
return;
|
|
|
}
|
|
|
Set<String> keys = data.get(0).keySet();
|
|
|
List<String> sortedKeys = keys.stream().sorted().toList();
|
|
|
- String insertSqlKey = CollectionUtil.join(sortedKeys,",");
|
|
|
- String insertSqlValue = ":"+CollectionUtil.join(sortedKeys,", :");
|
|
|
- String sql = "insert into "+tableName+"("+insertSqlKey+") values("+insertSqlValue+")";
|
|
|
- log.debug("执行的SQL为:{}",sql);
|
|
|
+ String insertSqlKey = CollectionUtil.join(sortedKeys, ",");
|
|
|
+ String insertSqlValue = ":" + CollectionUtil.join(sortedKeys, ", :");
|
|
|
+ String sql = "insert into " + tableName + "(" + insertSqlKey + ") values(" + insertSqlValue + ")";
|
|
|
+ log.debug("执行的SQL为:{}", sql);
|
|
|
MapSqlParameterSource sqlParam = new MapSqlParameterSource();
|
|
|
- otherJdbc.update("delete from "+tableName,sqlParam);
|
|
|
- for (Map<String ,Object> map : data) {
|
|
|
- otherJdbc.update(sql,map);;
|
|
|
+ otherJdbc.update("delete from " + tableName, sqlParam);
|
|
|
+ for (Map<String, Object> map : data) {
|
|
|
+ otherJdbc.update(sql, map);
|
|
|
+ ;
|
|
|
}
|
|
|
}
|
|
|
|