diff --git a/pigx-common/pigx-common-sequence/pom.xml b/pigx-common/pigx-common-sequence/pom.xml index 4feb4a640..4350c8906 100755 --- a/pigx-common/pigx-common-sequence/pom.xml +++ b/pigx-common/pigx-common-sequence/pom.xml @@ -23,5 +23,10 @@ com.pig4cloud pigx-common-core + + + com.baomidou + mybatis-plus-extension + \ No newline at end of file diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java index c710adbe6..e3dee2afb 100755 --- a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/BaseDbHelper.java @@ -1,7 +1,10 @@ package com.pig4cloud.pigx.common.sequence.range.impl.db; import cn.hutool.core.util.RandomUtil; +import cn.hutool.extra.spring.SpringUtil; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; import com.pig4cloud.pigx.common.sequence.exception.SeqException; +import com.pig4cloud.pigx.common.sequence.range.impl.db.provider.SqlProviderFactory; import javax.sql.DataSource; import java.sql.*; @@ -13,21 +16,10 @@ import java.sql.*; */ abstract class BaseDbHelper { + private static final SqlProviderFactory SQL_PROVIDER_FACTORY = SpringUtil.getBean(SqlProviderFactory.class); + private static final long DELTA = 100000000L; - private final static String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS #tableName(" - + "id bigint(20) NOT NULL AUTO_INCREMENT," + "value bigint(20) NOT NULL," + "name varchar(32) NOT NULL," - + "gmt_create DATETIME NOT NULL," + "gmt_modified DATETIME NOT NULL," - + "PRIMARY KEY (`id`),UNIQUE uk_name (`name`)" + ")"; - - private final static String SQL_INSERT_RANGE = "INSERT IGNORE INTO #tableName(id,name,value,gmt_create,gmt_modified)" - + " VALUE(?,?,?,?,?)"; - - private final static String SQL_UPDATE_RANGE = "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND " - + "value=?"; - - private final static String SQL_SELECT_RANGE = "SELECT value FROM #tableName WHERE name=?"; - /** * 创建表 * @param dataSource DB来源 @@ -41,7 +33,7 @@ abstract class BaseDbHelper { try { conn = dataSource.getConnection(); stmt = conn.createStatement(); - stmt.executeUpdate(SQL_CREATE_TABLE.replace("#tableName", tableName)); + stmt.executeUpdate(SQL_PROVIDER_FACTORY.getCreateTableSql().replace("#tableName", tableName)); } catch (SQLException e) { throw new SeqException(e); @@ -66,7 +58,7 @@ abstract class BaseDbHelper { try { conn = dataSource.getConnection(); - stmt = conn.prepareStatement(SQL_INSERT_RANGE.replace("#tableName", tableName)); + stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getInsertRangeSql().replace("#tableName", tableName)); stmt.setLong(1, RandomUtil.randomLong()); stmt.setString(2, name); stmt.setLong(3, stepStart); @@ -99,7 +91,7 @@ abstract class BaseDbHelper { try { conn = dataSource.getConnection(); - stmt = conn.prepareStatement(SQL_UPDATE_RANGE.replace("#tableName", tableName)); + stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getUpdateRangeSql().replace("#tableName", tableName)); stmt.setLong(1, newValue); stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis())); stmt.setString(3, name); @@ -132,7 +124,7 @@ abstract class BaseDbHelper { try { conn = dataSource.getConnection(); - stmt = conn.prepareStatement(SQL_SELECT_RANGE.replace("#tableName", tableName)); + stmt = conn.prepareStatement(SQL_PROVIDER_FACTORY.getSelectRangeSql().replace("#tableName", tableName)); stmt.setString(1, name); rs = stmt.executeQuery(); diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java new file mode 100644 index 000000000..8f3b44cf2 --- /dev/null +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/MysqlSqlProvider.java @@ -0,0 +1,29 @@ +package com.pig4cloud.pigx.common.sequence.range.impl.db.provider; + +import com.baomidou.mybatisplus.annotation.DbType; +import org.springframework.stereotype.Component; + +/** + * 数据源提供者 + * + * @author lishangbu + * @date 2021/12/29 + */ +@Component +public class MysqlSqlProvider implements SqlProvider { + + /** + * 获取插入范围语句 + * @return + */ + @Override + public String getInsertRangeSql() { + return "INSERT IGNORE INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; + } + + @Override + public Boolean support(DbType dbType) { + return DbType.MYSQL.equals(dbType); + } + +} diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java new file mode 100644 index 000000000..1b3e5af61 --- /dev/null +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/OracleSqlProvider.java @@ -0,0 +1,25 @@ +package com.pig4cloud.pigx.common.sequence.range.impl.db.provider; + +import com.baomidou.mybatisplus.annotation.DbType; +import org.springframework.stereotype.Component; + +/** + * oracle数据源 + * + * @author lishangbu + * @date 2021/12/29 + */ +@Component +public class OracleSqlProvider implements SqlProvider { + + @Override + public String getInsertRangeSql() { + return "INSERT INTO #tableName(id,name,value,gmt_create,gmt_modified)" + " VALUE(?,?,?,?,?)"; + } + + @Override + public Boolean support(DbType dbType) { + return DbType.ORACLE.equals(dbType); + } + +} diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java new file mode 100644 index 000000000..3c28e52c6 --- /dev/null +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProvider.java @@ -0,0 +1,47 @@ +package com.pig4cloud.pigx.common.sequence.range.impl.db.provider; + +import com.baomidou.mybatisplus.annotation.DbType; + +/** + * 数据源提供者 + * + * @author lishangbu + * @date 2021/12/29 + */ +public interface SqlProvider { + + /** + * 获取建表语句 + * @return + */ + default String getCreateTableSql() { + return "CREATE TABLE IF NOT EXISTS #tableName(" + "id bigint(20) NOT NULL AUTO_INCREMENT," + + "value bigint(20) NOT NULL," + "name varchar(32) NOT NULL," + "gmt_create DATETIME NOT NULL," + + "gmt_modified DATETIME NOT NULL," + "PRIMARY KEY (`id`),UNIQUE uk_name (`name`)" + ")"; + } + + /** + * 获取插入范围语句 + * @return + */ + String getInsertRangeSql(); + + /** + * 获取更新范围语句 + * @return + */ + default String getUpdateRangeSql() { + return "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND " + "value=?"; + } + + /** + * 获取查询范围语句 + * @return + */ + default String getSelectRangeSql() { + return "SELECT value FROM #tableName WHERE name=?"; + } + + Boolean support(DbType dbType); + +} diff --git a/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java new file mode 100644 index 000000000..8961d4153 --- /dev/null +++ b/pigx-common/pigx-common-sequence/src/main/java/com/pig4cloud/pigx/common/sequence/range/impl/db/provider/SqlProviderFactory.java @@ -0,0 +1,75 @@ +package com.pig4cloud.pigx.common.sequence.range.impl.db.provider; + +import cn.hutool.db.DbUtil; +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * sql语句提供工厂 + * + * @author lishangbu + * @date 2021/12/29 + */ +@Lazy(value = false) +@Component +@RequiredArgsConstructor +public class SqlProviderFactory { + + private final List sqlProviders; + + private final DataSourceProperties dataSourceProperties; + + public SqlProvider getSqlProvider() { + String url = dataSourceProperties.getUrl(); + DbType dbType = JdbcUtils.getDbType(url); + for (SqlProvider sqlProvider : sqlProviders) { + if (sqlProvider.support(dbType)) { + return sqlProvider; + } + } + throw new UnsupportedOperationException("不支持的数据源"); + } + + /** + * 获取插入范围语句 + * @return 插入范围语句 + */ + public String getCreateTableSql() { + SqlProvider sqlProvider = getSqlProvider(); + return sqlProvider.getCreateTableSql(); + } + + /** + * 获取插入范围语句 + * @return 插入范围语句 + */ + public String getInsertRangeSql() { + SqlProvider sqlProvider = getSqlProvider(); + return sqlProvider.getInsertRangeSql(); + } + + /** + * 获取查询范围语句 + * @return + */ + public String getSelectRangeSql() { + SqlProvider sqlProvider = getSqlProvider(); + return sqlProvider.getSelectRangeSql(); + } + + /** + * 获取更新范围语句 + * @return 更新范围语句 + */ + public String getUpdateRangeSql() { + SqlProvider sqlProvider = getSqlProvider(); + return sqlProvider.getUpdateRangeSql(); + } + +} diff --git a/pigx-visual/pigx-report-platform/pom.xml b/pigx-visual/pigx-report-platform/pom.xml index 979d31c7a..fb2fa4e1b 100644 --- a/pigx-visual/pigx-report-platform/pom.xml +++ b/pigx-visual/pigx-report-platform/pom.xml @@ -35,6 +35,11 @@ 3.3.2 + + com.oracle.database.jdbc + ojdbc8 + + mysql