|
@@ -1,6 +1,5 @@
|
|
|
package org.dbsyncer.connector.util;
|
|
|
|
|
|
-import org.apache.commons.dbcp.DelegatingDatabaseMetaData;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.dbsyncer.common.util.CollectionUtils;
|
|
|
import org.dbsyncer.connector.ConnectorException;
|
|
@@ -12,7 +11,6 @@ import org.dbsyncer.connector.database.DatabaseTemplate;
|
|
|
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
|
|
|
import org.springframework.jdbc.support.rowset.SqlRowSet;
|
|
|
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
|
|
|
-import org.springframework.util.Assert;
|
|
|
|
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
@@ -38,7 +36,7 @@ public abstract class DatabaseUtil {
|
|
|
try {
|
|
|
rs.close();
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ throw new ConnectorException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -62,7 +60,6 @@ public abstract class DatabaseUtil {
|
|
|
throw new ConnectorException("查询表字段不能为空.");
|
|
|
}
|
|
|
List<Field> fields = new ArrayList<>(columnCount);
|
|
|
- // <表名,[主键, ...]>
|
|
|
Map<String, List<String>> tables = new HashMap<>();
|
|
|
try {
|
|
|
DatabaseMetaData md = databaseTemplate.getConnection().getMetaData();
|
|
@@ -90,30 +87,6 @@ public abstract class DatabaseUtil {
|
|
|
return new MetaInfo().setColumn(fields);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取数据库名称
|
|
|
- *
|
|
|
- * @param conn
|
|
|
- * @return
|
|
|
- * @throws NoSuchFieldException
|
|
|
- * @throws SQLException
|
|
|
- * @throws IllegalAccessException
|
|
|
- */
|
|
|
- public static String getDataBaseName(Connection conn) throws NoSuchFieldException, SQLException, IllegalAccessException {
|
|
|
- DelegatingDatabaseMetaData md = (DelegatingDatabaseMetaData) conn.getMetaData();
|
|
|
- DatabaseMetaData delegate = md.getDelegate();
|
|
|
- String driverVersion = delegate.getDriverVersion();
|
|
|
- boolean driverThanMysql8 = isDriverVersionMoreThanMysql8(driverVersion);
|
|
|
- String databaseProductVersion = delegate.getDatabaseProductVersion();
|
|
|
- boolean dbThanMysql8 = isDatabaseProductVersionMoreThanMysql8(databaseProductVersion);
|
|
|
- Assert.isTrue(driverThanMysql8 == dbThanMysql8, String.format("当前驱动%s和数据库%s版本不一致.", driverVersion, databaseProductVersion));
|
|
|
-
|
|
|
- Class clazz = delegate.getClass().getSuperclass();
|
|
|
- java.lang.reflect.Field field = clazz.getDeclaredField("database");
|
|
|
- field.setAccessible(true);
|
|
|
- return (String) field.get(delegate);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 返回主键名称
|
|
|
*
|
|
@@ -135,30 +108,6 @@ public abstract class DatabaseUtil {
|
|
|
throw new ConnectorException("Table primary key can not be empty.");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Mysql 8.0
|
|
|
- * <p>mysql-connector-java-8.0.11</p>
|
|
|
- * <p>mysql-connector-java-5.1.40</p>
|
|
|
- *
|
|
|
- * @param driverVersion
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static boolean isDriverVersionMoreThanMysql8(String driverVersion) {
|
|
|
- return StringUtils.startsWith(driverVersion, "mysql-connector-java-8");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Mysql 8.0
|
|
|
- * <p>8.0.0-log</p>
|
|
|
- * <p>5.7.26-log</p>
|
|
|
- *
|
|
|
- * @param databaseProductVersion
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static boolean isDatabaseProductVersionMoreThanMysql8(String databaseProductVersion) {
|
|
|
- return StringUtils.startsWith(databaseProductVersion, "8");
|
|
|
- }
|
|
|
-
|
|
|
private static boolean isPk(Map<String, List<String>> tables, String tableName, String name) {
|
|
|
List<String> pk = tables.get(tableName);
|
|
|
return !CollectionUtils.isEmpty(pk) && pk.contains(name);
|