mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-11 05:19:00 +08:00
♻️ Refactoring code.优化发号器sql语句
This commit is contained in:
1 parent
c1680ad72b
commit
944a45ce9a
7 files changed
+195
-17
No files matched your search
@@ -23,5 +23,10 @@
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-core</artifactId>
|
||||
</dependency>
|
||||
<!--mybatis plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+9
-17
@@ -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();
|
||||
|
||||
+29
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -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);
|
||||
|
||||
}
|
||||
+75
@@ -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<SqlProvider> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,6 +35,11 @@
|
||||
<version>3.3.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.oracle.database.jdbc</groupId>
|
||||
<artifactId>ojdbc8</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mysql -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
|
||||
Reference in new issue
Block a user