Skip to content

Commit

Permalink
Merge branch 'dev' into 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
chenrenfei committed Dec 12, 2024
2 parents 7e68b93 + 28e3429 commit 971e0e3
Show file tree
Hide file tree
Showing 24 changed files with 343 additions and 257 deletions.
8 changes: 4 additions & 4 deletions trunk/sqltoy-orm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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>
<groupId>com.sagframe</groupId>
<version>5.6.33</version>
<version>5.6.34</version>
<name>sagacity-sqltoy</name>
<description>sqltoy core code</description>
<artifactId>sagacity-sqltoy</artifactId>
Expand All @@ -14,15 +14,15 @@
<ehcache.version>3.10.8</ehcache.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient-core.version>4.4.16</httpclient-core.version>
<elastic-rest-client.version>8.16.0</elastic-rest-client.version>
<elastic-rest-client.version>8.16.1</elastic-rest-client.version>
<fastjson.version>2.0.53</fastjson.version>
<mongo.version>5.2.1</mongo.version>
<junit-jupiter.version>5.11.3</junit-jupiter.version>
<junit-platform.version>1.11.3</junit-platform.version>
<slf4j.version>2.0.16</slf4j.version>
<h2.version>2.3.232</h2.version>
<lombok.version>1.18.34</lombok.version>
<druid.version>1.2.23</druid.version>
<lombok.version>1.18.36</lombok.version>
<druid.version>1.2.24</druid.version>
<transmittable-thread-local>2.14.5</transmittable-thread-local>
</properties>
<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.sagacity.sqltoy.config.model.SqlExecuteLog;
import org.sagacity.sqltoy.config.model.SqlExecuteTrace;
import org.sagacity.sqltoy.model.OperateDetailType;
import org.sagacity.sqltoy.model.OverTimeSql;
import org.sagacity.sqltoy.plugins.FirstBizCodeTrace;
import org.sagacity.sqltoy.plugins.OverTimeSqlHandler;
Expand Down Expand Up @@ -70,11 +71,11 @@ public class SqlExecuteStat {
* @param type
* @param debugPrint
*/
public static void start(String sqlId, String type, Boolean debugPrint) {
public static void start(String sqlId, OperateDetailType type, Boolean debugPrint) {
threadLocal.set(new SqlExecuteTrace(sqlId, type, (debugPrint == null) ? debug : debugPrint.booleanValue()));
}

public static void start(String sqlId, String type, Long batchSize, Boolean debugPrint) {
public static void start(String sqlId, OperateDetailType type, Long batchSize, Boolean debugPrint) {
SqlExecuteTrace sqlExecuteTrace = new SqlExecuteTrace(sqlId, type,
(debugPrint == null) ? debug : debugPrint.booleanValue());
sqlExecuteTrace.setBatchSize(batchSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.sagacity.sqltoy;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
* @project sagacity-sqltoy
* @description sqltoy全局的线程值持有者(整合I18nThreadHolder和UnifyUpdateFieldsController)
* @author zhongxuchen
* @version v1.0, Date:2024-12-06
*/
public class SqlToyThreadDataHolder {
/**
* 当前语言,主要用于字典国际化处理
*/
private static ThreadLocal<String> i18nThreadLocal = new TransmittableThreadLocal<String>();

// 是否启用统一字段处理中修改行为(一些业务数据不需要强制对修改人、修改时间做强制覆盖)
private static ThreadLocal<Boolean> unifyUpdateFields = new TransmittableThreadLocal<Boolean>();

// 放入当前用户语言方言
public static void setLanguage(String locale) {
if (locale != null) {
i18nThreadLocal.set(locale);
}
}

public static String getLanguage() {
return i18nThreadLocal.get();
}

/**
* 取消统一更新字段处理
*/
public static void stopUnifyUpdate() {
unifyUpdateFields.set(true);
}

/**
* @TODO 判断是否关闭了统一更新字段
* @return
*/
public static boolean useUnifyFields() {
Boolean cancalUnify = unifyUpdateFields.get();
if (cancalUnify != null && cancalUnify) {
return false;
}
return true;
}

// 清除语言
public static void clearLanguage() {
i18nThreadLocal.remove();
i18nThreadLocal.set(null);
}

// 恢复统一更新字段处理
public static void resumeUnifyUpdate() {
unifyUpdateFields.remove();
unifyUpdateFields.set(null);
}

public static void clearAll() {
clearLanguage();
resumeUnifyUpdate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,16 @@ else if (null == paramsArg || paramsArg.length == 0) {
processBlank(sqlToyResult);
// 检查 like 对应参数部分,如果参数中不存在%符合则自动两边增加%
processLike(sqlToyResult);
// add update 2024-12-12 将@value(?) 参数值中不含?的提前完成处理
processValue(sqlToyResult, dialect, true);
// in 处理策略2012-7-10 进行了修改,提供参数preparedStatement.setObject()机制,并同时兼容
// 用具体数据替换 in (?)中问号的处理机制
processIn(sqlToyResult);
// 参数为null的处理策略(用null直接代替变量)
replaceNull(sqlToyResult, 0);
// update 2021-4-29 放在最后,避免参数值中存在?号
// 替换@value(?) 为参数对应的数值
processValue(sqlToyResult, dialect);
processValue(sqlToyResult, dialect, false);
// 将特殊字符替换回问号
if (isNamedArgs) {
sqlToyResult.setSql(sqlToyResult.getSql().replaceAll(questionMark, ARG_NAME));
Expand Down Expand Up @@ -737,8 +739,9 @@ private static void processBlank(SqlToyResult sqlToyResult) {
* @TODO 处理直接显示参数值:#[@value(:paramNamed) sql]
* @param sqlToyResult
* @param dialect
* @param hasNotArgRun
*/
private static void processValue(SqlToyResult sqlToyResult, String dialect) {
private static void processValue(SqlToyResult sqlToyResult, String dialect, boolean hasNotArgRun) {
if (null == sqlToyResult.getParamsValue() || sqlToyResult.getParamsValue().length == 0) {
return;
}
Expand All @@ -747,37 +750,48 @@ private static void processValue(SqlToyResult sqlToyResult, String dialect) {
Matcher m = VALUE_PATTERN.matcher(queryStr);
int index = 0;
int paramCnt = 0;
int valueCnt = 0;
int atValueCnt = 0;
List paramValueList = null;
Object paramValue = null;
String valueStr;
boolean hasArg;
// 跳过的@value(?) 数量
int skipAtValueCnt = 0;
while (m.find()) {
if (valueCnt == 0) {
if (atValueCnt == 0) {
paramValueList = CollectionUtil.arrayToList(sqlToyResult.getParamsValue());
}
index = m.start();
// @value(?)
if (m.group().contains(ARG_NAME)) {
paramCnt = StringUtil.matchCnt(queryStr.substring(0, index), ARG_NAME_PATTERN, 0);
// 用参数的值直接覆盖@value(:name)
paramValue = paramValueList.get(paramCnt - valueCnt);
paramValue = paramValueList.get(paramCnt - atValueCnt);
// update 2024-03-03 强化对数组、枚举、日期等类型的输出
valueStr = SqlUtil.toSqlString(paramValue, false);
// update 2021-11-13 加强@value对应值中存在函数,进行跨数据库适配
if (dialect != null && valueStr.contains("(") && valueStr.contains(")")) {
valueStr = FunctionUtils.getDialectSql(valueStr, dialect);
// 判断值中是否有问号,有问号放最后处理(有?提前处理会影响后续的参数查询和定位)
hasArg = valueStr.contains(ARG_NAME);
if (!hasArg || !hasNotArgRun) {
// update 2021-11-13 加强@value对应值中存在函数,进行跨数据库适配
if (dialect != null && valueStr.contains("(") && valueStr.contains(")")) {
valueStr = FunctionUtils.getDialectSql(valueStr, dialect);
}
// 将sql中第skipAtValueCnt + 1位置的@value替换成具体值,@value偏移量为6
sqlToyResult.setSql(StringUtil.replaceRegex(sqlToyResult.getSql(), VALUE_PATTERN,
Matcher.quoteReplacement(valueStr), skipAtValueCnt + 1, 6));
// 剔除参数@value(?) 对应的参数值
paramValueList.remove(paramCnt - atValueCnt);
atValueCnt++;
} else {
skipAtValueCnt++;
}
sqlToyResult
.setSql(sqlToyResult.getSql().replaceFirst(VALUE_REGEX, Matcher.quoteReplacement(valueStr)));
// 剔除参数@value(?) 对应的参数值
paramValueList.remove(paramCnt - valueCnt);
valueCnt++;
} // @value(null)
else {
sqlToyResult.setSql(sqlToyResult.getSql().replaceFirst(VALUE_REGEX, "null"));
sqlToyResult.setSql(
StringUtil.replaceRegex(sqlToyResult.getSql(), VALUE_PATTERN, "null", skipAtValueCnt + 1, 6));
}
}
if (valueCnt > 0) {
if (atValueCnt > 0) {
sqlToyResult.setParamsValue(paramValueList.toArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ public SqlToyConfig getSqlConfig(String sqlKey, SqlType sqlType, String dialect,
String sql = result.getSql();
// update 2024-09-19 增加@fast(@include("sqlId")) 场景,result.getSql()
// 已经剔除了@fast,导致再次解析时已经无法判断是@fast语句,所以需要拼接上@fast还原原本的sql
// SqlToyConfig.setSql(fastPreSql.concat(" (").concat(fastSql).concat(")").concat(fastTailSql)),剔除了@fast
// SqlToyConfig.setSql(fastPreSql.concat("
// (").concat(fastSql).concat(")").concat(fastTailSql)),剔除了@fast
if (result.isHasFast()) {
sql = result.getFastPreSql(null).concat(" @fast(").concat(result.getFastSql(null)).concat(") ")
.concat(result.getFastTailSql(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.sagacity.sqltoy.model.OperateDetailType;
import org.sagacity.sqltoy.utils.IdUtil;

/**
Expand All @@ -21,9 +22,9 @@ public class SqlExecuteTrace implements Serializable {
*/
private static final long serialVersionUID = 6050450953137017285L;

public SqlExecuteTrace(String id, String type, boolean isPrint) {
public SqlExecuteTrace(String id, OperateDetailType operateDetailType, boolean isPrint) {
this.id = id;
this.type = type;
this.operateDetailType = operateDetailType;
this.start = System.currentTimeMillis();
this.isPrint = isPrint;
// 不需要体现年月日
Expand Down Expand Up @@ -70,7 +71,7 @@ public boolean isPrint() {
/**
* sql执行的类别(分页查询\普通查询\修改操作)
*/
private String type;
private OperateDetailType operateDetailType;

/**
* 批量信息
Expand Down Expand Up @@ -119,14 +120,18 @@ public void setId(String id) {
* @return the type
*/
public String getType() {
return type;
return (operateDetailType == null) ? "" : operateDetailType.value();
}

public OperateDetailType getOperateDetailType() {
return operateDetailType;
}

/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
public void setOperateDetailType(OperateDetailType operateDetailType) {
this.operateDetailType = operateDetailType;
}

/**
Expand Down
Loading

0 comments on commit 971e0e3

Please sign in to comment.