Skip to content

Commit

Permalink
Merge branch '5.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
chenrenfei committed Jul 18, 2024
2 parents 71a82cd + f856fc7 commit 59ea88a
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 63 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ https://github.com/sagframe/sqltoy-online-doc/blob/master/docs/sqltoy/search.md
# 码云地址: https://gitee.com/sagacity/sagacity-sqltoy

# 最新版本
* 5.6.11 LTS (jdk17+/springboot3.x)/5.6.11.jre8 (兼容5.2.x/5.3.x版本) 发版日期: 2024-7-12
* 5.6.12 LTS (jdk17+/springboot3.x)/5.6.12.jre8 (兼容5.2.x/5.3.x版本) 发版日期: 2024-7-18
```xml
<dependency>
<groupId>com.sagframe</groupId>
<artifactId>sagacity-sqltoy-spring-starter</artifactId>
<!-- solon 适配版本 <artifactId>sagacity-sqltoy-solon-plugin</artifactId> -->
<!-- jdk8 对应的版本号为:5.6.11.jre8 -->
<version>5.6.11</version>
<!-- jdk8 对应的版本号为:5.6.12.jre8 -->
<version>5.6.12</version>
</dependency>
```
* 5.2.105 LTS (jdk1.8+) 发版日期: 2024-6-6
* 5.2.106 LTS (jdk1.8+) 发版日期: 2024-7-12

# 1. 前言
## 1.1 sqltoy-orm是什么
Expand Down
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.11</version>
<version>5.6.12</version>
<name>sagacity-sqltoy</name>
<description>sqltoy core code</description>
<artifactId>sagacity-sqltoy</artifactId>
Expand All @@ -14,13 +14,13 @@
<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.14.2</elastic-rest-client.version>
<fastjson.version>2.0.51</fastjson.version>
<elastic-rest-client.version>8.14.3</elastic-rest-client.version>
<fastjson.version>2.0.52</fastjson.version>
<mongo.version>3.12.14</mongo.version>
<junit-jupiter.version>5.10.3</junit-jupiter.version>
<junit-platform.version>1.10.3</junit-platform.version>
<slf4j.version>2.0.13</slf4j.version>
<h2.version>2.2.224</h2.version>
<h2.version>2.3.230</h2.version>
<lombok.version>1.18.34</lombok.version>
<druid.version>1.2.23</druid.version>
<transmittable-thread-local>2.14.5</transmittable-thread-local>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.sagacity.sqltoy.config.model.SqlExecuteLog;
import org.sagacity.sqltoy.config.model.SqlExecuteTrace;
import org.sagacity.sqltoy.model.OverTimeSql;
import org.sagacity.sqltoy.plugins.FirstBizCodeTrace;
import org.sagacity.sqltoy.plugins.OverTimeSqlHandler;
import org.sagacity.sqltoy.plugins.formater.SqlFormater;
import org.sagacity.sqltoy.utils.SqlUtil;
Expand Down Expand Up @@ -52,6 +53,11 @@ public class SqlExecuteStat {
// sql执行超时处理器
public static OverTimeSqlHandler overTimeSqlHandler;

/**
* 获取业务代码调用位置的实现类
*/
public static FirstBizCodeTrace firstBizCodeTrace;

/**
* sql格式化输出器(用于debug sql输出)
*/
Expand Down Expand Up @@ -312,34 +318,42 @@ private static String fitSqlParams(String sql, Object[] params) {
*/
public static String getFirstTrace() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (stackTraceElements == null || stackTraceElements.length == 0) {
return "未知类.未知方法[代码第:null行]";
}
StackTraceElement traceElement = null;
String className = null;
int lineNumber = 0;
String method = null;
StackTraceElement traceElement;
int length = stackTraceElements.length;
// 逆序
for (int i = length - 1; i > 0; i--) {
traceElement = stackTraceElements[i];
className = traceElement.getClassName();
// 进入调用sqltoy的代码,此时取上一个
if (className.startsWith("org.sagacity.sqltoy")) {
method = traceElement.getMethodName();
lineNumber = traceElement.getLineNumber();
// 避免异常发生
if (i + 1 < length) {
traceElement = stackTraceElements[i + 1];
//判断是否代理类,是代理类继续向上推一层
if ((i + 2) < length && Proxy.isProxyClass(traceElement.getClass())) {
traceElement = stackTraceElements[i + 2];
if (firstBizCodeTrace != null) {
traceElement = firstBizCodeTrace.getFirstTrace(stackTraceElements);
} else {
int length = stackTraceElements.length;
// 逆序
for (int i = length - 1; i > 0; i--) {
traceElement = stackTraceElements[i];
className = traceElement.getClassName();
// 进入调用sqltoy的代码,此时取上一个
if (className.startsWith(SqlToyConstants.SQLTOY_PACKAGE)) {
// 避免异常发生
if (i + 1 < length) {
traceElement = stackTraceElements[i + 1];
className = traceElement.getClassName();
// 判断是否代理类,找到非代理类、非主流框架类
int nextIndex = 2;
while ((i + nextIndex) < length && (Proxy.isProxyClass(traceElement.getClass())
|| className.startsWith("java.") || className.startsWith("sun.")
|| className.startsWith("com.sun.") || className.startsWith("org.springframework.")
|| className.startsWith("org.noear.solon."))) {
traceElement = stackTraceElements[i + nextIndex];
className = traceElement.getClassName();
nextIndex++;
}
}
className = traceElement.getClassName();
method = traceElement.getMethodName();
lineNumber = traceElement.getLineNumber();
break;
}
break;
}
}
return "" + className + "." + method + "[代码第:" + lineNumber + " 行]";
return traceElement.getClassName() + "." + traceElement.getMethodName() + "[代码第:" + traceElement.getLineNumber()
+ " 行]";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public class SqlToyConstants {
public final static String MERGE_ALIAS_ON_REGEX = "\\)\\s+tv\\s+on\\s+\\(";
public final static String MERGE_UPDATE = " when matched then update set ";
public final static String MERGE_INSERT = " when not matched then insert ";

/**
* sqltoy的框架包路径
*/
public final static String SQLTOY_PACKAGE="org.sagacity.sqltoy";

public static String localDateTimeFormat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.sagacity.sqltoy.model.OverTimeSql;
import org.sagacity.sqltoy.model.QueryExecutor;
import org.sagacity.sqltoy.plugins.FilterHandler;
import org.sagacity.sqltoy.plugins.FirstBizCodeTrace;
import org.sagacity.sqltoy.plugins.IUnifyFieldsHandler;
import org.sagacity.sqltoy.plugins.OverTimeSqlHandler;
import org.sagacity.sqltoy.plugins.SqlInterceptor;
Expand Down Expand Up @@ -398,6 +399,11 @@ public TranslateManager getTranslateManager() {
*/
private DialectDDLGenerator dialectDDLGenerator;

/**
* 自定义获取业务代码调用点
*/
private FirstBizCodeTrace firstBizCodeTrace;

/**
* @todo 初始化
* @throws Exception
Expand All @@ -411,6 +417,9 @@ public void initialize() throws Exception {
ReservedWordsUtil.put(reservedWords);
// 初始化方言对应的类别代码,避免线程安全
DataSourceUtils.initialize();
if (firstBizCodeTrace != null) {
SqlExecuteStat.firstBizCodeTrace = firstBizCodeTrace;
}
// 设置方言映射(默认OSCAR==>gaussdb)
if (dialectMap != null && !dialectMap.isEmpty()) {
DataSourceUtils.dialectMap = dialectMap;
Expand Down Expand Up @@ -1288,4 +1297,8 @@ public DialectDDLGenerator getDialectDDLGenerator() {
public void setDialectDDLGenerator(DialectDDLGenerator dialectDDLGenerator) {
this.dialectDDLGenerator = dialectDDLGenerator;
}

public void setFirstBizCodeTrace(FirstBizCodeTrace firstBizCodeTrace) {
this.firstBizCodeTrace = firstBizCodeTrace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public class EntityManager {
* id产生器的包路径(针对类名补充包路径,直接包含了包路径的除外)
*/
private static final String IdGeneratorPackage = "org.sagacity.sqltoy.plugins.id.impl.";
// 历史id主键策略的包路径,5.x版本开始剔除了兼容处理,2024-7-16日增加了兼容处理
private static final String IdGeneratorOldPackage = "org.sagacity.sqltoy.plugin.id.";

/**
* 扫描的包(意义不大,sqltoy已经改为在使用时自动加载)
Expand Down Expand Up @@ -780,6 +782,10 @@ private void processIdGenerator(SqlToyContext sqlToyContext, EntityMeta entityMe
} else {
String generator = IdGenerators.get(idGenerator.toLowerCase());
generator = (generator != null) ? IdGeneratorPackage.concat(generator) : idGenerator;
// 针对历史id策略包路径提供兼容处理:update 2024-07-16
if (generator.startsWith(IdGeneratorOldPackage)) {
generator = IdGeneratorPackage.concat(generator.substring(generator.lastIndexOf(".") + 1));
}
// 自定义(不依赖spring模式),用法在quickvo中配置例如:com.xxxx..CustomIdGenerator
try {
IdGenerator idGeneratorBean = (IdGenerator) Class.forName(generator).getDeclaredConstructor()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sagacity.sqltoy.plugins;

/**
* 提供获取业务代码的位置
*/
public interface FirstBizCodeTrace {
public StackTraceElement getFirstTrace(StackTraceElement[] stackTraceElements);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ public void put(TranslateConfigModel cacheConfig, String cacheName, String cache
// 缓存没有配置,自动创建缓存(不建议使用)
if (cache == null) {
ResourcePoolsBuilder resBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder();
// 堆内内存大小(20000条)
// 堆内内存大小(默认10000条)
resBuilder = resBuilder.heap((cacheConfig.getHeap() < 1) ? 1000 : cacheConfig.getHeap(),
EntryUnit.ENTRIES);
//offHeap 堆外内存
if (cacheConfig.getOffHeap() > 0) {
resBuilder = resBuilder.offheap(cacheConfig.getOffHeap(), MemoryUnit.MB);
}
//disk
if (cacheConfig.getDiskSize() > 0) {
resBuilder = resBuilder.disk(cacheConfig.getDiskSize(), MemoryUnit.MB, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
Expand Down Expand Up @@ -69,6 +69,7 @@ public static String doPost(SqlToyContext sqltoyContext, final String url, Strin
httpPost.setHeader("Connection", "close");
httpPost.setConfig(requestConfig);
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
if (StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password)) {
// 凭据提供器
Expand All @@ -91,14 +92,25 @@ public static String doPost(SqlToyContext sqltoyContext, final String url, Strin
((UrlEncodedFormEntity) httpEntity).setContentType(CONTENT_TYPE);
httpPost.setEntity(httpEntity);
}
HttpResponse response = client.execute(httpPost);
response = client.execute(httpPost);
// 返回结果
HttpEntity reponseEntity = response.getEntity();
if (reponseEntity != null) {
return EntityUtils.toString(reponseEntity, CHARSET);
}
} catch (Exception e) {
throw e;
} finally {
try {
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
} catch (Exception e) {

}
}
return null;
}
Expand Down Expand Up @@ -137,6 +149,7 @@ public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel no
String realUrl;
// 返回结果
HttpEntity reponseEntity = null;
String result = null;
// 使用elastic rest client(默认)
if (esConfig.getRestClient() != null) {
realUrl = wrapUrl(esConfig, nosqlConfig);
Expand All @@ -146,17 +159,25 @@ public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel no
}
// 默认采用post请求
RestClient restClient = null;
Response response;
try {
restClient = esConfig.getRestClient();
Request request = new Request(POST, realUrl);
request.setEntity(httpEntity);
Response response = restClient.performRequest(request);
response = restClient.performRequest(request);
reponseEntity = response.getEntity();
if (reponseEntity != null) {
result = EntityUtils.toString(reponseEntity, nosqlConfig.getCharset());
}
} catch (Exception e) {
throw e;
} finally {
if (restClient != null) {
restClient.close();
try {
if (restClient != null) {
restClient.close();
}
} catch (Exception e) {

}
}
} // 组织httpclient模式调用(此种模式不推荐使用)
Expand All @@ -180,6 +201,7 @@ public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel no
httpPost.setConfig(requestConfig);
}
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
if (StringUtil.isNotBlank(esConfig.getUsername()) && StringUtil.isNotBlank(esConfig.getPassword())) {
// 凭据提供器
Expand All @@ -191,22 +213,32 @@ public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel no
} else {
client = HttpClients.createDefault();
}
HttpResponse response = client.execute(httpPost);
response = client.execute(httpPost);
reponseEntity = response.getEntity();
if (reponseEntity != null) {
result = EntityUtils.toString(reponseEntity, nosqlConfig.getCharset());
}
} catch (Exception e) {
throw e;
}
}
String result = null;
if (reponseEntity != null) {
result = EntityUtils.toString(reponseEntity, nosqlConfig.getCharset());
if (sqltoyContext.isDebug()) {
logger.debug("result={}", result);
} finally {
try {
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
} catch (Exception e) {

}
}
}
if (StringUtil.isBlank(result)) {
return null;
}
if (sqltoyContext.isDebug()) {
logger.debug("result={}", result);
}
// 将结果转换为JSON对象
JSONObject json = JSON.parseObject(result);
// 存在错误
Expand Down
2 changes: 1 addition & 1 deletion trunk/sqltoy-orm-solon-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.sagframe</groupId>
<version>5.6.11</version>
<version>5.6.12</version>

<artifactId>sagacity-sqltoy-solon-plugin</artifactId>
<name>sagacity-sqltoy-solon-plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public class SqlToyContextProperties implements Serializable {
* 是否基于实体注解动态生成表结构
*/
private Boolean autoDDL;

/**
* 业务代码调用点获取
*/
private String firstBizCodeTrace;

/**
* @return the sqlResourcesDir
Expand Down Expand Up @@ -748,4 +753,12 @@ public void setDialectMap(Map<String, String> dialectMap) {
this.dialectMap = dialectMap;
}

public String getFirstBizCodeTrace() {
return firstBizCodeTrace;
}

public void setFirstBizCodeTrace(String firstBizCodeTrace) {
this.firstBizCodeTrace = firstBizCodeTrace;
}

}
Loading

0 comments on commit 59ea88a

Please sign in to comment.