Skip to content

Commit 218a319

Browse files
author
wanghaun
committed
feature/20220629_fix fix 回表查询
1 parent c30305c commit 218a319

File tree

4 files changed

+36
-104
lines changed

4 files changed

+36
-104
lines changed

elasticsearch-engine-base/src/main/java/com/elasticsearch/engine/base/common/parse/sql/EsSqlQueryHelper.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import com.elasticsearch.engine.base.model.constant.CommonConstant;
88
import com.elasticsearch.engine.base.model.domain.BackDto;
99
import com.elasticsearch.engine.base.model.emenu.SqlParamParse;
10+
import com.elasticsearch.engine.base.model.exception.EsEngineExecuteException;
1011
import com.elasticsearch.engine.base.model.exception.EsEngineJpaExecuteException;
1112
import lombok.extern.slf4j.Slf4j;
1213
import net.sf.jsqlparser.JSQLParserException;
1314
import net.sf.jsqlparser.statement.select.Select;
1415
import org.apache.commons.collections4.CollectionUtils;
16+
import org.apache.commons.lang3.StringUtils;
1517
import org.aspectj.lang.ProceedingJoinPoint;
1618
import org.aspectj.lang.reflect.MethodSignature;
1719
import org.springframework.stereotype.Component;
@@ -36,6 +38,9 @@ public class EsSqlQueryHelper {
3638
@Resource
3739
private EsSqlExecuteHandler esSqlExecuteHandler;
3840

41+
@Resource
42+
private EsSqlQueryHelper esSqlQueryHelper;
43+
3944
/**
4045
* es aop 查询逻辑
4146
*
@@ -48,25 +53,40 @@ public Object esSqlQueryAopCommon(ProceedingJoinPoint pjp, BackDto backDto) thro
4853
MethodSignature signature = (MethodSignature) pjp.getSignature();
4954
Method method = signature.getMethod();
5055
Object[] args = pjp.getArgs();
56+
//不走es查询直接返回(全局开关)
57+
if (!EsEngineConfig.isEsquery(method)) {
58+
return pjp.proceed(args);
59+
}
60+
//获取回表查询参数
5161
Object result = null;
5262
try {
63+
//设置标记,在sql拦截器中抛出异常->回到后面的异常处理逻辑中实现es查询
5364
ThreadLocalUtil.set(CommonConstant.IS_ES_QUERY, Boolean.TRUE);
5465
result = pjp.proceed(args);
5566
} catch (EsEngineJpaExecuteException e) {
56-
if (Objects.nonNull(backDto)) {
67+
String esSql = e.getMessage();
68+
//判断是否需要回表查询
69+
if (Objects.isNull(backDto)) {
70+
//无需回表直接执行es查询
71+
//原生es执行 直接使用绑定参数后的sql
72+
result = esSqlQueryHelper.esQuery(method, esSql, args, backDto);
73+
} else {
74+
//需要回表es查询并回表查询
5775
//回表sql执行, sql重新时使用 原生未绑定参数的sql
58-
List<?> esResult = esQueryBack(method, e.getMessage(), args, backDto);
59-
if(CollectionUtils.isEmpty(esResult)){
76+
String bakSql = ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
77+
if (StringUtils.isEmpty(bakSql)) {
78+
throw new EsEngineExecuteException("回表sql异常");
79+
}
80+
List<?> esResult = esSqlQueryHelper.esQueryBack(method, esSql, bakSql, args, backDto);
81+
if (CollectionUtils.isEmpty(esResult)) {
6082
return result;
6183
}
6284
result = pjp.proceed(args);
63-
} else {
64-
//原生es执行 直接使用绑定参数后的sql
65-
result = esQuery(method, e.getMessage(), args, backDto);
6685
}
6786
} finally {
6887
ThreadLocalUtil.remove(CommonConstant.IS_ES_QUERY);
6988
ThreadLocalUtil.remove(CommonConstant.BACK_QUERY_SQL);
89+
ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
7090
}
7191
return result;
7292
}
@@ -95,8 +115,8 @@ public Object esQuery(Method method, String sql, Object[] args, BackDto backDto)
95115
* @param backDto
96116
* @throws Exception
97117
*/
98-
public List<?> esQueryBack(Method method, String sql, Object[] args, BackDto backDto) throws Exception {
99-
String paramSql = fillParamSql(method, sql, args, backDto);
118+
public List<?> esQueryBack(Method method, String esSql, String sql, Object[] args, BackDto backDto) throws Exception {
119+
String paramSql = fillParamSql(method, esSql, args, backDto);
100120
//执行ES查询
101121
List<?> esResult = esSqlExecuteHandler.queryBySql(paramSql, backDto.getBackColumnTyp(), Boolean.TRUE);
102122
if (CollectionUtils.isEmpty(esResult)) {

elasticsearch-engine-base/src/main/java/com/elasticsearch/engine/base/common/parse/sql/SqlParserHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.sf.jsqlparser.JSQLParserException;
1313
import net.sf.jsqlparser.expression.*;
1414
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
15+
import net.sf.jsqlparser.expression.operators.relational.Between;
1516
import net.sf.jsqlparser.expression.operators.relational.InExpression;
1617
import net.sf.jsqlparser.parser.CCJSqlParserManager;
1718
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
@@ -267,6 +268,11 @@ public static void setWhereItem(Expression where, Map<String, String> tableAlias
267268
rightExpression = inExpression.getRightExpression() instanceof Parenthesis ? ((Parenthesis) inExpression.getRightExpression()).getExpression() : inExpression.getRightExpression();
268269
leftExpression = inExpression.getLeftExpression() instanceof Parenthesis ? ((Parenthesis) inExpression.getLeftExpression()).getExpression() : inExpression.getLeftExpression();
269270
}
271+
272+
if (where instanceof Between) {
273+
Between between = (Between) where;
274+
leftExpression = between.getLeftExpression() instanceof Parenthesis ? ((Parenthesis) between.getLeftExpression()).getExpression() : between.getLeftExpression();
275+
}
270276
if (rightExpression instanceof Column) {
271277
Column rightColumn = (Column) rightExpression;
272278
reNameColumnName(rightColumn, tableAlias);
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package com.elasticsearch.engine.jooq.aop;
22

33
import com.elasticsearch.engine.base.common.parse.sql.EsSqlQueryHelper;
4-
import com.elasticsearch.engine.base.common.utils.ThreadLocalUtil;
5-
import com.elasticsearch.engine.base.config.EsEngineConfig;
6-
import com.elasticsearch.engine.base.model.constant.CommonConstant;
7-
import com.elasticsearch.engine.base.model.domain.BackDto;
8-
import com.elasticsearch.engine.base.model.exception.EsEngineExecuteException;
9-
import com.elasticsearch.engine.base.model.exception.EsEngineJpaExecuteException;
104
import com.elasticsearch.engine.jooq.model.JooqBackDto;
115
import lombok.extern.slf4j.Slf4j;
12-
import org.apache.commons.collections4.CollectionUtils;
13-
import org.apache.commons.lang3.StringUtils;
146
import org.aspectj.lang.ProceedingJoinPoint;
157
import org.aspectj.lang.annotation.Around;
168
import org.aspectj.lang.annotation.Aspect;
@@ -19,9 +11,6 @@
1911
import org.springframework.stereotype.Component;
2012

2113
import javax.annotation.Resource;
22-
import java.lang.reflect.Method;
23-
import java.util.List;
24-
import java.util.Objects;
2514

2615

2716
/**
@@ -47,42 +36,7 @@ public void esQueryCut() {
4736
@Around(value = "esQueryCut()")
4837
public Object retryAdvice(ProceedingJoinPoint pjp) throws Throwable {
4938
MethodSignature signature = (MethodSignature) pjp.getSignature();
50-
Method method = signature.getMethod();
51-
Object[] args = pjp.getArgs();
52-
//不走es查询直接返回(全局开关)
53-
if(!EsEngineConfig.isEsquery(method)){
54-
return pjp.proceed(args);
55-
}
56-
//获取回表查询参数
57-
BackDto backDto = JooqBackDto.hasJooqBack(method);
58-
Object result = null;
59-
try {
60-
//设置标记,在sql拦截器中抛出异常->回到后面的异常处理逻辑中实现es查询
61-
ThreadLocalUtil.set(CommonConstant.IS_ES_QUERY, Boolean.TRUE);
62-
result = pjp.proceed(args);
63-
} catch (EsEngineJpaExecuteException e) {
64-
//判断是否需要回表查询
65-
if (Objects.isNull(backDto)) {
66-
//无需回表直接执行es查询
67-
result = esSqlQueryHelper.esQuery(method, e.getMessage(), args, backDto);
68-
} else {
69-
//回表sql执行, sql重新时使用 原生未绑定参数的sql
70-
String bakSql = ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
71-
if (StringUtils.isEmpty(bakSql)) {
72-
throw new EsEngineExecuteException("jpa 回表sql异常");
73-
}
74-
List<?> esResult = esSqlQueryHelper.esQueryBack(method, bakSql, args, backDto);
75-
if (CollectionUtils.isEmpty(esResult)) {
76-
return result;
77-
}
78-
result = pjp.proceed(args);
79-
}
80-
} finally {
81-
ThreadLocalUtil.remove(CommonConstant.IS_ES_QUERY);
82-
ThreadLocalUtil.remove(CommonConstant.BACK_QUERY_SQL);
83-
ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
84-
}
85-
return result;
39+
return esSqlQueryHelper.esSqlQueryAopCommon(pjp, JooqBackDto.hasJooqBack(signature.getMethod()));
8640
}
8741

8842
}
Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package com.elasticsearch.engine.jpa.aop;
22

33
import com.elasticsearch.engine.base.common.parse.sql.EsSqlQueryHelper;
4-
import com.elasticsearch.engine.base.common.utils.ThreadLocalUtil;
5-
import com.elasticsearch.engine.base.config.EsEngineConfig;
6-
import com.elasticsearch.engine.base.model.constant.CommonConstant;
7-
import com.elasticsearch.engine.base.model.domain.BackDto;
8-
import com.elasticsearch.engine.base.model.exception.EsEngineExecuteException;
9-
import com.elasticsearch.engine.base.model.exception.EsEngineJpaExecuteException;
104
import com.elasticsearch.engine.jpa.model.JpaBackDto;
115
import lombok.extern.slf4j.Slf4j;
12-
import org.apache.commons.collections4.CollectionUtils;
13-
import org.apache.commons.lang3.StringUtils;
146
import org.aspectj.lang.ProceedingJoinPoint;
157
import org.aspectj.lang.annotation.Around;
168
import org.aspectj.lang.annotation.Aspect;
@@ -19,9 +11,6 @@
1911
import org.springframework.stereotype.Component;
2012

2113
import javax.annotation.Resource;
22-
import java.lang.reflect.Method;
23-
import java.util.List;
24-
import java.util.Objects;
2514

2615

2716
/**
@@ -47,43 +36,6 @@ public void esQueryCut() {
4736
@Around(value = "esQueryCut()")
4837
public Object retryAdvice(ProceedingJoinPoint pjp) throws Throwable {
4938
MethodSignature signature = (MethodSignature) pjp.getSignature();
50-
Method method = signature.getMethod();
51-
Object[] args = pjp.getArgs();
52-
//不走es查询直接返回(全局开关)
53-
if(!EsEngineConfig.isEsquery(method)){
54-
return pjp.proceed(args);
55-
}
56-
//获取回表查询参数
57-
BackDto backDto = JpaBackDto.hasJpaBack(method);
58-
Object result = null;
59-
try {
60-
//设置标记,在sql拦截器中抛出异常->回到后面的异常处理逻辑中实现es查询
61-
ThreadLocalUtil.set(CommonConstant.IS_ES_QUERY, Boolean.TRUE);
62-
result = pjp.proceed(args);
63-
} catch (EsEngineJpaExecuteException e) {
64-
//判断是否需要回表查询
65-
if (Objects.isNull(backDto)) {
66-
//无需回表直接执行es查询
67-
//原生es执行 直接使用绑定参数后的sql
68-
result = esSqlQueryHelper.esQuery(method, e.getMessage(), args, backDto);
69-
} else {
70-
//需要回表es查询并回表查询
71-
//回表sql执行, sql重新时使用 原生未绑定参数的sql
72-
String bakSql = ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
73-
if (StringUtils.isEmpty(bakSql)) {
74-
throw new EsEngineExecuteException("jpa 回表sql异常");
75-
}
76-
List<?> esResult = esSqlQueryHelper.esQueryBack(method, bakSql, args, backDto);
77-
if (CollectionUtils.isEmpty(esResult)) {
78-
return result;
79-
}
80-
result = pjp.proceed(args);
81-
}
82-
} finally {
83-
ThreadLocalUtil.remove(CommonConstant.IS_ES_QUERY);
84-
ThreadLocalUtil.remove(CommonConstant.BACK_QUERY_SQL);
85-
ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
86-
}
87-
return result;
39+
return esSqlQueryHelper.esSqlQueryAopCommon(pjp, JpaBackDto.hasJpaBack(signature.getMethod()));
8840
}
8941
}

0 commit comments

Comments
 (0)