version for 1.6.3

This commit is contained in:
inrgihc
2022-01-23 00:53:48 +08:00
parent df66d0e1b4
commit 6fea38b0e4
281 changed files with 11250 additions and 10915 deletions

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.6.2</version>
<version>1.6.3</version>
</parent>
<artifactId>dbswitch-dbcommon</artifactId>

View File

@@ -11,24 +11,23 @@ package com.gitee.dbswitch.dbcommon.constant;
/**
* 常量值定义
*
* @author tang
*
* @author tang
*/
public final class Constants {
/**
* 默认的JDBC数据查询超时时间单位
*/
public static Integer DEFAULT_QUERY_TIMEOUT_SECONDS = 1 * 60 * 60;
/**
* 默认的fetch-size的值
*/
public static int DEFAULT_FETCH_SIZE = 1000;
/**
* fetch-size的最小有效值
*/
public static int MINIMUM_FETCH_SIZE = 100;
/**
* 默认的JDBC数据查询超时时间单位
*/
public static Integer DEFAULT_QUERY_TIMEOUT_SECONDS = 1 * 60 * 60;
/**
* 默认的fetch-size的值
*/
public static int DEFAULT_FETCH_SIZE = 1000;
/**
* fetch-size的最小有效值
*/
public static int MINIMUM_FETCH_SIZE = 100;
}

View File

@@ -9,16 +9,14 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database;
import com.gitee.dbswitch.dbcommon.constant.Constants;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import javax.sql.DataSource;
import org.springframework.jdbc.support.JdbcUtils;
import com.gitee.dbswitch.dbcommon.constant.Constants;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
import lombok.extern.slf4j.Slf4j;
/**
@@ -29,84 +27,78 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class AbstractDatabaseOperator implements IDatabaseOperator {
protected DataSource dataSource;
protected DataSource dataSource;
protected int fetchSize;
protected int fetchSize;
public AbstractDatabaseOperator(DataSource dataSource) {
this.dataSource = Objects.requireNonNull(dataSource, "数据源非法,为null");
this.fetchSize = Constants.DEFAULT_FETCH_SIZE;
public AbstractDatabaseOperator(DataSource dataSource) {
this.dataSource = Objects.requireNonNull(dataSource, "数据源非法,为null");
this.fetchSize = Constants.DEFAULT_FETCH_SIZE;
}
@Override
public DataSource getDataSource() {
return this.dataSource;
}
@Override
public int getFetchSize() {
return this.fetchSize;
}
@Override
public void setFetchSize(int size) {
if (size < Constants.MINIMUM_FETCH_SIZE) {
throw new IllegalArgumentException(
"设置的批量处理行数的大小fetchSize不得小于" + Constants.MINIMUM_FETCH_SIZE);
}
@Override
public DataSource getDataSource() {
return this.dataSource;
this.fetchSize = size;
}
/**
* 已经指定的查询SQL语句查询数据结果集
*
* @param sql 查询的SQL语句
* @param fetchSize 批处理大小
* @return 结果集包装对象
*/
protected final StatementResultSet selectTableData(String sql, int fetchSize) {
if (log.isDebugEnabled()) {
log.debug("Query table data sql :{}", sql);
}
@Override
public int getFetchSize() {
return this.fetchSize;
try {
StatementResultSet srs = new StatementResultSet();
srs.setConnection(dataSource.getConnection());
srs.setAutoCommit(srs.getConnection().getAutoCommit());
srs.getConnection().setAutoCommit(false);
srs.setStatement(srs.getConnection()
.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
srs.getStatement().setQueryTimeout(Constants.DEFAULT_QUERY_TIMEOUT_SECONDS);
srs.getStatement().setFetchSize(fetchSize);
srs.setResultset(srs.getStatement().executeQuery(sql));
return srs;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
@Override
public void setFetchSize(int size) {
if (size < Constants.MINIMUM_FETCH_SIZE) {
throw new IllegalArgumentException("设置的批量处理行数的大小fetchSize不得小于" + Constants.MINIMUM_FETCH_SIZE);
}
this.fetchSize = size;
/**
* 执行写SQL操作
*
* @param sql 写SQL语句
*/
protected final int executeSql(String sql) {
if (log.isDebugEnabled()) {
log.debug("Execute sql :{}", sql);
}
/**
* 已经指定的查询SQL语句查询数据结果集
*
* @param sql 查询的SQL语句
* @param fetchSize 批处理大小
* @return 结果集包装对象
*/
protected final StatementResultSet selectTableData(String sql, int fetchSize) {
if (log.isDebugEnabled()) {
log.debug("Query table data sql :{}", sql);
}
try {
StatementResultSet srs = new StatementResultSet();
srs.setConnection(dataSource.getConnection());
srs.setAutoCommit(srs.getConnection().getAutoCommit());
srs.getConnection().setAutoCommit(false);
srs.setStatement(srs.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
srs.getStatement().setQueryTimeout(Constants.DEFAULT_QUERY_TIMEOUT_SECONDS);
srs.getStatement().setFetchSize(fetchSize);
srs.setResultset(srs.getStatement().executeQuery(sql));
return srs;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
/**
* 执行写SQL操作
*
* @param sql 写SQL语句
*/
protected final int executeSql(String sql) {
Connection connection = null;
PreparedStatement pstmt = null;
if (log.isDebugEnabled()) {
log.debug("Execute sql :{}", sql);
}
try {
connection = dataSource.getConnection();
pstmt = connection.prepareStatement(sql);
return pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.closeStatement(pstmt);
JdbcUtils.closeConnection(connection);
}
try (Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql)) {
return ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -9,10 +9,6 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Function;
import javax.sql.DataSource;
import com.gitee.dbswitch.dbcommon.database.impl.DB2DatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.impl.DmDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.impl.GreenplumDatabaseOperator;
@@ -22,6 +18,10 @@ import com.gitee.dbswitch.dbcommon.database.impl.OracleDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.impl.PostgreSqlDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.impl.SqlServerDatabaseOperator;
import com.gitee.dbswitch.dbcommon.util.DatabaseAwareUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import javax.sql.DataSource;
/**
* 数据库操作器构造工厂类
@@ -30,35 +30,36 @@ import com.gitee.dbswitch.dbcommon.util.DatabaseAwareUtils;
*/
public final class DatabaseOperatorFactory {
private static final Map<String, Function<DataSource, IDatabaseOperator>> DATABASE_OPERATOR_MAPPER = new HashMap<String, Function<DataSource, IDatabaseOperator>>() {
private static final Map<String, Function<DataSource, IDatabaseOperator>> DATABASE_OPERATOR_MAPPER = new HashMap<String, Function<DataSource, IDatabaseOperator>>() {
private static final long serialVersionUID = -5278835613240515265L;
private static final long serialVersionUID = -5278835613240515265L;
{
put("MYSQL", MysqlDatabaseOperator::new);
put("ORACLE", OracleDatabaseOperator::new);
put("SQLSERVER", SqlServerDatabaseOperator::new);
put("POSTGRESQL", PostgreSqlDatabaseOperator::new);
put("GREENPLUM", GreenplumDatabaseOperator::new);
put("DB2", DB2DatabaseOperator::new);
put("DM", DmDatabaseOperator::new);
put("KINGBASE", KingbaseDatabaseOperator::new);
}
};
{
put("MYSQL", MysqlDatabaseOperator::new);
put("ORACLE", OracleDatabaseOperator::new);
put("SQLSERVER", SqlServerDatabaseOperator::new);
put("POSTGRESQL", PostgreSqlDatabaseOperator::new);
put("GREENPLUM", GreenplumDatabaseOperator::new);
put("DB2", DB2DatabaseOperator::new);
put("DM", DmDatabaseOperator::new);
put("KINGBASE", KingbaseDatabaseOperator::new);
}
};
/**
* 根据数据源获取数据的读取操作器
*
* @param dataSource 数据库源
* @return 指定类型的数据库读取器
*/
public static IDatabaseOperator createDatabaseOperator(DataSource dataSource) {
String type = DatabaseAwareUtils.getDatabaseNameByDataSource(dataSource).toUpperCase();
if (!DATABASE_OPERATOR_MAPPER.containsKey(type)) {
throw new RuntimeException(String.format("[dbcommon] Unkown Supported database type (%s)", type));
}
return DATABASE_OPERATOR_MAPPER.get(type).apply(dataSource);
/**
* 根据数据源获取数据的读取操作器
*
* @param dataSource 数据库源
* @return 指定类型的数据库读取器
*/
public static IDatabaseOperator createDatabaseOperator(DataSource dataSource) {
String type = DatabaseAwareUtils.getDatabaseNameByDataSource(dataSource).toUpperCase();
if (!DATABASE_OPERATOR_MAPPER.containsKey(type)) {
throw new RuntimeException(
String.format("[dbcommon] Unknown Supported database type (%s)", type));
}
return DATABASE_OPERATOR_MAPPER.get(type).apply(dataSource);
}
}

View File

@@ -9,85 +9,83 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* 数据库操作器接口定义
*
* @author tang
*
* @author tang
*/
public interface IDatabaseOperator {
/**
* 获取数据源
*
* @return 数据源
*/
DataSource getDataSource();
/**
* 获取数据源
*
* @return 数据源
*/
DataSource getDataSource();
/**
* 获取读取(fetch)数据的批次大小
*
* @return 批次大小
*/
int getFetchSize();
/**
* 获取读取(fetch)数据的批次大小
*
* @return 批次大小
*/
int getFetchSize();
/**
* 设置读取(fetch)数据的批次大小
*
* @param size 批次大小
*/
void setFetchSize(int size);
/**
* 设置读取(fetch)数据的批次大小
*
* @param size 批次大小
*/
void setFetchSize(int size);
/**
* 生成查询指定字段的select查询SQL语句
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @return 查询指定字段的select查询SQL语句
*/
String getSelectTableSql(String schemaName, String tableName, List<String> fields);
/**
* 生成查询指定字段的select查询SQL语句
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @return 查询指定字段的select查询SQL语句
*/
String getSelectTableSql(String schemaName, String tableName, List<String> fields);
/**
* 获取指定schema下表的按主键有序的结果集
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @param orders 排序字段列表
* @param fetchSize 批处理大小
* @return 结果集包装对象
*/
StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields, List<String> orders);
/**
* 获取指定schema下表的按主键有序的结果集
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @param orders 排序字段列表
* @return 结果集包装对象
*/
StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders);
/**
* 获取指定schema下表的结果集
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @param fetchSize 批处理大小
* @return 结果集包装对象
*/
StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields);
/**
* 获取指定schema下表的结果集
*
* @param schemaName 模式名称
* @param tableName 表名称
* @param fields 字段列表
* @return 结果集包装对象
*/
StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields);
/**
* 清除指定表的所有数据
*
* @param schemaName 模式名称
* @param tableName 表名称
*/
void truncateTableData(String schemaName, String tableName);
/**
* 删除指定物理表
*
* @param schemaName 模式名称
* @param tableName 表名称
*/
void dropTable(String schemaName, String tableName);
/**
* 清除指定表的所有数据
*
* @param schemaName 模式名称
* @param tableName 表名称
*/
void truncateTableData(String schemaName, String tableName);
/**
* 删除指定物理表
*
* @param schemaName 模式名称
* @param tableName 表名称
*/
void dropTable(String schemaName, String tableName);
}

View File

@@ -9,56 +9,57 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database.impl;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* DB2数据库实现类
*
* @author tang
*
* @author tang
*/
public class DB2DatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
public DB2DatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public DB2DatabaseOperator(DataSource dataSource) {
super(dataSource);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName, tableName);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName, StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName,
StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName,
List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName, tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" immediate ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" immediate ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -10,18 +10,16 @@
package com.gitee.dbswitch.dbcommon.database.impl;
import javax.sql.DataSource;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
/**
* DM数据库实现类
*
* @author tang
*
*/
public class DmDatabaseOperator extends OracleDatabaseOperator implements IDatabaseOperator {
public class DmDatabaseOperator extends OracleDatabaseOperator {
public DmDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public DmDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
}

View File

@@ -13,13 +13,12 @@ import javax.sql.DataSource;
/**
* Greenplum数据库实现类
*
* @author tang
*
* @author tang
*/
public class GreenplumDatabaseOperator extends PostgreSqlDatabaseOperator {
public GreenplumDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public GreenplumDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
}

View File

@@ -13,15 +13,14 @@ package com.gitee.dbswitch.dbcommon.database.impl;
* Kingbase8数据库实现类
*
* @author tang
*
*/
import javax.sql.DataSource;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
public class KingbaseDatabaseOperator extends PostgreSqlDatabaseOperator implements IDatabaseOperator {
public class KingbaseDatabaseOperator extends PostgreSqlDatabaseOperator {
public KingbaseDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public KingbaseDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
}

View File

@@ -9,54 +9,56 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database.impl;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* MySQL数据库实现类
*
* @author tang
*
* @author tang
*/
public class MysqlDatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
public MysqlDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public MysqlDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select `%s` from `%s`.`%s` ", StringUtils.join(fields, "`,`"), schemaName, tableName);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select `%s` from `%s`.`%s` ",
StringUtils.join(fields, "`,`"), schemaName, tableName);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select `%s` from `%s`.`%s` order by `%s` asc ", StringUtils.join(fields, "`,`"),
schemaName, tableName, StringUtils.join(orders, "`,`"));
return this.selectTableData(sql, Integer.MIN_VALUE);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select `%s` from `%s`.`%s` order by `%s` asc ",
StringUtils.join(fields, "`,`"),
schemaName, tableName, StringUtils.join(orders, "`,`"));
return this.selectTableData(sql, Integer.MIN_VALUE);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields) {
String sql = String.format("select `%s` from `%s`.`%s` ", StringUtils.join(fields, "`,`"), schemaName,
tableName);
return this.selectTableData(sql, Integer.MIN_VALUE);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName,
List<String> fields) {
String sql = String.format("select `%s` from `%s`.`%s` ",
StringUtils.join(fields, "`,`"), schemaName, tableName);
return this.selectTableData(sql, Integer.MIN_VALUE);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE `%s`.`%s` ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE `%s`.`%s` ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE `%s`.`%s` ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE `%s`.`%s` ", schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -9,55 +9,56 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database.impl;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* Oracle数据库实现类
*
* @author tang
*
* @author tang
*/
public class OracleDatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
public OracleDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public OracleDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName, tableName);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName, StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName,
StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName,
List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName, tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" ", schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -9,55 +9,61 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database.impl;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* PostgreSQL数据库实现类
*
* @author tang
*
* @author tang
*/
public class PostgreSqlDatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
public class PostgreSqlDatabaseOperator extends AbstractDatabaseOperator implements
IDatabaseOperator {
public PostgreSqlDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public PostgreSqlDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName,
tableName);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName, StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" order by \"%s\" asc ",
StringUtils.join(fields, "\",\""), schemaName, tableName,
StringUtils.join(orders, "\",\""));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ", StringUtils.join(fields, "\",\""), schemaName,
tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName,
List<String> fields) {
String sql = String.format("select \"%s\" from \"%s\".\"%s\" ",
StringUtils.join(fields, "\",\""), schemaName,
tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" RESTART IDENTITY ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE \"%s\".\"%s\" RESTART IDENTITY ",
schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" cascade ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE \"%s\".\"%s\" cascade ",
schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -9,54 +9,57 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.database.impl;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.domain.StatementResultSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import com.gitee.dbswitch.dbcommon.database.AbstractDatabaseOperator;
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
import com.gitee.dbswitch.dbcommon.pojo.StatementResultSet;
/**
* SQLServer数据库实现类
*
* @author tang
*
* @author tang
*/
public class SqlServerDatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
public class SqlServerDatabaseOperator extends AbstractDatabaseOperator implements
IDatabaseOperator {
public SqlServerDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
public SqlServerDatabaseOperator(DataSource dataSource) {
super(dataSource);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select [%s] from [%s].[%s] ", StringUtils.join(fields, "],["), schemaName, tableName);
}
@Override
public String getSelectTableSql(String schemaName, String tableName, List<String> fields) {
return String.format("select [%s] from [%s].[%s] ",
StringUtils.join(fields, "],["), schemaName, tableName);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select [%s] from [%s].[%s] order by [%s] asc ", StringUtils.join(fields, "],["),
schemaName, tableName, StringUtils.join(orders, "],["));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields,
List<String> orders) {
String sql = String.format("select [%s] from [%s].[%s] order by [%s] asc ",
StringUtils.join(fields, "],["),
schemaName, tableName, StringUtils.join(orders, "],["));
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName, List<String> fields) {
String sql = String.format("select [%s] from [%s].[%s] ", StringUtils.join(fields, "],["), schemaName,
tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public StatementResultSet queryTableData(String schemaName, String tableName,
List<String> fields) {
String sql = String.format("select [%s] from [%s].[%s] ",
StringUtils.join(fields, "],["), schemaName, tableName);
return this.selectTableData(sql, this.fetchSize);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE [%s].[%s] ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE [%s].[%s] ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void truncateTableData(String schemaName, String tableName) {
String sql = String.format("TRUNCATE TABLE [%s].[%s] ", schemaName, tableName);
this.executeSql(sql);
}
@Override
public void dropTable(String schemaName, String tableName) {
String sql = String.format("DROP TABLE [%s].[%s] ", schemaName, tableName);
this.executeSql(sql);
}
}

View File

@@ -7,40 +7,40 @@
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.pojo;
package com.gitee.dbswitch.dbcommon.domain;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.jdbc.support.JdbcUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.support.JdbcUtils;
/**
* JDBC连接及结果集实体参数定义类
*
* @author tang
*
* @author tang
*/
@Slf4j
@Data
public class StatementResultSet implements AutoCloseable {
private boolean isAutoCommit;
private Connection connection;
private Statement statement;
private ResultSet resultset;
@Override
public void close() {
try {
connection.setAutoCommit(isAutoCommit);
} catch (SQLException e) {
log.warn("Jdbc Connect setAutoCommit() failed, error: {}",e.getMessage());
}
JdbcUtils.closeResultSet(resultset);
JdbcUtils.closeStatement(statement);
JdbcUtils.closeConnection(connection);
}
private boolean isAutoCommit;
private Connection connection;
private Statement statement;
private ResultSet resultset;
@Override
public void close() {
try {
connection.setAutoCommit(isAutoCommit);
} catch (SQLException e) {
log.warn("Jdbc Connect setAutoCommit() failed, error: {}", e.getMessage());
}
JdbcUtils.closeResultSet(resultset);
JdbcUtils.closeStatement(statement);
JdbcUtils.closeConnection(connection);
}
}

View File

@@ -16,56 +16,45 @@ import org.springframework.jdbc.support.MetaDataAccessException;
/**
* 数据库类型识别工具类
*
* @author tang
*
* @author tang
*/
public final class DatabaseAwareUtils {
/**
* 私有化构造函数,阻断继承
*/
private DatabaseAwareUtils() {
}
/**
* 私有化构造函数,阻断继承
*/
private DatabaseAwareUtils() {
}
/**
* 获取数据库的产品名
*
* @param dataSource 数据源
* @return 数据库产品名称字符串
*/
public static final String getDatabaseNameByDataSource(DataSource dataSource) {
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName").toString());
if (productName.equalsIgnoreCase("Greenplum")) {
return "greenplum";
} else if (productName.equalsIgnoreCase("Microsoft SQL Server")) {
return "sqlserver";
} else if (productName.equalsIgnoreCase("KingbaseES")) {
return "kingbase";
} else if (productName.equalsIgnoreCase("DM DBMS")) {
return "dm";
}
DatabaseDriver databaseDriver = DatabaseDriver.fromProductName(productName);
if (databaseDriver == DatabaseDriver.UNKNOWN) {
throw new IllegalStateException("Unable to detect database type from data source instance");
}
return databaseDriver.getId();
} catch (MetaDataAccessException ex) {
throw new IllegalStateException("Unable to detect database type", ex);
}
/**
* 获取数据库的产品名
*
* @param dataSource 数据源
* @return 数据库产品名称字符串
*/
public static final String getDatabaseNameByDataSource(DataSource dataSource) {
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName").toString());
if (productName.equalsIgnoreCase("Greenplum")) {
return "greenplum";
} else if (productName.equalsIgnoreCase("Microsoft SQL Server")) {
return "sqlserver";
} else if (productName.equalsIgnoreCase("KingbaseES")) {
return "kingbase";
} else if (productName.equalsIgnoreCase("DM DBMS")) {
return "dm";
}
}
DatabaseDriver databaseDriver = DatabaseDriver.fromProductName(productName);
if (databaseDriver == DatabaseDriver.UNKNOWN) {
throw new IllegalStateException("Unable to detect database type from data source instance");
}
return databaseDriver.getId();
} catch (MetaDataAccessException ex) {
throw new IllegalStateException("Unable to detect database type", ex);
}
/**
* 判断数据源是否为MySQL数据源
*
* @param dataSource 数据源
* @return 当数据源为MySQL时返回true否则为false
*/
public static final boolean isUsingMySQL(DataSource dataSource) {
return getDatabaseNameByDataSource(dataSource).equalsIgnoreCase("mysql");
}
}
}

View File

@@ -12,81 +12,67 @@ package com.gitee.dbswitch.dbcommon.util;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.sql.DataSource;
import org.springframework.jdbc.support.JdbcUtils;
/**
* 基于JDBC的数据库元数据查询工具类
*
* @author tang
*
* @author tang
*/
public class JdbcMetaDataUtils {
protected DataSource dataSource;
protected DataSource dataSource;
public JdbcMetaDataUtils(DataSource dataSource) {
this.dataSource = Objects.requireNonNull(dataSource);
}
public JdbcMetaDataUtils(DataSource dataSource) {
this.dataSource = Objects.requireNonNull(dataSource);
}
/**
* 获取指定模式表的字段列表
*
* @param schemaName 模式名称
* @param tableName 表名称
* @return 字段元信息列表
*/
public List<String> queryTableColumnName(String schemaName, String tableName) {
Set<String> result = new HashSet<String>();
Connection connection = null;
/**
* 获取指定模式表的字段列表
*
* @param schemaName 模式名称
* @param tableName 表名称
* @return 字段元信息列表
*/
public List<String> queryTableColumnName(String schemaName, String tableName) {
Set<String> result = new HashSet<>();
try (Connection connection = this.dataSource.getConnection();) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet columns = metaData.getColumns(null, schemaName, tableName, null);
while (columns.next()) {
result.add(columns.getString("COLUMN_NAME"));
}
return new ArrayList<>(result);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
try {
connection = this.dataSource.getConnection();
DatabaseMetaData medaData = connection.getMetaData();
ResultSet columns = medaData.getColumns(null, schemaName, tableName, null);
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
result.add(columnName);
}
return new ArrayList<>(result);
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
JdbcUtils.closeConnection(connection);
}
}
/**
* 获取指定模式表的主键字段列表
*
* @param schemaName 模式名称
* @param tableName 表名称
* @return 主键字段名称列表
*/
public List<String> queryTablePrimaryKeys(String schemaName, String tableName) {
Set<String> result = new HashSet<>();
Connection connection = null;
try {
connection = this.dataSource.getConnection();
DatabaseMetaData medaData = connection.getMetaData();
ResultSet columns = medaData.getPrimaryKeys(null, schemaName, tableName);
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
result.add(columnName);
}
return new ArrayList<>(result);
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
JdbcUtils.closeConnection(connection);
}
}
/**
* 获取指定模式表的主键字段列表
*
* @param schemaName 模式名称
* @param tableName 表名称
* @return 主键字段名称列表
*/
public List<String> queryTablePrimaryKeys(String schemaName, String tableName) {
Set<String> result = new HashSet<>();
try (Connection connection = this.dataSource.getConnection();) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet columns = metaData.getPrimaryKeys(null, schemaName, tableName);
while (columns.next()) {
result.add(columns.getString("COLUMN_NAME"));
}
return new ArrayList<>(result);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,142 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.dbcommon.util;
import java.lang.reflect.Field;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
/**
* JDBC的数据类型相关工具类
*
* @author tang
*/
public final class JdbcTypesUtils {
private static final Map<Integer, String> TYPE_NAMES = new HashMap<>();
static {
try {
for (Field field : Types.class.getFields()) {
TYPE_NAMES.put((Integer) field.get(null), field.getName());
}
} catch (Exception ex) {
throw new IllegalStateException("Failed to resolve JDBC Types constants", ex);
}
}
/**
* 将JDBC的整型类型转换成文本类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return JDBC的文本类型
*/
public static String resolveTypeName(int sqlType) {
return TYPE_NAMES.get(sqlType);
}
/**
* 判断是否为JDCB的浮点数类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isNumeric(int sqlType) {
// 5
return (Types.DECIMAL == sqlType || Types.DOUBLE == sqlType || Types.FLOAT == sqlType
|| Types.NUMERIC == sqlType || Types.REAL == sqlType);
}
/**
* 判断是否为JDCB的整型类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isInteger(int sqlType) {
// 5
return (Types.BIT == sqlType || Types.BIGINT == sqlType || Types.INTEGER == sqlType
|| Types.SMALLINT == sqlType
|| Types.TINYINT == sqlType);
}
/**
* 判断是否为JDCB的字符文本类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isString(int sqlType) {
// 10
return (Types.CHAR == sqlType || Types.NCHAR == sqlType || Types.VARCHAR == sqlType
|| Types.LONGVARCHAR == sqlType || Types.NVARCHAR == sqlType
|| Types.LONGNVARCHAR == sqlType
|| Types.CLOB == sqlType || Types.NCLOB == sqlType || Types.SQLXML == sqlType
|| Types.ROWID == sqlType);
}
/**
* 判断是否为JDCB的时间类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isDateTime(int sqlType) {
// 5
return (Types.DATE == sqlType || Types.TIME == sqlType || Types.TIMESTAMP == sqlType
|| Types.TIME_WITH_TIMEZONE == sqlType || Types.TIMESTAMP_WITH_TIMEZONE == sqlType);
}
/**
* 判断是否为JDCB的布尔类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isBoolean(int sqlType) {
// 1
return (Types.BOOLEAN == sqlType);
}
/**
* 判断是否为JDCB的二进制类型
*
* @param sqlType jdbc的整型类型详见:{@codejava.sql.Types }
* @return true为是否则为false
*/
public static boolean isBinary(int sqlType) {
// 4
return (Types.BINARY == sqlType || Types.VARBINARY == sqlType || Types.BLOB == sqlType
|| Types.LONGVARBINARY == sqlType);
}
public static boolean isTextable(int sqlType) {
return isNumeric(sqlType) || isString(sqlType) || isDateTime(sqlType) || isBoolean(sqlType);
}
// 其他类型如下9个
// JAVA_OBJECT
// OTHER
// NULL
// DISTINCT
// STRUCT
// ARRAY
// REF
// DATALINK
// REF_CURSOR
/**
* 构造函数私有化
*/
private JdbcTypesUtils() {
}
}