33import com .elasticsearch .engine .base .common .proxy .handler .exannotation .AnnotationQueryCommon ;
44import com .elasticsearch .engine .base .common .queryhandler .sql .EsSqlExecuteHandler ;
55import com .elasticsearch .engine .base .common .utils .ThreadLocalUtil ;
6+ import com .elasticsearch .engine .base .config .EsEngineConfig ;
67import com .elasticsearch .engine .base .model .constant .CommonConstant ;
78import com .elasticsearch .engine .base .model .domain .BackDto ;
89import com .elasticsearch .engine .base .model .emenu .SqlParamParse ;
10+ import com .elasticsearch .engine .base .model .exception .EsEngineExecuteException ;
911import com .elasticsearch .engine .base .model .exception .EsEngineJpaExecuteException ;
1012import lombok .extern .slf4j .Slf4j ;
1113import net .sf .jsqlparser .JSQLParserException ;
1214import net .sf .jsqlparser .statement .select .Select ;
1315import org .apache .commons .collections4 .CollectionUtils ;
16+ import org .apache .commons .lang3 .StringUtils ;
1417import org .aspectj .lang .ProceedingJoinPoint ;
1518import org .aspectj .lang .reflect .MethodSignature ;
1619import org .springframework .stereotype .Component ;
3033@ Component
3134public class EsSqlQueryHelper {
3235
36+ private static final String ENABLE_LOG_OUT_PROPERTIES = "es.engine.config.sql-trace-log" ;
37+
3338 @ Resource
3439 private EsSqlExecuteHandler esSqlExecuteHandler ;
3540
41+ @ Resource
42+ private EsSqlQueryHelper esSqlQueryHelper ;
43+
3644 /**
3745 * es aop 查询逻辑
3846 *
@@ -45,25 +53,40 @@ public Object esSqlQueryAopCommon(ProceedingJoinPoint pjp, BackDto backDto) thro
4553 MethodSignature signature = (MethodSignature ) pjp .getSignature ();
4654 Method method = signature .getMethod ();
4755 Object [] args = pjp .getArgs ();
56+ //不走es查询直接返回(全局开关)
57+ if (!EsEngineConfig .isEsquery (method )) {
58+ return pjp .proceed (args );
59+ }
60+ //获取回表查询参数
4861 Object result = null ;
4962 try {
63+ //设置标记,在sql拦截器中抛出异常->回到后面的异常处理逻辑中实现es查询
5064 ThreadLocalUtil .set (CommonConstant .IS_ES_QUERY , Boolean .TRUE );
5165 result = pjp .proceed (args );
5266 } catch (EsEngineJpaExecuteException e ) {
53- 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查询并回表查询
5475 //回表sql执行, sql重新时使用 原生未绑定参数的sql
55- List <?> esResult = esQueryBack (method , e .getMessage (), args , backDto );
56- 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 )) {
5782 return result ;
5883 }
5984 result = pjp .proceed (args );
60- } else {
61- //原生es执行 直接使用绑定参数后的sql
62- result = esQuery (method , e .getMessage (), args , backDto );
6385 }
6486 } finally {
6587 ThreadLocalUtil .remove (CommonConstant .IS_ES_QUERY );
6688 ThreadLocalUtil .remove (CommonConstant .BACK_QUERY_SQL );
89+ ThreadLocalUtil .remove (CommonConstant .JPA_NATIVE_SQL );
6790 }
6891 return result ;
6992 }
@@ -92,16 +115,18 @@ public Object esQuery(Method method, String sql, Object[] args, BackDto backDto)
92115 * @param backDto
93116 * @throws Exception
94117 */
95- public List <?> esQueryBack (Method method , String sql , Object [] args , BackDto backDto ) throws Exception {
96- 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 );
97120 //执行ES查询
98121 List <?> esResult = esSqlExecuteHandler .queryBySql (paramSql , backDto .getBackColumnTyp (), Boolean .TRUE );
99- if (CollectionUtils .isEmpty (esResult )){
122+ if (CollectionUtils .isEmpty (esResult )) {
100123 return null ;
101124 }
102125 //将原sql改写成回表sql
103126 String backSql = SqlParserHelper .rewriteBackSql (sql , backDto , esResult );
104- log .info ("回表sql : {}" , backSql );
127+ if (EsEngineConfig .getSqlTraceLog ()) {
128+ log .info ("回表sql : {}" , backSql );
129+ }
105130 //将回表sql添加到threadLocal
106131 ThreadLocalUtil .set (CommonConstant .BACK_QUERY_SQL , backSql );
107132 return esResult ;
@@ -120,21 +145,25 @@ public List<?> esQueryBack(Method method, String sql, Object[] args, BackDto bac
120145 private String fillParamSql (Method method , String sql , Object [] args , BackDto backDto ) throws JSQLParserException {
121146 //jpa判断是否清除as别名
122147 Boolean isCleanAs = Boolean .TRUE ;
123- log .info ("原始sql: {}" , sql );
148+ if (EsEngineConfig .getSqlTraceLog ()) {
149+ log .info ("原始sql: {}" , sql );
150+ }
124151 //jpa原生查询 则不清楚 as别名
125152// Query query = method.getAnnotation(Query.class);
126153// if (Objects.nonNull(query) && query.nativeQuery()) {
127154// isCleanAs = Boolean.FALSE;
128155// }
129156 //改写sql
130157 Select select = SqlParserHelper .rewriteSql (method , sql , isCleanAs , backDto );
131- log .info ("改写后sql: {}" , select );
158+ if (EsEngineConfig .getSqlTraceLog ()) {
159+ log .info ("改写后sql: {}" , select );
160+ }
132161 //参数替换
133162 // 解析sql参数
134- //jooq 需要替换"`"
135- String selectSql = select . toString (). replaceAll ( "`" , "" );
136- String paramSql = SqlParamParseHelper . getMethodArgsSqlJpa ( selectSql , method , args , SqlParamParse . JAP_SQL_PARAM );
137- log . info ( "替换参数后sql: {}" , paramSql );
163+ String paramSql = SqlParamParseHelper . getMethodArgsSqlJpa ( select . toString (), method , args , SqlParamParse . JAP_SQL_PARAM );
164+ if ( EsEngineConfig . getSqlTraceLog ()) {
165+ log . info ( "替换参数后sql: {}" , paramSql );
166+ }
138167 return paramSql ;
139168 }
140169
0 commit comments