mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-10 05:59:09 +00:00
version for 1.6.11 : support SQLite
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-parent</artifactId>
|
||||
<version>1.6.10</version>
|
||||
<version>1.6.11</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dbswitch-dbcommon</artifactId>
|
||||
|
@@ -9,6 +9,7 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package com.gitee.dbswitch.dbcommon.database;
|
||||
|
||||
import com.gitee.dbswitch.common.type.DatabaseTypeEnum;
|
||||
import com.gitee.dbswitch.common.util.DatabaseAwareUtils;
|
||||
import com.gitee.dbswitch.dbcommon.database.impl.DB2DatabaseOperator;
|
||||
import com.gitee.dbswitch.dbcommon.database.impl.DmDatabaseOperator;
|
||||
@@ -19,6 +20,7 @@ import com.gitee.dbswitch.dbcommon.database.impl.MysqlDatabaseOperator;
|
||||
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.database.impl.SqliteDatabaseOperator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
@@ -31,22 +33,24 @@ import javax.sql.DataSource;
|
||||
*/
|
||||
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<DatabaseTypeEnum, Function<DataSource, IDatabaseOperator>> DATABASE_OPERATOR_MAPPER
|
||||
= new HashMap<DatabaseTypeEnum, Function<DataSource, IDatabaseOperator>>() {
|
||||
|
||||
private static final long serialVersionUID = -5278835613240515265L;
|
||||
|
||||
{
|
||||
put("MYSQL", MysqlDatabaseOperator::new);
|
||||
put("MARIADB", MysqlDatabaseOperator::new);
|
||||
put("ORACLE", OracleDatabaseOperator::new);
|
||||
put("SQLSERVER", SqlServerDatabaseOperator::new);
|
||||
put("SQLSERVER2000", SqlServerDatabaseOperator::new);
|
||||
put("POSTGRESQL", PostgreSqlDatabaseOperator::new);
|
||||
put("GREENPLUM", GreenplumDatabaseOperator::new);
|
||||
put("DB2", DB2DatabaseOperator::new);
|
||||
put("DM", DmDatabaseOperator::new);
|
||||
put("KINGBASE", KingbaseDatabaseOperator::new);
|
||||
put("HIVE", HiveDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.MYSQL, MysqlDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.MYSQL, MysqlDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.ORACLE, OracleDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.SQLSERVER, SqlServerDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.SQLSERVER2000, SqlServerDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.POSTGRESQL, PostgreSqlDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.GREENPLUM, GreenplumDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.DB2, DB2DatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.DM, DmDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.KINGBASE, KingbaseDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.HIVE, HiveDatabaseOperator::new);
|
||||
put(DatabaseTypeEnum.SQLITE3, SqliteDatabaseOperator::new);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -57,7 +61,7 @@ public final class DatabaseOperatorFactory {
|
||||
* @return 指定类型的数据库读取器
|
||||
*/
|
||||
public static IDatabaseOperator createDatabaseOperator(DataSource dataSource) {
|
||||
String type = DatabaseAwareUtils.getDatabaseTypeByDataSource(dataSource).name();
|
||||
DatabaseTypeEnum type = DatabaseAwareUtils.getDatabaseTypeByDataSource(dataSource);
|
||||
if (!DATABASE_OPERATOR_MAPPER.containsKey(type)) {
|
||||
throw new RuntimeException(
|
||||
String.format("[dbcommon] Unsupported database type (%s)", type));
|
||||
|
@@ -0,0 +1,72 @@
|
||||
// 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 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;
|
||||
|
||||
/**
|
||||
* SQLite数据库实现类
|
||||
*
|
||||
* @author tang
|
||||
*/
|
||||
public class SqliteDatabaseOperator extends AbstractDatabaseOperator implements IDatabaseOperator {
|
||||
|
||||
public SqliteDatabaseOperator(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 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 void truncateTableData(String schemaName, String tableName) {
|
||||
String sql = String.format("DELETE FROM \"%s\".\"%s\" ", schemaName, tableName);
|
||||
this.executeSql(sql);
|
||||
|
||||
try {
|
||||
sql = String.format("DELETE FROM sqlite_sequence WHERE name = '%s' ", tableName);
|
||||
this.executeSql(sql);
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String schemaName, String tableName) {
|
||||
String sql = String.format("DROP TABLE \"%s\".\"%s\" ", schemaName, tableName);
|
||||
this.executeSql(sql);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user