mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-08-29 00:44:19 +00:00
相关代码整理
This commit is contained in:
@@ -6,6 +6,7 @@ body:
|
||||
attributes:
|
||||
label: dbswitch版本
|
||||
options:
|
||||
- "1.9.8"
|
||||
- "1.9.7"
|
||||
- "1.9.6"
|
||||
- "1.9.5"
|
||||
@@ -18,7 +19,7 @@ body:
|
||||
attributes:
|
||||
label: ISSUE类型
|
||||
options:
|
||||
- "BUG反馈"
|
||||
- "BUG"
|
||||
- "需求"
|
||||
- "咨询"
|
||||
validations:
|
||||
|
@@ -51,6 +51,7 @@
|
||||
├── dbswitch-product-sybase // -> sybase方言实现类
|
||||
├── dbswitch-product-hive // -> hive方言实现类
|
||||
├── dbswitch-product-sqlite // -> sqlite方言实现类
|
||||
├── dbswitch-product-greenplum // -> greenplum方言实现类
|
||||
├── dbswitch-product-clickhouse // -> clickhouse方言实现类
|
||||
├── dbswitch-product-mongodb // -> mongodb方言实现类
|
||||
├── dbswitch-product-elasticsearch // -> elasticsearch方言实现类
|
||||
|
@@ -80,7 +80,7 @@ jdbc连接地址:jdbc:gbase://172.17.2.10:5258/gbase
|
||||
jdbc驱动名称:com.gbase.jdbc.Driver
|
||||
```
|
||||
|
||||
**翰高HighGo数据库(可按PostgreSQL使用)**
|
||||
**翰高HighGo数据库**
|
||||
|
||||
```
|
||||
jdbc连接地址:jdbc:highgo://172.17.2.10:5866/highgo
|
||||
|
@@ -29,7 +29,7 @@ cd ${DOCKER_DBSWITCH_DIR} \
|
||||
# clean project
|
||||
cd $PROJECT_ROOT_DIR && sh docker-maven-clean.sh && cd -
|
||||
|
||||
# login and push docker image
|
||||
docker login -u inrgihc
|
||||
docker push inrgihc/dbswitch:${DBSWITCH_VERSION}
|
||||
# push docker image
|
||||
docker tag inrgihc/dbswitch:${DBSWITCH_VERSION} registry.cn-hangzhou.aliyuncs.com/inrgihc/dbswitch:${DBSWITCH_VERSION}
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/inrgihc/dbswitch:${DBSWITCH_VERSION}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
FROM openjdk:8-jre-alpine
|
||||
FROM registry.cn-hangzhou.aliyuncs.com/inrgihc/openjdk:8-jre-alpine
|
||||
|
||||
ENV TZ=Asia/Shanghai
|
||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||
|
@@ -10,13 +10,14 @@
|
||||
package com.gitee.dbswitch.common.util;
|
||||
|
||||
import com.gitee.dbswitch.common.type.ProductTypeEnum;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -87,17 +88,17 @@ public final class DatabaseAwareUtils {
|
||||
if (driverNameMap.containsKey(driverName)) {
|
||||
ProductTypeEnum productType = driverNameMap.get(driverName);
|
||||
if (productType == ProductTypeEnum.POSTGRESQL) {
|
||||
// String url = connection.getMetaData().getURL();
|
||||
// Set<ProductTypeEnum> excludes = Sets.immutableEnumSet(ProductTypeEnum.POSTGRESQL);
|
||||
// ProductTypeEnum pgLikeType = ProductTypeEnum.getProductType(url, excludes);
|
||||
// if (null != pgLikeType) {
|
||||
// return pgLikeType;
|
||||
// }
|
||||
if (DataSourceTypeUtils.isGreenplum(connection)) {
|
||||
if (ProductTypeUtils.isGreenplum(connection)) {
|
||||
return ProductTypeEnum.GREENPLUM;
|
||||
}
|
||||
String url = connection.getMetaData().getURL();
|
||||
Set<ProductTypeEnum> excludes = Sets.immutableEnumSet(ProductTypeEnum.POSTGRESQL, ProductTypeEnum.GREENPLUM);
|
||||
ProductTypeEnum pgLikeType = ProductTypeEnum.getProductType(url, excludes);
|
||||
if (null != pgLikeType) {
|
||||
return pgLikeType;
|
||||
}
|
||||
} else if (productType == ProductTypeEnum.MYSQL) {
|
||||
if (isStarRocks(connection)) {
|
||||
if (ProductTypeUtils.isStarRocks(connection)) {
|
||||
return ProductTypeEnum.STARROCKS;
|
||||
}
|
||||
}
|
||||
@@ -125,18 +126,6 @@ public final class DatabaseAwareUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isStarRocks(Connection connection) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
// 此查询语句是Starrocks查询be节点是否存活,可以用来判断是否是Starrocks数据源
|
||||
String sql = "SHOW BACKENDS";
|
||||
return statement.execute(sql);
|
||||
} catch (Exception sqlException) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Failed to execute sql :show backends, and guesses it is mysql datasource!");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查MySQL数据库表的存储引擎是否为Innodb
|
||||
|
@@ -1,18 +1,26 @@
|
||||
// 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.common.util;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public final class DataSourceTypeUtils {
|
||||
public final class ProductTypeUtils {
|
||||
|
||||
public static boolean isGreenplum(Connection connection) {
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("SELECT version()")) {
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
@@ -31,4 +39,18 @@ public final class DataSourceTypeUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isStarRocks(Connection connection) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
// 此查询语句是Starrocks查询be节点是否存活,可以用来判断是否是Starrocks数据源
|
||||
String sql = "SHOW BACKENDS";
|
||||
return statement.execute(sql);
|
||||
} catch (Exception sqlException) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Failed to execute sql :show backends, and guesses it is mysql datasource!");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -13,8 +13,8 @@ import cn.hutool.core.util.ClassLoaderUtil;
|
||||
import com.gitee.dbswitch.common.entity.CloseableDataSource;
|
||||
import com.gitee.dbswitch.common.entity.InvisibleDataSource;
|
||||
import com.gitee.dbswitch.common.entity.JarClassLoader;
|
||||
import com.gitee.dbswitch.common.util.DataSourceTypeUtils;
|
||||
import com.gitee.dbswitch.common.util.ExamineUtils;
|
||||
import com.gitee.dbswitch.common.util.ProductTypeUtils;
|
||||
import com.gitee.dbswitch.data.domain.WrapCommonDataSource;
|
||||
import com.gitee.dbswitch.data.domain.WrapHikariDataSource;
|
||||
import com.gitee.dbswitch.data.entity.SourceDataSourceProperties;
|
||||
@@ -22,13 +22,10 @@ import com.gitee.dbswitch.data.entity.TargetDataSourceProperties;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.net.URLClassLoader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import javax.sql.DataSource;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -135,7 +132,7 @@ public final class DataSourceUtils {
|
||||
// 如果是Greenplum数据库,这里需要关闭会话的查询优化器
|
||||
if (properties.getDriverClassName().contains("postgresql")) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
if (DataSourceTypeUtils.isGreenplum(connection)) {
|
||||
if (ProductTypeUtils.isGreenplum(connection)) {
|
||||
ds.setConnectionInitSql("set optimizer to 'off'");
|
||||
log.info("Greenplum: Close Optimizer now: set optimizer to 'off'");
|
||||
}
|
||||
@@ -228,20 +225,4 @@ public final class DataSourceUtils {
|
||||
return urlClassLoader;
|
||||
}
|
||||
|
||||
private static String executeStringReturnedSql(
|
||||
DataSource dataSource, String sql) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
try (ResultSet resultSet = statement.executeQuery(sql)) {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getString(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,40 +1,61 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-parent</artifactId>
|
||||
<version>1.9.8</version>
|
||||
</parent>
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.gitee.dbswitch</groupId>
|
||||
<artifactId>dbswitch-parent</artifactId>
|
||||
<version>1.9.8</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dbswitch-dist</artifactId>
|
||||
<artifactId>dbswitch-dist</artifactId>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<properties>
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
</properties>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>dbswitch-release-${project.version}</finalName>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
<descriptors>
|
||||
<descriptor>src/main/assembly/assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
<outputDirectory>${project.parent.basedir}/target/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>dbswitch-release-${project.version}</finalName>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
<descriptors>
|
||||
<descriptor>src/main/assembly/assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
<outputDirectory>${project.parent.basedir}/target/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@@ -1,21 +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.greenplum;
|
||||
|
||||
import com.gitee.dbswitch.product.postgresql.PostgresTableCopyWriteProvider;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
|
||||
|
||||
public class GreenPlumTableCopyWriteProvider extends PostgresTableCopyWriteProvider {
|
||||
|
||||
public GreenPlumTableCopyWriteProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
}
|
@@ -12,6 +12,8 @@ package com.gitee.dbswitch.product.greenplum;
|
||||
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.PostgresTableCopyWriteProvider;
|
||||
import com.gitee.dbswitch.product.postgresql.PostgresTableManageProvider;
|
||||
import com.gitee.dbswitch.provider.AbstractFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.manage.TableManageProvider;
|
||||
import com.gitee.dbswitch.provider.meta.MetadataProvider;
|
||||
@@ -39,14 +41,14 @@ public class GreenplumFactoryProvider extends AbstractFactoryProvider {
|
||||
|
||||
@Override
|
||||
public TableManageProvider createTableManageProvider() {
|
||||
return new GreenplumTableManageProvider(this);
|
||||
return new PostgresTableManageProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDataWriteProvider createTableDataWriteProvider(boolean useInsert) {
|
||||
return useInsert
|
||||
? new AutoCastTableDataWriteProvider(this)
|
||||
: new GreenPlumTableCopyWriteProvider(this);
|
||||
: new PostgresTableCopyWriteProvider(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -19,24 +19,19 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class GreenplumMetadataQueryProvider extends PostgresMetadataQueryProvider {
|
||||
|
||||
|
||||
public GreenplumMetadataQueryProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void postAppendCreateTableSql(StringBuilder builder, String tblComment, List<String> primaryKeys,
|
||||
Map<String, String> tblProperties) {
|
||||
// 有主键就优先使用主键作为分布键。
|
||||
if (Objects.nonNull(primaryKeys) && !primaryKeys.isEmpty()) {
|
||||
getPkOrDkAsString(builder, primaryKeys);
|
||||
String pk = getPrimaryKeyAsString(primaryKeys);
|
||||
builder.append("\n DISTRIBUTED BY (").append(pk).append(")");
|
||||
log.info("using primary key as distributed key");
|
||||
}
|
||||
}
|
||||
|
||||
private void getPkOrDkAsString(StringBuilder builder, List<String> primaryKeys) {
|
||||
String pk = getPrimaryKeyAsString(primaryKeys);
|
||||
builder.append("\n DISTRIBUTED BY (").append(pk).append(")");
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +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.greenplum;
|
||||
|
||||
import com.gitee.dbswitch.product.postgresql.PostgresTableManageProvider;
|
||||
import com.gitee.dbswitch.provider.ProductFactoryProvider;
|
||||
import com.gitee.dbswitch.provider.manage.DefaultTableManageProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class GreenplumTableManageProvider extends PostgresTableManageProvider {
|
||||
|
||||
public GreenplumTableManageProvider(ProductFactoryProvider factoryProvider) {
|
||||
super(factoryProvider);
|
||||
}
|
||||
|
||||
}
|
@@ -5,5 +5,5 @@ docker run -it --rm \
|
||||
-v ~/.m2:/opt/maven/localRepository \
|
||||
-v "$PWD":/usr/src/mymaven \
|
||||
-w /usr/src/mymaven \
|
||||
inrgihc/maven-aliyun:3.6.3-jdk-8 mvn clean package
|
||||
registry.cn-hangzhou.aliyuncs.com/inrgihc/maven-aliyun:3.6.3-jdk-8 mvn clean package
|
||||
|
||||
|
@@ -5,5 +5,5 @@ docker run -it --rm \
|
||||
-v ~/.m2:/opt/maven/localRepository \
|
||||
-v "$PWD":/usr/src/mymaven \
|
||||
-w /usr/src/mymaven \
|
||||
inrgihc/maven-aliyun:3.6.3-jdk-8 mvn clean
|
||||
registry.cn-hangzhou.aliyuncs.com/inrgihc/maven-aliyun:3.6.3-jdk-8 mvn clean
|
||||
|
||||
|
Reference in New Issue
Block a user