mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-09-08 05:07:40 +00:00
dbswitch模块代码结构调整
This commit is contained in:
28
dbswitch-product/dbswitch-product-sqlite/pom.xml
Normal file
28
dbswitch-product/dbswitch-product-sqlite/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>dbswitch-product</artifactId>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<version>1.8.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>dbswitch-product-sqlite</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,56 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.common.type.ProductTypeEnum;
|
||||
import com.gitee.dbswitch.features.ProductFeatures;
|
||||
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.meta.MetadataProvider;
|
||||
import com.gitee.dbswitch.provider.operate.TableOperateProvider;
|
||||
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
|
||||
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
public class SqliteFactoryProvider extends AbstractFactoryProvider {
|
||||
|
||||
public SqliteFactoryProvider(DataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductTypeEnum getProductType() {
|
||||
return ProductTypeEnum.SQLITE3;
|
||||
}
|
||||
|
||||
public ProductFeatures getProductFeatures() {
|
||||
return new SqliteFeatures();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetadataProvider createMetadataQueryProvider() {
|
||||
return new SqliteMetadataQueryProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDataWriteProvider createTableDataWriteProvider(boolean useInsert) {
|
||||
return new SqliteTableDataWriteProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableOperateProvider createTableOperateProvider() {
|
||||
return new SqliteTableOperateProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDataSynchronizer createTableDataSynchronizer() {
|
||||
return new SqliteTableSynchronizer(this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.features.ProductFeatures;
|
||||
|
||||
public class SqliteFeatures implements ProductFeatures {
|
||||
|
||||
}
|
@@ -0,0 +1,170 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.common.consts.Constants;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.meta.AbstractMetadataProvider;
|
||||
import com.gitee.dbswitch.schema.ColumnDescription;
|
||||
import com.gitee.dbswitch.schema.ColumnMetaData;
|
||||
import com.gitee.dbswitch.schema.TableDescription;
|
||||
import com.gitee.dbswitch.common.util.DDLFormatterUtils;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class SqliteMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
|
||||
private static final String SHOW_CREATE_TABLE_SQL =
|
||||
"SELECT DBMS_METADATA.GET_DDL('TABLE','%s','%s') FROM DUAL ";
|
||||
private static final String SHOW_CREATE_VIEW_SQL =
|
||||
"SELECT DBMS_METADATA.GET_DDL('VIEW','%s','%s') FROM DUAL ";
|
||||
|
||||
public SqliteMetadataQueryProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> querySchemaList(Connection connection) {
|
||||
return Collections.singletonList("main");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableDDL(Connection connection, String schemaName, String tableName) {
|
||||
String sql = "SELECT sql FROM \"sqlite_master\" where type='table' and tbl_name=? ";
|
||||
try (PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||
ps.setString(1, tableName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs != null && rs.next()) {
|
||||
return DDLFormatterUtils.format(rs.getString(1));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getViewDDL(Connection connection, String schemaName, String tableName) {
|
||||
String sql = "SELECT sql FROM \"sqlite_master\" where type='view' and tbl_name=? ";
|
||||
try (PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||
ps.setString(1, tableName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs != null && rs.next()) {
|
||||
return DDLFormatterUtils.format(rs.getString(1));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ColumnDescription> querySelectSqlColumnMeta(Connection connection, String sql) {
|
||||
String querySQL = String.format(" %s LIMIT 0 ", sql.replace(";", ""));
|
||||
return this.getSelectSqlColumnMeta(connection, querySQL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTableFieldsQuerySQL(String schemaName, String tableName) {
|
||||
return String.format("SELECT * FROM \"%s\".\"%s\" ", schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testQuerySQL(Connection connection, String sql) {
|
||||
String testQuerySql = String.format("explain %s", sql.replace(";", ""));
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Execute sql :{}", testQuerySql);
|
||||
}
|
||||
try (Statement st = connection.createStatement()) {
|
||||
st.execute(testQuerySql);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFieldDefinition(ColumnMetaData v, List<String> pks, boolean useAutoInc,
|
||||
boolean addCr, boolean withRemarks) {
|
||||
String fieldname = v.getName();
|
||||
int length = v.getLength();
|
||||
int precision = v.getPrecision();
|
||||
int type = v.getType();
|
||||
|
||||
String retval = " \"" + fieldname + "\" ";
|
||||
|
||||
switch (type) {
|
||||
case ColumnMetaData.TYPE_TIMESTAMP:
|
||||
case ColumnMetaData.TYPE_TIME:
|
||||
case ColumnMetaData.TYPE_DATE:
|
||||
// sqlite中没有时间数据类型
|
||||
retval += "DATETIME";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_BOOLEAN:
|
||||
retval += "CHAR(1)";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_NUMBER:
|
||||
case ColumnMetaData.TYPE_INTEGER:
|
||||
case ColumnMetaData.TYPE_BIGNUMBER:
|
||||
if (null != pks && !pks.isEmpty() && pks.contains(fieldname)) {
|
||||
// 关键字 AUTOINCREMENT 只能⽤于整型(INTEGER)字段。
|
||||
if (useAutoInc) {
|
||||
retval += "INTEGER PRIMARY KEY AUTOINCREMENT";
|
||||
} else {
|
||||
retval += "BIGINT ";
|
||||
}
|
||||
} else {
|
||||
if (precision != 0 || length < 0 || length > 18) {
|
||||
retval += "NUMERIC";
|
||||
} else {
|
||||
retval += "INTEGER";
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ColumnMetaData.TYPE_STRING:
|
||||
if (length < 1 || length >= Constants.CLOB_LENGTH) {
|
||||
retval += "BLOB";
|
||||
} else {
|
||||
retval += "TEXT";
|
||||
}
|
||||
break;
|
||||
case ColumnMetaData.TYPE_BINARY:
|
||||
retval += "BLOB";
|
||||
break;
|
||||
default:
|
||||
retval += "TEXT";
|
||||
break;
|
||||
}
|
||||
|
||||
if (addCr) {
|
||||
retval += Constants.CR;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTableColumnCommentDefinition(TableDescription td,
|
||||
List<ColumnDescription> cds) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.common.util.ObjectCastUtils;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.write.DefaultTableDataWriteProvider;
|
||||
import java.util.List;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
public class SqliteTableDataWriteProvider extends DefaultTableDataWriteProvider {
|
||||
|
||||
public SqliteTableDataWriteProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TransactionDefinition getDefaultTransactionDefinition() {
|
||||
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
|
||||
definition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
|
||||
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
return definition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long write(List<String> fieldNames, List<Object[]> recordValues) {
|
||||
recordValues.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
try {
|
||||
row[i] = ObjectCastUtils.castByDetermine(row[i]);
|
||||
} catch (Exception e) {
|
||||
row[i] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return super.write(fieldNames, recordValues);
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.operate.DefaultTableOperateProvider;
|
||||
|
||||
public class SqliteTableOperateProvider extends DefaultTableOperateProvider {
|
||||
|
||||
public SqliteTableOperateProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
// 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.product.sqlite;
|
||||
|
||||
import com.gitee.dbswitch.common.util.TypeConvertUtils;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.sync.DefaultTableDataSynchronizer;
|
||||
import java.util.List;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
public class SqliteTableSynchronizer extends DefaultTableDataSynchronizer {
|
||||
|
||||
public SqliteTableSynchronizer(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TransactionDefinition getDefaultTransactionDefinition() {
|
||||
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
|
||||
definition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
|
||||
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
return definition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeInsert(List<Object[]> records) {
|
||||
records.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
try {
|
||||
row[i] = TypeConvertUtils.castByDetermine(row[i]);
|
||||
} catch (Exception e) {
|
||||
row[i] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return super.executeInsert(records);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long executeUpdate(List<Object[]> records) {
|
||||
records.parallelStream().forEach((Object[] row) -> {
|
||||
for (int i = 0; i < row.length; ++i) {
|
||||
try {
|
||||
row[i] = TypeConvertUtils.castByDetermine(row[i]);
|
||||
} catch (Exception e) {
|
||||
row[i] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return super.executeUpdate(records);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user