初始代码
This commit is contained in:
35
yunzhupaas-cloud-common/yunzhupaas-common-seata/pom.xml
Normal file
35
yunzhupaas-cloud-common/yunzhupaas-common-seata/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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>yunzhupaas-cloud-common</artifactId>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yunzhupaas-common-seata</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- seata -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.seata</groupId>
|
||||
<artifactId>seata-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.esotericsoftware</groupId>
|
||||
<artifactId>kryo</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.javakaffee</groupId>
|
||||
<artifactId>kryo-serializers</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,159 @@
|
||||
package io.seata.rm.datasource;
|
||||
|
||||
import io.seata.rm.datasource.sql.struct.Field;
|
||||
import io.seata.sqlparser.util.ColumnUtils;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* generate sql and set value to sql
|
||||
*
|
||||
* @author JerryYin
|
||||
*/
|
||||
public class SqlGenerateUtils {
|
||||
|
||||
private static final int MAX_IN_SIZE = 1000;
|
||||
|
||||
private SqlGenerateUtils() {
|
||||
|
||||
}
|
||||
|
||||
public static String buildWhereConditionByPKs(List<String> pkNameList, int rowSize, String dbType)
|
||||
throws SQLException {
|
||||
return "sqlserver".equals(dbType) ? buildWhereConditionByPKsBySqlServer(pkNameList, rowSize, dbType, MAX_IN_SIZE) : buildWhereConditionByPKs(pkNameList, rowSize, dbType, MAX_IN_SIZE);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* each pk is a condition.the result will like :" (id,userCode) in ((?,?),(?,?)) or (id,userCode) in ((?,?),(?,?)
|
||||
* ) or (id,userCode) in ((?,?))"
|
||||
* Build where condition by pks string.
|
||||
*
|
||||
* @param pkNameList pk column name list
|
||||
* @param rowSize the row size of records
|
||||
* @param dbType the type of database
|
||||
* @param maxInSize the max in size
|
||||
* @return return where condition sql string.the sql can search all related records not just one.
|
||||
* @throws SQLException the sql exception
|
||||
*/
|
||||
public static String buildWhereConditionByPKs(List<String> pkNameList, int rowSize, String dbType, int maxInSize)
|
||||
throws SQLException {
|
||||
StringBuilder whereStr = new StringBuilder();
|
||||
//we must consider the situation of composite primary key
|
||||
int batchSize = rowSize % maxInSize == 0 ? rowSize / maxInSize : (rowSize / maxInSize) + 1;
|
||||
for (int batch = 0; batch < batchSize; batch++) {
|
||||
if (batch > 0) {
|
||||
whereStr.append(" or ");
|
||||
}
|
||||
whereStr.append("(");
|
||||
for (int i = 0; i < pkNameList.size(); i++) {
|
||||
if (i > 0) {
|
||||
whereStr.append(",");
|
||||
}
|
||||
whereStr.append(ColumnUtils.addEscape(pkNameList.get(i), dbType));
|
||||
}
|
||||
whereStr.append(") in ( ");
|
||||
|
||||
int eachSize = (batch == batchSize - 1) ? (rowSize % maxInSize == 0 ? maxInSize : rowSize % maxInSize)
|
||||
: maxInSize;
|
||||
for (int i = 0; i < eachSize; i++) {
|
||||
//each row is a bracket
|
||||
if (i > 0) {
|
||||
whereStr.append(",");
|
||||
}
|
||||
whereStr.append("(");
|
||||
for (int x = 0; x < pkNameList.size(); x++) {
|
||||
if (x > 0) {
|
||||
whereStr.append(",");
|
||||
}
|
||||
whereStr.append("?");
|
||||
}
|
||||
whereStr.append(")");
|
||||
}
|
||||
whereStr.append(" )");
|
||||
}
|
||||
|
||||
return whereStr.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* each pk is a condition.the result will like :" (id = ? and userCode = ?) or (id = ? and userCode = ?)"
|
||||
* Build where condition by pks string.
|
||||
*
|
||||
* @param pkNameList pk column name list
|
||||
* @param rowSize the row size of records
|
||||
* @param dbType the type of database
|
||||
* @param maxInSize the max in size
|
||||
* @return return where condition sql string.the sql can search all related records not just one.
|
||||
* @throws SQLException the sql exception
|
||||
*/
|
||||
public static String buildWhereConditionByPKsBySqlServer(List<String> pkNameList, int rowSize, String dbType, int maxInSize)
|
||||
throws SQLException {
|
||||
StringBuilder whereStr = new StringBuilder();
|
||||
//we must consider the situation of composite primary key
|
||||
for (int batch = 0; batch < rowSize; batch++) {
|
||||
if (batch > 0) {
|
||||
whereStr.append(" or ");
|
||||
}
|
||||
whereStr.append("(");
|
||||
for (int i = 0; i < pkNameList.size(); i++) {
|
||||
if (i > 0) {
|
||||
whereStr.append(" and ");
|
||||
}
|
||||
whereStr.append(ColumnUtils.addEscape(pkNameList.get(i), dbType));
|
||||
whereStr.append(" = ? ");
|
||||
}
|
||||
whereStr.append(")");
|
||||
}
|
||||
|
||||
return whereStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* set parameter for PreparedStatement, this is only used in pk sql.
|
||||
*
|
||||
* @param pkRowsList pkRowsList
|
||||
* @param pkColumnNameList pkColumnNameList
|
||||
* @param pst preparedStatement
|
||||
* @throws SQLException SQLException
|
||||
*/
|
||||
public static void setParamForPk(List<Map<String, Field>> pkRowsList, List<String> pkColumnNameList,
|
||||
PreparedStatement pst) throws SQLException {
|
||||
int paramIndex = 1;
|
||||
for (int i = 0; i < pkRowsList.size(); i++) {
|
||||
Map<String, Field> rowData = pkRowsList.get(i);
|
||||
for (String columnName : pkColumnNameList) {
|
||||
Field pkField = rowData.get(columnName);
|
||||
pst.setObject(paramIndex, pkField.getValue(), pkField.getType());
|
||||
paramIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* each pk is a condition.the result will like :" id =? and userCode =?"
|
||||
*
|
||||
* @param pkNameList pkNameList
|
||||
* @param dbType dbType
|
||||
* @return return where condition sql string.the sql can just search one related record.
|
||||
*/
|
||||
public static String buildWhereConditionByPKs(List<String> pkNameList, String dbType) {
|
||||
StringBuilder whereStr = new StringBuilder();
|
||||
//we must consider the situation of composite primary key
|
||||
for (int i = 0; i < pkNameList.size(); i++) {
|
||||
if (i > 0) {
|
||||
whereStr.append(" and ");
|
||||
}
|
||||
String pkName = pkNameList.get(i);
|
||||
whereStr.append(ColumnUtils.addEscape(pkName, dbType));
|
||||
whereStr.append(" = ? ");
|
||||
}
|
||||
return whereStr.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user