代码逻辑梳理调整

This commit is contained in:
inrgihc
2023-07-26 21:53:50 +08:00
parent 8ea03521ef
commit 6cb3662b5c
37 changed files with 309 additions and 137 deletions

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.db2;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.DB2)
public class DB2FactoryProvider extends AbstractFactoryProvider {
public DB2FactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.DB2;
}
public ProductFeatures getProductFeatures() {
return new DB2Features();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.db2.DB2FactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.dm;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.DM)
public class DmFactoryProvider extends AbstractFactoryProvider {
public DmFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.DM;
}
public ProductFeatures getProductFeatures() {
return new DmFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.dm.DmFactoryProvider

View File

@@ -9,33 +9,29 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.gbase;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider;
import com.gitee.dbswitch.product.mysql.MysqlTableSynchronizer;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
import com.gitee.dbswitch.provider.meta.MetadataProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import javax.sql.DataSource;
@Product(ProductTypeEnum.GBASE8A)
public class GbaseFactoryProvider extends AbstractFactoryProvider {
public GbaseFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.GBASE8A;
}
public ProductFeatures getProductFeatures() {
return new GbaseFeatures();
}
@Override
public MetadataProvider createMetadataQueryProvider() {
return new MysqlMetadataQueryProvider(this);
return new GbaseMetadataQueryProvider(this);
}
@Override

View File

@@ -0,0 +1,35 @@
package com.gitee.dbswitch.product.gbase;
import com.gitee.dbswitch.common.util.JdbcUrlUtils;
import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
public class GbaseMetadataQueryProvider extends MysqlMetadataQueryProvider {
public GbaseMetadataQueryProvider(ProductFactoryProvider factoryProvider) {
super(factoryProvider);
}
@Override
public List<String> querySchemaList(Connection connection) {
try {
final Matcher matcher = JdbcUrlUtils
.getPattern("jdbc:gbase:://{host}[:{port}]/[{database}][\\?{params}]")
.matcher(connection.getMetaData().getURL());
if (matcher.matches()) {
return Collections.singletonList(matcher.group("database"));
}
throw new RuntimeException("get database name from jdbc url failed!");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (RuntimeException e) {
throw e;
}
}
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.gbase.GbaseFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.hive;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -16,17 +17,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider;
import com.gitee.dbswitch.provider.query.TableDataQueryProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.HIVE)
public class HiveFactoryProvider extends AbstractFactoryProvider {
public HiveFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.HIVE;
}
public ProductFeatures getProductFeatures() {
return new HiveFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.hive.HiveFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.kingbase;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.product.postgresql.PostgresTableOperateProvider;
@@ -20,17 +21,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.KINGBASE)
public class KingbaseFactoryProvider extends AbstractFactoryProvider {
public KingbaseFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.KINGBASE;
}
public ProductFeatures getProductFeatures() {
return new KingbaseFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.kingbase.KingbaseFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mariadb;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.product.mysql.MysqlMetadataQueryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import javax.sql.DataSource;
@Product(ProductTypeEnum.MARIADB)
public class MariadbFactoryProvider extends AbstractFactoryProvider {
public MariadbFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.MARIADB;
}
public ProductFeatures getProductFeatures() {
return new MariadbFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.mariadb.MariadbFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.mysql;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -16,17 +17,13 @@ import com.gitee.dbswitch.provider.meta.MetadataProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import javax.sql.DataSource;
@Product(ProductTypeEnum.MYSQL)
public class MysqlFactoryProvider extends AbstractFactoryProvider {
public MysqlFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.MYSQL;
}
public ProductFeatures getProductFeatures() {
return new MysqlFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.mysql.MysqlFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.oracle;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.ORACLE)
public class OracleFactoryProvider extends AbstractFactoryProvider {
public OracleFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.ORACLE;
}
public ProductFeatures getProductFeatures() {
return new OracleFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.oracle.OracleFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.oscar;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.OSCAR)
public class OscarFactoryProvider extends AbstractFactoryProvider {
public OscarFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.OSCAR;
}
public ProductFeatures getProductFeatures() {
return new OscarFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.oscar.OscarFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.postgresql;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.POSTGRESQL)
public class PostgresFactoryProvider extends AbstractFactoryProvider {
public PostgresFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.POSTGRESQL;
}
public ProductFeatures getProductFeatures() {
return new PostgresFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.postgresql.PostgresFactoryProvider

View File

@@ -1,55 +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.product.repository;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.product.db2.DB2FactoryProvider;
import com.gitee.dbswitch.product.dm.DmFactoryProvider;
import com.gitee.dbswitch.product.gbase.GbaseFactoryProvider;
import com.gitee.dbswitch.product.hive.HiveFactoryProvider;
import com.gitee.dbswitch.product.kingbase.KingbaseFactoryProvider;
import com.gitee.dbswitch.product.mariadb.MariadbFactoryProvider;
import com.gitee.dbswitch.product.mysql.MysqlFactoryProvider;
import com.gitee.dbswitch.product.oracle.OracleFactoryProvider;
import com.gitee.dbswitch.product.oscar.OscarFactoryProvider;
import com.gitee.dbswitch.product.postgresql.PostgresFactoryProvider;
import com.gitee.dbswitch.product.sqlite.SqliteFactoryProvider;
import com.gitee.dbswitch.product.sqlserver.SqlserverFactoryProvider;
import com.gitee.dbswitch.product.sybase.SybaseFactoryProvider;
import com.gitee.dbswitch.provider.ProductProviderFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
@ConditionalOnClass(ProductProviderFactory.class)
public class ProductRegisterAutoConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
log.info("Register database product now ...");
ProductProviderFactory.register(ProductTypeEnum.MYSQL, MysqlFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.ORACLE, OracleFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.POSTGRESQL, PostgresFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.SQLSERVER, SqlserverFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.DB2, DB2FactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.DM, DmFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.SYBASE, SybaseFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.KINGBASE, KingbaseFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.OSCAR, OscarFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.SQLITE3, SqliteFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.MARIADB, MariadbFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.GBASE8A, GbaseFactoryProvider.class.getName());
ProductProviderFactory.register(ProductTypeEnum.HIVE, HiveFactoryProvider.class.getName());
log.info("Finish to register database product ");
}
}

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.sqlite;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -18,17 +19,13 @@ import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import com.gitee.dbswitch.provider.write.TableDataWriteProvider;
import javax.sql.DataSource;
@Product(ProductTypeEnum.SQLITE3)
public class SqliteFactoryProvider extends AbstractFactoryProvider {
public SqliteFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.SQLITE3;
}
public ProductFeatures getProductFeatures() {
return new SqliteFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.sqlite.SqliteFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.sqlserver;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
@@ -17,17 +18,13 @@ import com.gitee.dbswitch.provider.operate.TableOperateProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import javax.sql.DataSource;
@Product(ProductTypeEnum.SQLSERVER)
public class SqlserverFactoryProvider extends AbstractFactoryProvider {
public SqlserverFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.SQLSERVER;
}
public ProductFeatures getProductFeatures() {
return new SqlserverFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.sqlserver.SqlserverFactoryProvider

View File

@@ -9,6 +9,7 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.product.sybase;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.features.ProductFeatures;
import com.gitee.dbswitch.product.sqlserver.SqlserverTableOperateProvider;
@@ -19,17 +20,13 @@ import com.gitee.dbswitch.provider.operate.TableOperateProvider;
import com.gitee.dbswitch.provider.sync.TableDataSynchronizer;
import javax.sql.DataSource;
@Product(ProductTypeEnum.SYBASE)
public class SybaseFactoryProvider extends AbstractFactoryProvider {
public SybaseFactoryProvider(DataSource dataSource) {
super(dataSource);
}
@Override
public ProductTypeEnum getProductType() {
return ProductTypeEnum.SYBASE;
}
public ProductFeatures getProductFeatures() {
return new SybaseFeatures();
}

View File

@@ -0,0 +1 @@
com.gitee.dbswitch.product.sybase.SybaseFactoryProvider

View File

@@ -77,6 +77,7 @@
<artifactId>dbswitch-product-gbase</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 新增加的数据库产品需要在这里追加依赖-->
</dependencies>
</project>

View File

@@ -0,0 +1,135 @@
// 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.repository;
import com.gitee.dbswitch.annotation.Product;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
import com.gitee.dbswitch.provider.ProductFactoryProvider;
import com.gitee.dbswitch.provider.ProductProviderFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
@ConditionalOnClass(ProductProviderFactory.class)
public class ProductRegisterAutoConfiguration implements InitializingBean, BeanClassLoaderAware {
private static final String SPI_FILE = "META-INF/services/dbswitch.providers";
private static final Set<String> providers = new HashSet<>();
private ClassLoader classLoader;
private int parseLine(BufferedReader reader, int lc, List<String> names)
throws IOException, ServiceConfigurationError {
String ln = reader.readLine();
if (ln == null) {
return -1;
}
int ci = ln.indexOf('#');
if (ci >= 0) {
ln = ln.substring(0, ci);
}
ln = ln.trim();
int n = ln.length();
if (n != 0) {
if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) {
log.error("Illegal configuration-file syntax: {}", ln);
}
int cp = ln.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp)) {
log.error("Illegal provider-class name: {}", ln);
}
for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
cp = ln.codePointAt(i);
if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) {
log.error("Illegal provider-class name: {}", ln);
}
}
if (!providers.contains(ln) && !names.contains(ln)) {
names.add(ln);
}
}
return lc + 1;
}
private List<String> parse(URL url) throws ServiceConfigurationError {
InputStream in = null;
BufferedReader reader = null;
ArrayList<String> names = new ArrayList<>();
try {
in = url.openStream();
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
int lc = 1;
while ((lc = parseLine(reader, lc, names)) >= 0) {
}
} catch (IOException x) {
log.error("Error reading configuration file", x);
} finally {
try {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
} catch (IOException y) {
log.error("Error closing configuration file", y);
}
}
return names;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("Register database product now ...");
ClassLoader loader = (null != classLoader)
? classLoader
: ProductProviderFactory.class.getClassLoader();
Enumeration<URL> resources = loader.getResources(SPI_FILE);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
providers.addAll(parse(url));
}
int totalCount = 0;
for (String className : providers) {
Class<?> aClass = classLoader.loadClass(className);
if (ProductFactoryProvider.class.isAssignableFrom(aClass)) {
if (aClass.isAnnotationPresent(Product.class)) {
Product annotation = aClass.getAnnotation(Product.class);
ProductTypeEnum productType = annotation.value();
if (null != productType) {
ProductProviderFactory.register(productType, className);
++totalCount;
}
}
}
}
log.info("Finish to register total {} database product !", totalCount);
}
}

View File

@@ -25,7 +25,7 @@
<module>dbswitch-product-mariadb</module>
<module>dbswitch-product-hive</module>
<module>dbswitch-product-gbase</module>
<module>dbswitch-product-repository</module>
<module>dbswitch-register-product</module>
</modules>
</project>