-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSelectVisitorAdapter.java
More file actions
326 lines (274 loc) · 12.3 KB
/
Copy pathSelectVisitorAdapter.java
File metadata and controls
326 lines (274 loc) · 12.3 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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.statement.OutputClause;
import net.sf.jsqlparser.statement.ParenthesedStatement;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.piped.FromQuery;
@SuppressWarnings({"PMD.UncommentedEmptyMethodBody"})
public class SelectVisitorAdapter<T> implements SelectVisitor<T> {
private final ExpressionVisitor<T> expressionVisitor;
private final PivotVisitor<T> pivotVisitor;
private final SelectItemVisitor<T> selectItemVisitor;
private final FromItemVisitor<T> fromItemVisitor;
/**
* Optional, and only needed for data-modifying CTEs - see {@link #visit(WithItem, Object)}. A
* plain SelectVisitor cannot accept an INSERT/UPDATE/DELETE body; without this the CTE is
* skipped rather than traversed. {@link net.sf.jsqlparser.statement.StatementVisitorAdapter}
* sets it automatically.
*/
private StatementVisitor<T> statementVisitor;
public SelectVisitorAdapter() {
this.expressionVisitor = new ExpressionVisitorAdapter<>(this);
this.pivotVisitor = new PivotVisitorAdapter<>(this.expressionVisitor);
this.selectItemVisitor = new SelectItemVisitorAdapter<>(this.expressionVisitor);
this.fromItemVisitor = new FromItemVisitorAdapter<>(this, this.expressionVisitor);
}
public SelectVisitorAdapter(ExpressionVisitor<T> expressionVisitor,
PivotVisitor<T> pivotVisitor, SelectItemVisitor<T> selectItemVisitor,
FromItemVisitor<T> fromItemVisitor) {
this.expressionVisitor = expressionVisitor;
this.pivotVisitor = pivotVisitor;
this.selectItemVisitor = selectItemVisitor;
this.fromItemVisitor = fromItemVisitor;
}
public SelectVisitorAdapter(ExpressionVisitor<T> expressionVisitor,
FromItemVisitor<T> fromItemVisitor) {
this.expressionVisitor = expressionVisitor;
this.pivotVisitor = new PivotVisitorAdapter<>();
this.selectItemVisitor = new SelectItemVisitorAdapter<>(this.expressionVisitor);
this.fromItemVisitor = fromItemVisitor;
}
public SelectVisitorAdapter(ExpressionVisitor<T> expressionVisitor) {
this.expressionVisitor = expressionVisitor;
this.pivotVisitor = new PivotVisitorAdapter<>();
this.selectItemVisitor = new SelectItemVisitorAdapter<>(this.expressionVisitor);
this.fromItemVisitor = new FromItemVisitorAdapter<>();
}
@Override
public <S> T visitOutputClause(OutputClause outputClause, S context) {
if (outputClause != null) {
if (outputClause.getSelectItemList() != null) {
for (SelectItem<?> selectItem : outputClause.getSelectItemList()) {
selectItem.accept(selectItemVisitor, context);
}
}
if (outputClause.getTableVariable() != null) {
outputClause.getTableVariable().accept(expressionVisitor, context);
}
if (outputClause.getOutputTable() != null) {
outputClause.getOutputTable().accept(fromItemVisitor, context);
}
// @todo: check why this is a list of strings
// if (outputClause.getColumnList()!=null) {
// for (Column column:outputClause.getColumnList())
// }
}
return null;
}
public ExpressionVisitor<T> getExpressionVisitor() {
return expressionVisitor;
}
public PivotVisitor<T> getPivotVisitor() {
return pivotVisitor;
}
public SelectItemVisitor<T> getSelectItemVisitor() {
return selectItemVisitor;
}
public FromItemVisitor<T> getFromItemVisitor() {
return fromItemVisitor;
}
public StatementVisitor<T> getStatementVisitor() {
return statementVisitor;
}
public SelectVisitorAdapter<T> setStatementVisitor(StatementVisitor<T> statementVisitor) {
this.statementVisitor = statementVisitor;
return this;
}
@Override
public <S> T visit(ParenthesedSelect select, S context) {
visitWithItems(select.withItemsList, context);
select.getSelect().accept(this, context);
expressionVisitor.visitOrderBy(select.getOrderByElements(), context);
Pivot pivot = select.getPivot();
if (pivot != null) {
pivot.accept(pivotVisitor, context);
}
UnPivot unpivot = select.getUnPivot();
if (unpivot != null) {
unpivot.accept(pivotVisitor, context);
}
expressionVisitor.visitLimit(select.getLimit(), context);
if (select.getOffset() != null) {
expressionVisitor.visitExpression(select.getOffset().getOffset(), null);
}
if (select.getFetch() != null) {
expressionVisitor.visitExpression(select.getFetch().getExpression(), null);
}
return null;
}
@Override
@SuppressWarnings({"PMD.ExcessiveMethodLength"})
public <S> T visit(PlainSelect plainSelect, S context) {
visitWithItems(plainSelect.withItemsList, context);
if (plainSelect.getDistinct() != null) {
for (SelectItem<?> selectItem : plainSelect.getDistinct().getOnSelectItems()) {
selectItem.accept(selectItemVisitor, context);
}
}
if (plainSelect.getTop() != null) {
plainSelect.getTop().getExpression().accept(expressionVisitor, context);
}
for (SelectItem<?> selectItem : plainSelect.getSelectItems()) {
selectItem.accept(selectItemVisitor, context);
}
if (plainSelect.getMySqlSelectIntoClause() != null) {
MySqlSelectIntoClause mySqlSelectIntoClause = plainSelect.getMySqlSelectIntoClause();
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFileName(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsTerminatedBy(),
context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsEnclosedBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsEscapedBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getLinesStartingBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getLinesTerminatedBy(),
context);
}
fromItemVisitor.visitTables(plainSelect.getIntoTables(), context);
fromItemVisitor.visitFromItem(plainSelect.getFromItem(), context);
// if (plainSelect.getLateralViews() != null) {
// //@todo: implement this
// }
fromItemVisitor.visitJoins(plainSelect.getJoins(), context);
// if (plainSelect.getKsqlWindow() != null) {
// //@todo: implement
// }
expressionVisitor.visitExpression(plainSelect.getPreWhere(), context);
expressionVisitor.visitExpression(plainSelect.getWhere(), context);
// if (plainSelect.getOracleHierarchical() != null) {
// //@todo: implement
// }
//
expressionVisitor.visitPreferringClause(plainSelect.getPreferringClause(), context);
expressionVisitor.visit(plainSelect.getGroupBy(), context);
expressionVisitor.visitExpression(plainSelect.getHaving(), context);
expressionVisitor.visitExpression(plainSelect.getQualify(), context);
// if (plainSelect.getWindowDefinitions() != null) {
// //@todo: implement
// }
Pivot pivot = plainSelect.getPivot();
if (pivot != null) {
pivot.accept(pivotVisitor, context);
}
UnPivot unpivot = plainSelect.getUnPivot();
if (unpivot != null) {
unpivot.accept(pivotVisitor, context);
}
expressionVisitor.visitOrderBy(plainSelect.getOrderByElements(), context);
// if (plainSelect.getLimitBy() != null) {
// //@todo: implement
// }
// if (plainSelect.getLimit() != null) {
// //@todo: implement
// }
if (plainSelect.getOffset() != null) {
expressionVisitor.visitExpression(plainSelect.getOffset().getOffset(), context);
}
if (plainSelect.getMySqlProcedureAnalyse() != null) {
expressionVisitor.visitExpression(
plainSelect.getMySqlProcedureAnalyse().getMaxElements(), context);
expressionVisitor.visitExpression(
plainSelect.getMySqlProcedureAnalyse().getMaxMemory(), context);
}
if (plainSelect.getFetch() != null) {
expressionVisitor.visitExpression(plainSelect.getFetch().getExpression(), context);
}
// if (plainSelect.getForMode() != null) {
// //@todo: implement
// }
fromItemVisitor.visitFromItem(plainSelect.getIntoTempTable(), context);
return null;
}
@Override
public <S> T visit(PivotQuery pivotQuery, S context) {
visitWithItems(pivotQuery.getWithItemsList(), context);
fromItemVisitor.visitFromItem(pivotQuery.getFromItem(), context);
expressionVisitor.visitExpressions(pivotQuery.getOnExpressions(), context);
if (pivotQuery.getUsingItems() != null) {
for (SelectItem<Function> item : pivotQuery.getUsingItems()) {
item.accept(selectItemVisitor, context);
}
}
expressionVisitor.visitExpressions(pivotQuery.getGroupByExpressions(), context);
expressionVisitor.visitOrderBy(pivotQuery.getOrderByElements(), context);
expressionVisitor.visitLimit(pivotQuery.getLimit(), context);
if (pivotQuery.getOffset() != null) {
expressionVisitor.visitExpression(pivotQuery.getOffset().getOffset(), context);
}
if (pivotQuery.getFetch() != null) {
expressionVisitor.visitExpression(pivotQuery.getFetch().getExpression(), context);
}
return null;
}
@Override
public <S> T visit(FromQuery fromQuery, S context) {
return null;
}
@Override
public <S> T visit(SetOperationList setOpList, S context) {
for (Select select : setOpList.getSelects()) {
select.accept(this, context);
}
return null;
}
/**
* A CTE body is a {@link ParenthesedStatement}, which is not necessarily a SELECT: PostgreSQL
* and others allow {@code WITH c AS (DELETE FROM t RETURNING *) SELECT * FROM c}.
*
* <p>
* The previous implementation called {@code withItem.getSelect()}, an unchecked cast to
* {@link ParenthesedSelect}, and threw {@link ClassCastException} on every data-modifying CTE
* reached through the select path - which {@code ExpressionVisitorAdapter#visit(Select)} does
* for any sub-select. {@code WithItem#accept(StatementVisitor)} has always dispatched correctly
* through {@code getParenthesedStatement()}; only this path did not.
*/
@Override
public <S> T visit(WithItem<?> withItem, S context) {
if (withItem.getCycleClause() != null) {
withItem.getCycleClause().accept(expressionVisitor, context);
}
ParenthesedStatement body = withItem.getParenthesedStatement();
// ParenthesedSelect is a Select and stays on the select path
if (body instanceof Select) {
return ((Select) body).accept(this, context);
}
// ParenthesedInsert / ParenthesedUpdate / ParenthesedDelete need a StatementVisitor
if (statementVisitor != null && body instanceof Statement) {
return ((Statement) body).accept(statementVisitor, context);
}
// no statement visitor available: skip the body rather than fail
return null;
}
@Override
public <S> T visit(Values aThis, S context) {
return null;
}
@Override
public <S> T visit(LateralSubSelect lateralSubSelect, S context) {
return lateralSubSelect.getSelect().accept(this, context);
}
@Override
public <S> T visit(TableStatement tableStatement, S context) {
return null;
}
}