mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-10 05:59:09 +00:00
version for 1.6.3
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-parent</artifactId>
|
||||
<version>1.6.2</version>
|
||||
<version>1.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dbswitch-dbsynch</artifactId>
|
||||
|
@@ -19,6 +19,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.support.JdbcUtils;
|
||||
@@ -27,7 +28,6 @@ import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 数据同步抽象基类
|
||||
@@ -37,249 +37,256 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public abstract class AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
private DefaultTransactionDefinition defination;
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
private Map<String, Integer> columnType;
|
||||
protected List<String> fieldOrders;
|
||||
protected List<String> pksOrders;
|
||||
protected String insertStatementSql;
|
||||
protected String updateStatementSql;
|
||||
protected String deleteStatementSql;
|
||||
protected int[] insertArgsType;
|
||||
protected int[] updateArgsType;
|
||||
protected int[] deleteArgsType;
|
||||
private final DefaultTransactionDefinition defination;
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
private Map<String, Integer> columnType;
|
||||
protected List<String> fieldOrders;
|
||||
protected List<String> pksOrders;
|
||||
protected String insertStatementSql;
|
||||
protected String updateStatementSql;
|
||||
protected String deleteStatementSql;
|
||||
protected int[] insertArgsType;
|
||||
protected int[] updateArgsType;
|
||||
protected int[] deleteArgsType;
|
||||
|
||||
public AbstractDatabaseSynchronize(DataSource ds) {
|
||||
this.defination = new DefaultTransactionDefinition();
|
||||
this.defination.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
|
||||
this.defination.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
public AbstractDatabaseSynchronize(DataSource ds) {
|
||||
this.defination = new DefaultTransactionDefinition();
|
||||
this.defination.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
|
||||
this.defination.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
this.jdbcTemplate = new JdbcTemplate(ds);
|
||||
this.transactionManager = new DataSourceTransactionManager(ds);
|
||||
this.columnType = new HashMap<>();
|
||||
this.jdbcTemplate = new JdbcTemplate(ds);
|
||||
this.transactionManager = new DataSourceTransactionManager(ds);
|
||||
this.columnType = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getDataSource() {
|
||||
return this.jdbcTemplate.getDataSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询列元信息的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @return SQL语句
|
||||
*/
|
||||
public abstract String getColumnMetaDataSql(String schemaName, String tableName);
|
||||
|
||||
/**
|
||||
* 生成Insert操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param fieldNames 字段列表
|
||||
* @return Insert操作的SQL语句
|
||||
*/
|
||||
public abstract String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames);
|
||||
|
||||
/**
|
||||
* 生成Update操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param fieldNames 字段列表
|
||||
* @param pks 主键列表
|
||||
* @return Update操作的SQL语句
|
||||
*/
|
||||
public abstract String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks);
|
||||
|
||||
/**
|
||||
* 生成Delete操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param pks 主键列表
|
||||
* @return Delete操作的SQL语句
|
||||
*/
|
||||
public abstract String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks);
|
||||
|
||||
@Override
|
||||
public void prepare(String schemaName, String tableName, List<String> fieldNames,
|
||||
List<String> pks) {
|
||||
if (fieldNames.isEmpty() || pks.isEmpty() || fieldNames.size() < pks.size()) {
|
||||
throw new IllegalArgumentException("字段列表和主键列表不能为空,或者字段总个数应不小于主键总个数");
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getDataSource() {
|
||||
return this.jdbcTemplate.getDataSource();
|
||||
if (!fieldNames.containsAll(pks)) {
|
||||
throw new IllegalArgumentException("字段列表必须包含主键列表");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询列元信息的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @return SQL语句
|
||||
*/
|
||||
public abstract String getColumnMetaDataSql(String schemaName, String tableName);
|
||||
String sql = this.getColumnMetaDataSql(schemaName, tableName);
|
||||
columnType.clear();
|
||||
|
||||
/**
|
||||
* 生成Insert操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param fieldNames 字段列表
|
||||
* @return Insert操作的SQL语句
|
||||
*/
|
||||
public abstract String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames);
|
||||
|
||||
/**
|
||||
* 生成Update操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param fieldNames 字段列表
|
||||
* @param pks 主键列表
|
||||
* @return Update操作的SQL语句
|
||||
*/
|
||||
public abstract String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks);
|
||||
|
||||
/**
|
||||
* 生成Delete操作的SQL语句
|
||||
*
|
||||
* @param schemaName 模式名称
|
||||
* @param tableName 表名称
|
||||
* @param pks 主键列表
|
||||
* @return Delete操作的SQL语句
|
||||
*/
|
||||
public abstract String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks);
|
||||
|
||||
@Override
|
||||
public void prepare(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
if (fieldNames.isEmpty() || pks.isEmpty() || fieldNames.size() < pks.size()) {
|
||||
throw new IllegalArgumentException("字段列表和主键列表不能为空,或者字段总个数应不小于主键总个数");
|
||||
this.jdbcTemplate.execute((Connection conn) -> {
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(sql);
|
||||
ResultSetMetaData rsMetaData = rs.getMetaData();
|
||||
for (int i = 0, len = rsMetaData.getColumnCount(); i < len; i++) {
|
||||
columnType.put(rsMetaData.getColumnName(i + 1), rsMetaData.getColumnType(i + 1));
|
||||
}
|
||||
|
||||
if (!fieldNames.containsAll(pks)) {
|
||||
throw new IllegalArgumentException("字段列表必须包含主键列表");
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(
|
||||
String.format("获取表:%s.%s 的字段的元信息时失败. 请联系 DBA 核查该库、表信息.", schemaName, tableName), e);
|
||||
} finally {
|
||||
JdbcUtils.closeResultSet(rs);
|
||||
JdbcUtils.closeStatement(stmt);
|
||||
}
|
||||
});
|
||||
|
||||
String sql = this.getColumnMetaDataSql(schemaName, tableName);
|
||||
columnType.clear();
|
||||
this.fieldOrders = new ArrayList<String>(fieldNames);
|
||||
this.pksOrders = new ArrayList<String>(pks);
|
||||
|
||||
this.jdbcTemplate.execute((Connection conn) -> {
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(sql);
|
||||
ResultSetMetaData rsMetaData = rs.getMetaData();
|
||||
for (int i = 0, len = rsMetaData.getColumnCount(); i < len; i++) {
|
||||
columnType.put(rsMetaData.getColumnName(i + 1), rsMetaData.getColumnType(i + 1));
|
||||
}
|
||||
this.insertStatementSql = this.getInsertPrepareStatementSql(schemaName, tableName, fieldNames);
|
||||
this.updateStatementSql = this
|
||||
.getUpdatePrepareStatementSql(schemaName, tableName, fieldNames, pks);
|
||||
this.deleteStatementSql = this.getDeletePrepareStatementSql(schemaName, tableName, pks);
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(String.format("获取表:%s.%s 的字段的元信息时失败. 请联系 DBA 核查该库、表信息.", schemaName, tableName), e);
|
||||
} finally {
|
||||
JdbcUtils.closeResultSet(rs);
|
||||
JdbcUtils.closeStatement(stmt);
|
||||
}
|
||||
});
|
||||
|
||||
this.fieldOrders = new ArrayList<String>(fieldNames);
|
||||
this.pksOrders = new ArrayList<String>(pks);
|
||||
|
||||
this.insertStatementSql = this.getInsertPrepareStatementSql(schemaName, tableName, fieldNames);
|
||||
this.updateStatementSql = this.getUpdatePrepareStatementSql(schemaName, tableName, fieldNames, pks);
|
||||
this.deleteStatementSql = this.getDeletePrepareStatementSql(schemaName, tableName, pks);
|
||||
|
||||
insertArgsType = new int[fieldNames.size()];
|
||||
for (int k = 0; k < fieldNames.size(); ++k) {
|
||||
String field = fieldNames.get(k);
|
||||
insertArgsType[k] = this.columnType.get(field);
|
||||
}
|
||||
|
||||
updateArgsType = new int[fieldNames.size()];
|
||||
int idx = 0;
|
||||
for (int i = 0; i < fieldNames.size(); ++i) {
|
||||
String field = fieldNames.get(i);
|
||||
if (!pks.contains(field)) {
|
||||
updateArgsType[idx++] = this.columnType.get(field);
|
||||
}
|
||||
}
|
||||
for (String pk : pks) {
|
||||
updateArgsType[idx++] = this.columnType.get(pk);
|
||||
}
|
||||
|
||||
deleteArgsType = new int[pks.size()];
|
||||
for (int j = 0; j < pks.size(); ++j) {
|
||||
String pk = pks.get(j);
|
||||
deleteArgsType[j] = this.columnType.get(pk);
|
||||
}
|
||||
insertArgsType = new int[fieldNames.size()];
|
||||
for (int k = 0; k < fieldNames.size(); ++k) {
|
||||
String field = fieldNames.get(k);
|
||||
insertArgsType[k] = this.columnType.get(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeInsert(List<Object[]> records) {
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Insert SQL : {}", this.insertStatementSql);
|
||||
}
|
||||
|
||||
try {
|
||||
int[] affects = jdbcTemplate.batchUpdate(this.insertStatementSql, records, this.insertArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
}
|
||||
updateArgsType = new int[fieldNames.size()];
|
||||
int idx = 0;
|
||||
for (int i = 0; i < fieldNames.size(); ++i) {
|
||||
String field = fieldNames.get(i);
|
||||
if (!pks.contains(field)) {
|
||||
updateArgsType[idx++] = this.columnType.get(field);
|
||||
}
|
||||
}
|
||||
for (String pk : pks) {
|
||||
updateArgsType[idx++] = this.columnType.get(pk);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeUpdate(List<Object[]> records) {
|
||||
List<Object[]> datas = new LinkedList<>();
|
||||
for (Object[] r : records) {
|
||||
deleteArgsType = new int[pks.size()];
|
||||
for (int j = 0; j < pks.size(); ++j) {
|
||||
String pk = pks.get(j);
|
||||
deleteArgsType[j] = this.columnType.get(pk);
|
||||
}
|
||||
}
|
||||
|
||||
Object[] nr = new Object[this.fieldOrders.size()];
|
||||
int idx = 0;
|
||||
|
||||
for (int i = 0; i < this.fieldOrders.size(); ++i) {
|
||||
String field = this.fieldOrders.get(i);
|
||||
if (!this.pksOrders.contains(field)) {
|
||||
int index = this.fieldOrders.indexOf(field);
|
||||
nr[idx++] = r[index];
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < this.pksOrders.size(); ++j) {
|
||||
String pk = this.pksOrders.get(j);
|
||||
int index = this.fieldOrders.indexOf(pk);
|
||||
nr[idx++] = r[index];
|
||||
}
|
||||
|
||||
datas.add(nr);
|
||||
}
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Update SQL : {}", this.updateStatementSql);
|
||||
}
|
||||
|
||||
try {
|
||||
int[] affects = jdbcTemplate.batchUpdate(this.updateStatementSql, datas, this.updateArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
}
|
||||
@Override
|
||||
public long executeInsert(List<Object[]> records) {
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Insert SQL : {}", this.insertStatementSql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeDelete(List<Object[]> records) {
|
||||
List<Object[]> datas = new LinkedList<>();
|
||||
for (Object[] r : records) {
|
||||
Object[] nr = new Object[this.pksOrders.size()];
|
||||
for (int i = 0; i < this.pksOrders.size(); ++i) {
|
||||
String pk = this.pksOrders.get(i);
|
||||
int index = this.fieldOrders.indexOf(pk);
|
||||
nr[i] = r[index];
|
||||
}
|
||||
try {
|
||||
int[] affects = jdbcTemplate
|
||||
.batchUpdate(this.insertStatementSql, records, this.insertArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
datas.add(nr);
|
||||
}
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Delete SQL : {}", this.deleteStatementSql);
|
||||
}
|
||||
|
||||
try {
|
||||
int[] affects = jdbcTemplate.batchUpdate(this.deleteStatementSql, datas, this.deleteArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} finally {
|
||||
datas.clear();
|
||||
}
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeUpdate(List<Object[]> records) {
|
||||
List<Object[]> datas = new LinkedList<>();
|
||||
for (Object[] r : records) {
|
||||
|
||||
Object[] nr = new Object[this.fieldOrders.size()];
|
||||
int idx = 0;
|
||||
|
||||
for (int i = 0; i < this.fieldOrders.size(); ++i) {
|
||||
String field = this.fieldOrders.get(i);
|
||||
if (!this.pksOrders.contains(field)) {
|
||||
int index = this.fieldOrders.indexOf(field);
|
||||
nr[idx++] = r[index];
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < this.pksOrders.size(); ++j) {
|
||||
String pk = this.pksOrders.get(j);
|
||||
int index = this.fieldOrders.indexOf(pk);
|
||||
nr[idx++] = r[index];
|
||||
}
|
||||
|
||||
datas.add(nr);
|
||||
}
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Update SQL : {}", this.updateStatementSql);
|
||||
}
|
||||
|
||||
try {
|
||||
int[] affects = jdbcTemplate.batchUpdate(this.updateStatementSql, datas, this.updateArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeDelete(List<Object[]> records) {
|
||||
List<Object[]> datas = new LinkedList<>();
|
||||
for (Object[] r : records) {
|
||||
Object[] nr = new Object[this.pksOrders.size()];
|
||||
for (int i = 0; i < this.pksOrders.size(); ++i) {
|
||||
String pk = this.pksOrders.get(i);
|
||||
int index = this.fieldOrders.indexOf(pk);
|
||||
nr[i] = r[index];
|
||||
}
|
||||
|
||||
datas.add(nr);
|
||||
}
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(defination);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute Delete SQL : {}", this.deleteStatementSql);
|
||||
}
|
||||
|
||||
try {
|
||||
int[] affects = jdbcTemplate.batchUpdate(this.deleteStatementSql, datas, this.deleteArgsType);
|
||||
int affectCount = 0;
|
||||
for (int i : affects) {
|
||||
affectCount += i;
|
||||
}
|
||||
|
||||
transactionManager.commit(status);
|
||||
return affectCount;
|
||||
} catch (TransactionException e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
} finally {
|
||||
datas.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -9,19 +9,19 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbsynch;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbcommon.util.DatabaseAwareUtils;
|
||||
import com.gitee.dbswitch.dbsynch.db2.DB2DatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.dm.DmDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.kingbase.KingbaseDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.mssql.SqlServerDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.mysql.MySqlDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.oracle.OracleDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.GreenplumDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.PostgresqlDatabaseSynchImpl;
|
||||
import com.gitee.dbswitch.dbsynch.db2.DB2DatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.dm.DmDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.kingbase.KingbaseDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.mssql.SqlServerDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.mysql.MySqlDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.oracle.OracleDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.GreenplumDatabaseSyncImpl;
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.PostgresqlDatabaseSyncImpl;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 数据库同步器构造工厂类
|
||||
@@ -30,35 +30,35 @@ import java.util.function.Function;
|
||||
*/
|
||||
public final class DatabaseSynchronizeFactory {
|
||||
|
||||
private static final Map<String, Function<DataSource, IDatabaseSynchronize>> DATABASE_SYNCH_MAPPER = new HashMap<String, Function<DataSource, IDatabaseSynchronize>>() {
|
||||
private static final Map<String, Function<DataSource, IDatabaseSynchronize>> DATABASE_SYNC_MAPPER = new HashMap<String, Function<DataSource, IDatabaseSynchronize>>() {
|
||||
|
||||
private static final long serialVersionUID = -2359773637275934408L;
|
||||
private static final long serialVersionUID = -2359773637275934408L;
|
||||
|
||||
{
|
||||
put("MYSQL", MySqlDatabaseSynchImpl::new);
|
||||
put("ORACLE", OracleDatabaseSynchImpl::new);
|
||||
put("SQLSERVER", SqlServerDatabaseSynchImpl::new);
|
||||
put("POSTGRESQL", PostgresqlDatabaseSynchImpl::new);
|
||||
put("GREENPLUM", GreenplumDatabaseSynchImpl::new);
|
||||
put("DB2", DB2DatabaseSynchImpl::new);
|
||||
put("DM", DmDatabaseSynchImpl::new);
|
||||
put("KINGBASE", KingbaseDatabaseSynchImpl::new);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取指定数据源的同步器
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @return 同步器对象
|
||||
*/
|
||||
public static IDatabaseSynchronize createDatabaseWriter(DataSource dataSource) {
|
||||
String type = DatabaseAwareUtils.getDatabaseNameByDataSource(dataSource).toUpperCase();
|
||||
|
||||
if (!DATABASE_SYNCH_MAPPER.containsKey(type)) {
|
||||
throw new RuntimeException(String.format("[dbsynch] Unkown Supported database type (%s)", type));
|
||||
}
|
||||
|
||||
return DATABASE_SYNCH_MAPPER.get(type).apply(dataSource);
|
||||
{
|
||||
put("MYSQL", MySqlDatabaseSyncImpl::new);
|
||||
put("ORACLE", OracleDatabaseSyncImpl::new);
|
||||
put("SQLSERVER", SqlServerDatabaseSyncImpl::new);
|
||||
put("POSTGRESQL", PostgresqlDatabaseSyncImpl::new);
|
||||
put("GREENPLUM", GreenplumDatabaseSyncImpl::new);
|
||||
put("DB2", DB2DatabaseSyncImpl::new);
|
||||
put("DM", DmDatabaseSyncImpl::new);
|
||||
put("KINGBASE", KingbaseDatabaseSyncImpl::new);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取指定数据源的同步器
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @return 同步器对象
|
||||
*/
|
||||
public static IDatabaseSynchronize createDatabaseWriter(DataSource dataSource) {
|
||||
String type = DatabaseAwareUtils.getDatabaseNameByDataSource(dataSource).toUpperCase();
|
||||
if (!DATABASE_SYNC_MAPPER.containsKey(type)) {
|
||||
throw new RuntimeException(
|
||||
String.format("[dbsynch] Unknown Supported database type (%s)", type));
|
||||
}
|
||||
|
||||
return DATABASE_SYNC_MAPPER.get(type).apply(dataSource);
|
||||
}
|
||||
}
|
||||
|
@@ -14,50 +14,49 @@ import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 数据同步接口定义
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public interface IDatabaseSynchronize {
|
||||
|
||||
/**
|
||||
* 获取数据源对象
|
||||
*
|
||||
* @return DataSource数据源对象
|
||||
*/
|
||||
DataSource getDataSource();
|
||||
/**
|
||||
* 获取数据源对象
|
||||
*
|
||||
* @return DataSource数据源对象
|
||||
*/
|
||||
DataSource getDataSource();
|
||||
|
||||
/**
|
||||
* 批量Insert/Update/Delete预处理
|
||||
*
|
||||
* @param schemaName schema名称
|
||||
* @param tableName table名称
|
||||
* @param fieldNames 字段列表
|
||||
* @param pks 主键字段列表
|
||||
*/
|
||||
void prepare(String schemaName, String tableName, List<String> fieldNames, List<String> pks);
|
||||
/**
|
||||
* 批量Insert/Update/Delete预处理
|
||||
*
|
||||
* @param schemaName schema名称
|
||||
* @param tableName table名称
|
||||
* @param fieldNames 字段列表
|
||||
* @param pks 主键字段列表
|
||||
*/
|
||||
void prepare(String schemaName, String tableName, List<String> fieldNames, List<String> pks);
|
||||
|
||||
/**
|
||||
* 批量数据Insert
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeInsert(List<Object[]> records);
|
||||
/**
|
||||
* 批量数据Insert
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeInsert(List<Object[]> records);
|
||||
|
||||
/**
|
||||
* 批量数据Update
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeUpdate(List<Object[]> records);
|
||||
/**
|
||||
* 批量数据Update
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeUpdate(List<Object[]> records);
|
||||
|
||||
/**
|
||||
* 批量数据Delete
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeDelete(List<Object[]> records);
|
||||
/**
|
||||
* 批量数据Delete
|
||||
*
|
||||
* @param records 数据记录
|
||||
* @return 返回实际影响的记录条数
|
||||
*/
|
||||
long executeDelete(List<Object[]> records);
|
||||
}
|
||||
|
@@ -0,0 +1,75 @@
|
||||
// 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.dbsynch.db2;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* DB2数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class DB2DatabaseSyncImpl extends AbstractDatabaseSynchronize implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public DB2DatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )",
|
||||
schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""),
|
||||
StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s",
|
||||
schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ",
|
||||
schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// 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.dbsynch.db2;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
|
||||
/**
|
||||
* DB2数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class DB2DatabaseSynchImpl extends AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
public DB2DatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s", schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ", schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -9,18 +9,18 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbsynch.dm;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.oracle.OracleDatabaseSyncImpl;
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbsynch.oracle.OracleDatabaseSynchImpl;
|
||||
|
||||
/**
|
||||
* DM数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class DmDatabaseSynchImpl extends OracleDatabaseSynchImpl {
|
||||
public class DmDatabaseSyncImpl extends OracleDatabaseSyncImpl {
|
||||
|
||||
public DmDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
public DmDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
}
|
@@ -9,19 +9,18 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbsynch.kingbase;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.PostgresqlDatabaseSyncImpl;
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbsynch.pgsql.PostgresqlDatabaseSynchImpl;
|
||||
|
||||
/**
|
||||
* kingbase8数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
*/
|
||||
public class KingbaseDatabaseSynchImpl extends PostgresqlDatabaseSynchImpl {
|
||||
public class KingbaseDatabaseSyncImpl extends PostgresqlDatabaseSyncImpl {
|
||||
|
||||
public KingbaseDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
public KingbaseDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
// 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.dbsynch.mssql;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* SQLServer数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class SqlServerDatabaseSyncImpl extends AbstractDatabaseSynchronize implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public SqlServerDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM [%s].[%s] WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO [%s].[%s] ( [%s] ) VALUES ( %s )",
|
||||
schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "],["),
|
||||
StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("[%s]=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("[%s]=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE [%s].[%s] SET %s WHERE %s",
|
||||
schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("[%s]=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM [%s].[%s] WHERE %s ",
|
||||
schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// 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.dbsynch.mssql;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
|
||||
/**
|
||||
* SQLServer数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class SqlServerDatabaseSynchImpl extends AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
public SqlServerDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM [%s].[%s] WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO [%s].[%s] ( [%s] ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "],["), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("[%s]=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("[%s]=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE [%s].[%s] SET %s WHERE %s", schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("[%s]=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM [%s].[%s] WHERE %s ", schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
// 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.dbsynch.mysql;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* MySQL数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class MySqlDatabaseSyncImpl extends AbstractDatabaseSynchronize implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public MySqlDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM `%s`.`%s` WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO `%s`.`%s` ( `%s` ) VALUES ( %s )",
|
||||
schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "`,`"),
|
||||
StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("`%s`=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("`%s`=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE `%s`.`%s` SET %s WHERE %s",
|
||||
schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("`%s`=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM `%s`.`%s` WHERE %s ",
|
||||
schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// 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.dbsynch.mysql;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
|
||||
/**
|
||||
* MySQL数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class MySqlDatabaseSynchImpl extends AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
public MySqlDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM `%s`.`%s` WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO `%s`.`%s` ( `%s` ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "`,`"), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("`%s`=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("`%s`=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE `%s`.`%s` SET %s WHERE %s", schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("`%s`=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM `%s`.`%s` WHERE %s ", schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
// 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.dbsynch.oracle;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Oracle数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class OracleDatabaseSyncImpl extends AbstractDatabaseSynchronize implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public OracleDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )",
|
||||
schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""),
|
||||
StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s",
|
||||
schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ",
|
||||
schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// 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.dbsynch.oracle;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
|
||||
/**
|
||||
* Oracle数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class OracleDatabaseSynchImpl extends AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
public OracleDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s", schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ", schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -9,19 +9,19 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbsynch.pgsql;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Greenplum数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class GreenplumDatabaseSynchImpl extends PostgresqlDatabaseSynchImpl implements IDatabaseSynchronize {
|
||||
public class GreenplumDatabaseSyncImpl extends PostgresqlDatabaseSyncImpl implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public GreenplumDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
public GreenplumDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
// 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.dbsynch.pgsql;
|
||||
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* PostgreSQL数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class PostgresqlDatabaseSyncImpl extends AbstractDatabaseSynchronize implements
|
||||
IDatabaseSynchronize {
|
||||
|
||||
public PostgresqlDatabaseSyncImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String
|
||||
.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s", schemaName, tableName,
|
||||
StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName,
|
||||
List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ", schemaName, tableName,
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
// 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.dbsynch.pgsql;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.gitee.dbswitch.dbsynch.AbstractDatabaseSynchronize;
|
||||
import com.gitee.dbswitch.dbsynch.IDatabaseSynchronize;
|
||||
|
||||
/**
|
||||
* PostgreSQL数据库DML同步实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class PostgresqlDatabaseSynchImpl extends AbstractDatabaseSynchronize implements IDatabaseSynchronize {
|
||||
|
||||
public PostgresqlDatabaseSynchImpl(DataSource ds) {
|
||||
super(ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnMetaDataSql(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" WHERE 1=2", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInsertPrepareStatementSql(String schemaName, String tableName, List<String> fieldNames) {
|
||||
List<String> placeHolders = Collections.nCopies(fieldNames.size(), "?");
|
||||
return String.format("INSERT INTO \"%s\".\"%s\" ( \"%s\" ) VALUES ( %s )", schemaName, tableName,
|
||||
StringUtils.join(fieldNames, "\",\""), StringUtils.join(placeHolders, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUpdatePrepareStatementSql(String schemaName, String tableName, List<String> fieldNames, List<String> pks) {
|
||||
List<String> uf = fieldNames.stream()
|
||||
.filter(field -> !pks.contains(field))
|
||||
.map(field -> String.format("\"%s\"=?", field))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("UPDATE \"%s\".\"%s\" SET %s WHERE %s", schemaName, tableName, StringUtils.join(uf, " , "),
|
||||
StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeletePrepareStatementSql(String schemaName, String tableName, List<String> pks) {
|
||||
List<String> uw = pks.stream()
|
||||
.map(pk -> String.format("\"%s\"=?", pk))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return String.format("DELETE FROM \"%s\".\"%s\" WHERE %s ", schemaName, tableName, StringUtils.join(uw, " AND "));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user