JoinParser.java
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package io.mycat.catlets;
import java.util.LinkedHashMap;
import java.util.List;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLBooleanExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
/**
* 功能详细描述:分片join,解析join语句
* @author sohudo[http://blog.csdn.net/wind520]
* @create 2015年01月25日
* @version 0.0.1
*/
public class JoinParser {
protected static final Logger LOGGER = LoggerFactory.getLogger(JoinParser.class);
private MySqlSelectQueryBlock mysqlQuery;
private String stmt="";
private String joinType;
private String masterTable;
private TableFilter tableFilter;
//private LinkedHashMap<String,String> fieldAliasMap = new LinkedHashMap<String,String>();
public JoinParser(MySqlSelectQueryBlock selectQuery,String stmt) {
this.mysqlQuery=selectQuery;
this.stmt=stmt;
}
public void parser(){
masterTable="";
SQLTableSource table=mysqlQuery.getFrom();
parserTable(table,tableFilter,false);
parserFields(mysqlQuery.getSelectList());
parserMasterTable();
parserWhere(mysqlQuery.getWhere(),"");
// getJoinField();
parserOrderBy(mysqlQuery.getOrderBy());
parserLimit();
// LOGGER.info("field "+fieldAliasMap);
// LOGGER.info("master "+masterTable);
// LOGGER.info("join Lkey "+getJoinLkey());
// LOGGER.info("join Rkey "+getJoinRkey());
LOGGER.info("SQL: "+this.stmt);
}
private void parserTable(SQLTableSource table,TableFilter tFilter,boolean isOutJoin){
if(table instanceof SQLJoinTableSource){
SQLJoinTableSource table1=(SQLJoinTableSource)table;
joinType=table1.getJoinType().toString();
if ((table1.getJoinType()==JoinType.COMMA)||(table1.getJoinType()==JoinType.JOIN)||(table1.getJoinType()==JoinType.INNER_JOIN)
||(table1.getJoinType()==JoinType.LEFT_OUTER_JOIN)) {
tFilter=setTableFilter(tFilter,getTableFilter(table1.getLeft(),isOutJoin));
if (tableFilter==null){
tableFilter=tFilter;
}
}
//parserTable(table1.getLeft()); //SQLExprTableSource
parserTable(table1.getRight(),tFilter,true);
SQLExpr expr=table1.getCondition();//SQLBinaryOpExpr
parserJoinKey(expr);
}
else {
tFilter=setTableFilter(tFilter,getTableFilter(table,isOutJoin));
LOGGER.info("table "+table.toString() +" Alias:"+table.getAlias()+" Hints:"+table.getHints());
}
}
private TableFilter setTableFilter(TableFilter tFilter,TableFilter newFilter){
if (tFilter==null) {
tFilter=newFilter;
return tFilter;
}
else {
tFilter.setTableJoin(newFilter);
return tFilter.getTableJoin();
}
}
private TableFilter getTableFilter(SQLTableSource table,boolean isOutJoin){
String key ;
String value = table.toString().trim();
if (table.getAlias()==null) {
key=value;
}
else {
key = table.getAlias().trim();
}
return new TableFilter(value,key,isOutJoin);
}
private void parserJoinKey(SQLExpr expr){
if (expr==null) {
return;
}
parserWhere(expr,"");
}
private String getExprFieldName(SQLAggregateExpr expr){
StringBuilder field = new StringBuilder();
for (SQLExpr item :expr.getArguments()){
field.append(item.toString());
}
return expr.getMethodName()+"("+field.toString()+")";
}
private String getFieldName(SQLSelectItem item){
if (item.getExpr() instanceof SQLPropertyExpr) {
return item.getExpr().toString();//字段别名
}
else {
return item.toString();
}
}
private String getMethodInvokeFieldName(SQLSelectItem item){
SQLMethodInvokeExpr invoke = (SQLMethodInvokeExpr)item.getExpr();
List<SQLExpr> itemExprs = invoke.getParameters();
for(SQLExpr itemExpr:itemExprs){
if (itemExpr instanceof SQLPropertyExpr) {
return itemExpr.toString();//字段别名
}
}
return item.toString();
}
private void parserFields(List<SQLSelectItem> mysqlSelectList){
//显示的字段
String key="";
String value ="";
String exprfield = "";
for(SQLSelectItem item : mysqlSelectList) {
if (item.getExpr() instanceof SQLAllColumnExpr) {
//*解析
setField(item.toString(), item.toString());
}
else {
if (item.getExpr() instanceof SQLAggregateExpr) {
SQLAggregateExpr expr =(SQLAggregateExpr)item.getExpr();
key = getExprFieldName(expr);
setField(key, value);
}else if(item.getExpr() instanceof SQLMethodInvokeExpr){
key = getMethodInvokeFieldName(item);
exprfield=getFieldName(item);
// value=item.getAlias();
setField(key, value,exprfield);
}else {
key=getFieldName(item);
value=item.getAlias();
setField(key, value);
}
}
}
}
private void setField(String key,String value){
//fieldAliasMap.put(key, value);
if (tableFilter!=null){
tableFilter.addField(key, value);
}
}
private void setField(String key,String value,String expr){
//fieldAliasMap.put(key, value);
if (tableFilter!=null){
tableFilter.addField(key, value,expr);
}
}
//判断并获得主表
private void parserMasterTable(){
if (tableFilter!=null){
masterTable=tableFilter.getTableAlia();
}
}
private boolean checkJoinField(String value){
if (value==null){
return false;
}
else {
int i=value.indexOf('.');
return i>0;
}
}
private void parserWhere(SQLExpr aexpr,String Operator){
if (aexpr==null) {
return;
}
if (aexpr instanceof SQLBinaryOpExpr){
SQLBinaryOpExpr expr=(SQLBinaryOpExpr)aexpr;
SQLExpr exprL=expr.getLeft();
if (!(exprL instanceof SQLBinaryOpExpr))
{
opSQLExpr((SQLBinaryOpExpr)aexpr,Operator);
}
else {
// if (expr.getOperator().getName().equals("AND")) {
if (expr.getOperator()==SQLBinaryOperator.BooleanAnd) {
//parserWhere(exprL);
//parserWhere(expr.getRight());
andorWhere(exprL,expr.getOperator().getName(),expr.getRight());
}
else if (expr.getOperator()==SQLBinaryOperator.BooleanOr){//.getName().equals("OR")) {
andorWhere(exprL,expr.getOperator().getName(),expr.getRight());
}
else {
throw new RuntimeException("Can't identify the operation of of where");
}
}
}else if(aexpr instanceof SQLInListExpr){
SQLInListExpr expr = (SQLInListExpr)aexpr;
SQLExpr exprL = expr.getExpr();
String field=exprL.toString();
tableFilter.addWhere(field, SQLUtils.toMySqlString(expr), Operator);
}
}
private void andorWhere(SQLExpr exprL,String Operator,SQLExpr exprR ){
parserWhere(exprL,"");
parserWhere(exprR,Operator);
}
private void opSQLExpr(SQLBinaryOpExpr expr,String Operator) {
if (expr==null) {
return;
}
SQLExpr exprL=expr.getLeft();
if (!(exprL instanceof SQLBinaryOpExpr))
{
String field=exprL.toString();
String value=getExpValue(expr.getRight()).toString();
if (expr.getOperator()==SQLBinaryOperator.Equality) {
if (checkJoinField(value)) {
//joinLkey=field;
//joinRkey=value;
tableFilter.setJoinKey(field,value);
}
else {
tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
}
}
else {
tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
}
}
}
private Object getExpValue(SQLExpr expr){
if (expr instanceof SQLIntegerExpr){
return ((SQLIntegerExpr)expr).getNumber().intValue();
}
if (expr instanceof SQLNumberExpr){
return ((SQLNumberExpr)expr).getNumber().doubleValue();
}
if (expr instanceof SQLCharExpr){
String va=((SQLCharExpr)expr).toString();
return va;//remove(va,'\'');
}
if (expr instanceof SQLBooleanExpr){
return ((SQLBooleanExpr)expr).getValue();
}
if (expr instanceof SQLNullExpr){
return null;
}
return expr;
}
private void parserOrderBy(SQLOrderBy orderby)
{
if (orderby != null ){
for (int i = 0; i < orderby.getItems().size(); i++)
{
SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
tableFilter.addOrders(orderitem.getExpr().toString(), getSQLExprToAsc(orderitem.getType()));
}
}
}
private void parserLimit(){
int limitoff=0;
int limitnum=0;
if (this.mysqlQuery.getLimit()!=null) {
limitoff=getSQLExprToInt(this.mysqlQuery.getLimit().getOffset());
limitnum=getSQLExprToInt(this.mysqlQuery.getLimit().getRowCount());
tableFilter.addLimit(limitoff,limitnum);
}
}
private int getSQLExprToInt(SQLExpr expr){
if (expr instanceof SQLIntegerExpr){
return ((SQLIntegerExpr)expr).getNumber().intValue();
}
return 0;
}
private String getSQLExprToAsc(SQLOrderingSpecification ASC){
if (ASC==null ) {
return " ASC ";
}
if (ASC==SQLOrderingSpecification.DESC){
return " DESC ";
}
else {
return " ASC ";
}
}
public String getChildSQL(){
//String sql="select "+joinRkey+","+sql+" from "+mtable+" where "+joinRkey+" in ";
String sql=tableFilter.getTableJoin().getSQL();
return sql;
}
public String getSql(){
stmt=tableFilter.getSQL();
return stmt;
}
public String getJoinType(){
return joinType;
}
public String getJoinLkey(){
return tableFilter.getJoinKey(true);
}
public String getJoinRkey(){
return tableFilter.getJoinKey(false);
}
}