1
1
package com .elasticsearch .engine .base .common .parse .sql ;
2
2
3
3
import com .elasticsearch .engine .base .common .utils .CaseFormatUtils ;
4
- import com .elasticsearch .engine .base .model .exception .EsEngineQueryException ;
5
4
import com .elasticsearch .engine .base .config .EsEngineConfig ;
6
5
import com .elasticsearch .engine .base .model .annotion .EsQueryIndex ;
7
6
import com .elasticsearch .engine .base .model .domain .BackDto ;
7
+ import com .elasticsearch .engine .base .model .exception .EsEngineQueryException ;
8
8
import com .google .common .collect .Lists ;
9
+ import com .google .common .collect .Maps ;
9
10
import net .sf .jsqlparser .JSQLParserException ;
10
11
import net .sf .jsqlparser .expression .*;
11
12
import net .sf .jsqlparser .expression .operators .conditional .AndExpression ;
22
23
import java .io .StringReader ;
23
24
import java .lang .reflect .Method ;
24
25
import java .util .List ;
26
+ import java .util .Map ;
25
27
import java .util .Objects ;
26
28
27
29
/**
@@ -79,7 +81,7 @@ public static String rewriteBackSql(String oldSql, BackDto backDto, List<?> esRe
79
81
CCJSqlParserManager parserManager = new CCJSqlParserManager ();
80
82
Select select = (Select ) parserManager .parse (new StringReader (oldSql ));
81
83
PlainSelect plain = (PlainSelect ) select .getSelectBody ();
82
- //where 别名清除
84
+ //where 添加回表条件
83
85
setBackWhereItem (plain , backDto , esResult );
84
86
// plain.setGroupByElement(null);
85
87
// plain.setHaving(null);
@@ -96,7 +98,7 @@ public static String rewriteBackSql(String oldSql, BackDto backDto, List<?> esRe
96
98
* @throws Exception
97
99
*/
98
100
private static void setBackWhereItem (PlainSelect plain , BackDto backDto , List <?> esResult ) throws Exception {
99
- String tableName = StringUtils . isEmpty ( backDto . getTableName ()) ? getTableName ( plain ) : backDto . getTableName ( );
101
+ String tableName = getBackTableName ( plain , backDto );
100
102
//ColumnName es驼峰 转 mysql下划线
101
103
String backColumn = backDto .getBackColumn ();
102
104
if (!EsEngineConfig .isNamingStrategy ()) {
@@ -113,12 +115,31 @@ private static void setBackWhereItem(PlainSelect plain, BackDto backDto, List<?>
113
115
}
114
116
115
117
/**
116
- * 获取表名(关联查询时 表名为主表表名)
118
+ * 回去回表查询表名
119
+ * @param plain
120
+ * @param backDto
121
+ * @return
122
+ */
123
+ private static String getBackTableName (PlainSelect plain , BackDto backDto ) {
124
+ String tableName ;
125
+ if (StringUtils .isNotEmpty (backDto .getTableName ())) {
126
+ tableName = getJoinTableName (plain ).get (backDto .getTableName ());
127
+ } else {
128
+ tableName = getDefaultFromTableName (plain );
129
+ }
130
+ if (StringUtils .isEmpty (tableName )) {
131
+ throw new EsEngineQueryException ("回表查询指定的表名不存在: " + backDto .getTableName ());
132
+ }
133
+ return tableName ;
134
+ }
135
+
136
+ /**
137
+ * 获取表名(关联查询时 默认表名为主表表名)
117
138
*
118
139
* @param plain
119
140
* @return
120
141
*/
121
- public static String getTableName (PlainSelect plain ) {
142
+ private static String getDefaultFromTableName (PlainSelect plain ) {
122
143
FromItem fromItem = plain .getFromItem ();
123
144
String fromItemName = "" ;
124
145
if (fromItem instanceof Table ) {
@@ -127,6 +148,30 @@ public static String getTableName(PlainSelect plain) {
127
148
return fromItem .getAlias () == null ? fromItemName : fromItem .getAlias ().getName ();
128
149
}
129
150
151
+ /**
152
+ * 获取表名(主表表名,及关联表表名, key为原始表名, value为原始表名或别名)
153
+ *
154
+ * @param plain
155
+ * @return
156
+ */
157
+ private static Map <String , String > getJoinTableName (PlainSelect plain ) {
158
+ List <FromItem > fromItems = Lists .newArrayList ();
159
+ Map <String , String > tableNames = Maps .newHashMap ();
160
+ fromItems .add (plain .getFromItem ());
161
+ if (CollectionUtils .isNotEmpty (plain .getJoins ())) {
162
+ plain .getJoins ().forEach (item -> fromItems .add (item .getRightItem ()));
163
+ }
164
+
165
+ fromItems .forEach (fromItem -> {
166
+ String fromItemName = "" ;
167
+ if (fromItem instanceof Table ) {
168
+ fromItemName = ((Table ) fromItem ).getName ().replaceAll ("`" , "" );
169
+ }
170
+ tableNames .put (fromItemName , fromItem .getAlias () == null ? fromItemName : fromItem .getAlias ().getName ());
171
+ });
172
+ return tableNames ;
173
+ }
174
+
130
175
/**
131
176
* TODO 获取索引名的方法抽取通用方法
132
177
* 替换表名
0 commit comments