mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-09 13:39:10 +00:00
代码优化与错误拼写修正
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.constant;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database;
|
||||
@@ -15,6 +15,7 @@ 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;
|
||||
@@ -22,94 +23,90 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 数据读取抽象基类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
@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 DataSource getDataSource() {
|
||||
return this.dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFetchSize() {
|
||||
return this.fetchSize;
|
||||
}
|
||||
@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 void setFetchSize(int size) {
|
||||
if (size < Constants.MINIMUM_FETCH_SIZE) {
|
||||
throw new IllegalArgumentException("设置的批量处理行数的大小fetchSize不得小于" + Constants.MINIMUM_FETCH_SIZE);
|
||||
}
|
||||
|
||||
this.fetchSize = size;
|
||||
}
|
||||
this.fetchSize = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 已经指定的查询SQL语句查询数据结果集
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @param sql 查询的SQL语句
|
||||
* @param fetchSize 批处理大小
|
||||
* @return 结果集包装对象
|
||||
*/
|
||||
protected final StatementResultSet selectTableData(String sql, int fetchSize) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Query table data 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);
|
||||
}
|
||||
}
|
||||
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 dataSource 数据源
|
||||
* @param sql 写SQL语句
|
||||
*/
|
||||
protected final int executeSql(String sql) {
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
/**
|
||||
* 执行写SQL操作
|
||||
*
|
||||
* @param sql 写SQL语句
|
||||
*/
|
||||
protected final int executeSql(String sql) {
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute sql :{}", sql);
|
||||
}
|
||||
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 = dataSource.getConnection();
|
||||
pstmt = connection.prepareStatement(sql);
|
||||
return pstmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
JdbcUtils.closeStatement(pstmt);
|
||||
JdbcUtils.closeConnection(connection);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,14 +4,14 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database;
|
||||
|
||||
import java.util.Map;
|
||||
import java.lang.reflect.Constructor;
|
||||
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;
|
||||
@@ -25,50 +25,40 @@ import com.gitee.dbswitch.dbcommon.util.DatabaseAwareUtils;
|
||||
|
||||
/**
|
||||
* 数据库操作器构造工厂类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public final class DatabaseOperatorFactory {
|
||||
|
||||
private static final Map<String, String> DATABASE_OPERATOR_MAPPER = new HashMap<String, String>() {
|
||||
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.class.getName());
|
||||
put("ORACLE", OracleDatabaseOperator.class.getName());
|
||||
put("SQLSERVER", SqlServerDatabaseOperator.class.getName());
|
||||
put("POSTGRESQL", PostgreSqlDatabaseOperator.class.getName());
|
||||
put("GREENPLUM", GreenplumDatabaseOperator.class.getName());
|
||||
put("DB2", DB2DatabaseOperator.class.getName());
|
||||
put("DM", DmDatabaseOperator.class.getName());
|
||||
put("KINGBASE", KingbaseDatabaseOperator.class.getName());
|
||||
}
|
||||
};
|
||||
{
|
||||
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)) {
|
||||
String className = DATABASE_OPERATOR_MAPPER.get(type);
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Class<?>[] paraTypes = { DataSource.class };
|
||||
Object[] paraValues = { dataSource };
|
||||
Constructor<?> cons = clazz.getConstructor(paraTypes);
|
||||
return (IDatabaseOperator) cons.newInstance(paraValues);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据数据源获取数据的读取操作器
|
||||
*
|
||||
* @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));
|
||||
}
|
||||
|
||||
throw new RuntimeException(String.format("[dbcommon] Unkown Supported database type (%s)", type));
|
||||
}
|
||||
return DATABASE_OPERATOR_MAPPER.get(type).apply(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -1,8 +1,23 @@
|
||||
// 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.database.impl;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
|
||||
|
||||
/**
|
||||
* DM数据库实现类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
*/
|
||||
public class DmDatabaseOperator extends OracleDatabaseOperator implements IDatabaseOperator {
|
||||
|
||||
public DmDatabaseOperator(DataSource dataSource) {
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -1,5 +1,20 @@
|
||||
// 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.database.impl;
|
||||
|
||||
/**
|
||||
* Kingbase8数据库实现类
|
||||
*
|
||||
* @author tang
|
||||
*
|
||||
*/
|
||||
import javax.sql.DataSource;
|
||||
import com.gitee.dbswitch.dbcommon.database.IDatabaseOperator;
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database.impl;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.pojo;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.util;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
//
|
||||
// Author: tang (inrgihc@126.com)
|
||||
// Data : 2020/1/2
|
||||
// Date : 2020/1/2
|
||||
// Location: beijing , china
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.util;
|
||||
@@ -53,7 +53,7 @@ public class JdbcMetaDataUtils {
|
||||
String columnName = columns.getString("COLUMN_NAME");
|
||||
result.add(columnName);
|
||||
}
|
||||
return new ArrayList<String>(result);
|
||||
return new ArrayList<>(result);
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
} finally {
|
||||
@@ -70,7 +70,7 @@ public class JdbcMetaDataUtils {
|
||||
* @return 主键字段名称列表
|
||||
*/
|
||||
public List<String> queryTablePrimaryKeys(String schemaName, String tableName) {
|
||||
Set<String> result = new HashSet<String>();
|
||||
Set<String> result = new HashSet<>();
|
||||
Connection connection = null;
|
||||
|
||||
try {
|
||||
@@ -81,7 +81,7 @@ public class JdbcMetaDataUtils {
|
||||
String columnName = columns.getString("COLUMN_NAME");
|
||||
result.add(columnName);
|
||||
}
|
||||
return new ArrayList<String>(result);
|
||||
return new ArrayList<>(result);
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
} finally {
|
||||
|
Reference in New Issue
Block a user