forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementVisitorAdapter.java
More file actions
470 lines (386 loc) · 14.9 KB
/
StatementVisitorAdapter.java
File metadata and controls
470 lines (386 loc) · 14.9 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*-
* #%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;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Partition;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.AlterSystemStatement;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.analyze.Analyze;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
import net.sf.jsqlparser.statement.create.sequence.CreateSequence;
import net.sf.jsqlparser.statement.create.synonym.CreateSynonym;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.view.AlterView;
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.delete.ParenthesedDelete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.export.Export;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.imprt.Import;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.insert.InsertConflictAction;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.merge.MergeOperationVisitor;
import net.sf.jsqlparser.statement.merge.MergeOperationVisitorAdapter;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.FromItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.PivotVisitor;
import net.sf.jsqlparser.statement.select.PivotVisitorAdapter;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
import net.sf.jsqlparser.statement.truncate.Truncate;
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;
import java.util.List;
@SuppressWarnings({"PMD.UncommentedEmptyMethodBody"})
public class StatementVisitorAdapter<T> implements StatementVisitor<T> {
private final ExpressionVisitor<T> expressionVisitor;
private final PivotVisitor<T> pivotVisitor;
private final SelectItemVisitor<T> selectItemVisitor;
private final FromItemVisitor<T> fromItemVisitor;
private final SelectVisitor<T> selectVisitor;
private final MergeOperationVisitor<T> mergeOperationVisitor;
public StatementVisitorAdapter() {
this.expressionVisitor = new ExpressionVisitorAdapter<>();
this.pivotVisitor = new PivotVisitorAdapter<>(this.expressionVisitor);
this.selectItemVisitor = new SelectItemVisitorAdapter<>(this.expressionVisitor);
this.fromItemVisitor = new FromItemVisitorAdapter<>();
this.selectVisitor = new SelectVisitorAdapter<>(this.expressionVisitor, this.pivotVisitor,
this.selectItemVisitor, this.fromItemVisitor);
this.mergeOperationVisitor = new MergeOperationVisitorAdapter<>();
}
public StatementVisitorAdapter(ExpressionVisitor<T> expressionVisitor,
PivotVisitor<T> pivotVisitor, SelectItemVisitor<T> selectItemVisitor,
FromItemVisitor<T> fromItemVisitor, SelectVisitor<T> selectVisitor,
MergeOperationVisitor<T> mergeOperationVisitor) {
this.expressionVisitor = expressionVisitor;
this.pivotVisitor = pivotVisitor;
this.selectItemVisitor = selectItemVisitor;
this.fromItemVisitor = fromItemVisitor;
this.selectVisitor = selectVisitor;
this.mergeOperationVisitor = mergeOperationVisitor;
}
public StatementVisitorAdapter(SelectVisitorAdapter<T> selectVisitor) {
this.selectVisitor = selectVisitor;
this.expressionVisitor = selectVisitor.getExpressionVisitor();
this.pivotVisitor = selectVisitor.getPivotVisitor();
this.selectItemVisitor = selectVisitor.getSelectItemVisitor();
this.fromItemVisitor = selectVisitor.getFromItemVisitor();
this.mergeOperationVisitor = new MergeOperationVisitorAdapter<>();
}
@Override
public <S> T visit(Comment comment, S context) {
return null;
}
@Override
public <S> T visit(Commit commit, S context) {
return null;
}
@Override
public <S> T visit(Select select, S context) {
return select.accept(selectVisitor, context);
}
@Override
public <S> T visit(Delete delete, S context) {
visitWithItems(delete.getWithItemsList(), context);
fromItemVisitor.visitTables(delete.getTables(), context);
selectVisitor.visitOutputClause(delete.getOutputClause(), context);
fromItemVisitor.visitFromItem(delete.getTable(), context);
fromItemVisitor.visitTables(delete.getUsingList(), context);
fromItemVisitor.visitJoins(delete.getJoins(), context);
expressionVisitor.visitExpression(delete.getWhere(), context);
expressionVisitor.visitPreferringClause(delete.getPreferringClause(), context);
expressionVisitor.visitOrderBy(delete.getOrderByElements(), context);
expressionVisitor.visitLimit(delete.getLimit(), context);
return null;
}
private <S> void visitWithItems(List<WithItem<?>> withItemsList, S context) {
if (withItemsList != null) {
for (WithItem<?> item : withItemsList) {
item.accept(this, context);
}
}
}
@Override
public <S> T visit(ParenthesedDelete delete, S context) {
delete.getDelete().accept(this, context);
return null;
}
@Override
public <S> T visit(SessionStatement sessionStatement, S context) {
return null;
}
@Override
public <S> T visit(Update update, S context) {
visitWithItems(update.getWithItemsList(), context);
fromItemVisitor.visitFromItem(update.getTable(), context);
fromItemVisitor.visitJoins(update.getStartJoins(), context);
expressionVisitor.visitUpdateSets(update.getUpdateSets(), context);
selectVisitor.visitOutputClause(update.getOutputClause(), context);
fromItemVisitor.visitFromItem(update.getFromItem(), context);
fromItemVisitor.visitJoins(update.getJoins(), context);
expressionVisitor.visitExpression(update.getWhere(), context);
expressionVisitor.visitPreferringClause(update.getPreferringClause(), context);
expressionVisitor.visitOrderBy(update.getOrderByElements(), context);
expressionVisitor.visitLimit(update.getLimit(), context);
visitReturningClause(update.getReturningClause(), context);
return null;
}
@Override
public <S> T visit(ParenthesedUpdate update, S context) {
return update.getUpdate().accept(this, context);
}
@Override
public <S> T visit(Insert insert, S context) {
visitWithItems(insert.getWithItemsList(), context);
insert.getTable().accept(fromItemVisitor, context);
if (insert.getColumns() != null) {
for (Column column : insert.getColumns()) {
column.accept(expressionVisitor, context);
}
}
if (insert.getPartitions() != null) {
for (Partition partition : insert.getPartitions()) {
partition.getColumn().accept(expressionVisitor, context);
if (partition.getValue() != null) {
partition.getValue().accept(expressionVisitor, context);
}
}
}
selectVisitor.visitOutputClause(insert.getOutputClause(), context);
if (insert.getSelect() != null) {
insert.getSelect().accept(selectVisitor, null);
}
expressionVisitor.visitUpdateSets(insert.getSetUpdateSets(), context);
expressionVisitor.visitUpdateSets(insert.getDuplicateUpdateSets(), context);
final InsertConflictAction conflictAction = insert.getConflictAction();
if (conflictAction != null) {
expressionVisitor.visitExpression(conflictAction.getWhereExpression(), context);
expressionVisitor.visitUpdateSets(conflictAction.getUpdateSets(), context);
}
visitReturningClause(insert.getReturningClause(), context);
return null;
}
private <S> T visitReturningClause(ReturningClause returningClause, S context) {
if (returningClause != null) {
returningClause.forEach(selectItem -> selectItem.accept(selectItemVisitor, context));
// @todo: verify why this is a list of strings and not columns
}
return null;
}
@Override
public <S> T visit(ParenthesedInsert insert, S context) {
insert.getInsert().accept(this, context);
return null;
}
@Override
public <S> T visit(Drop drop, S context) {
if (drop.getType().equalsIgnoreCase("table")) {
fromItemVisitor.visitFromItem(drop.getName(), context);
}
// @todo: handle schemas
return null;
}
@Override
public <S> T visit(Truncate truncate, S context) {
return truncate.getTable().accept(fromItemVisitor, context);
}
@Override
public <S> T visit(CreateIndex createIndex, S context) {
return null;
}
@Override
public <S> T visit(CreateSchema createSchema, S context) {
return null;
}
@Override
public <S> T visit(CreateTable createTable, S context) {
return createTable.getTable().accept(fromItemVisitor, context);
}
@Override
public <S> T visit(CreateView createView, S context) {
return null;
}
@Override
public <S> T visit(Alter alter, S context) {
return null;
}
@Override
public <S> T visit(Statements statements, S context) {
for (Statement statement : statements) {
statement.accept(this, context);
}
return null;
}
@Override
public <S> T visit(Execute execute, S context) {
return null;
}
@Override
public <S> T visit(LockStatement lock, S context) {
return null;
}
@Override
public <S> T visit(SetStatement set, S context) {
return null;
}
@Override
public <S> T visit(ResetStatement reset, S context) {
return null;
}
@Override
public <S> T visit(Merge merge, S context) {
visitWithItems(merge.getWithItemsList(), context);
fromItemVisitor.visitFromItem(merge.getTable(), context);
fromItemVisitor.visitFromItem(merge.getFromItem(), context);
expressionVisitor.visitExpression(merge.getOnCondition(), context);
mergeOperationVisitor.visit(merge.getOperations(), context);
selectVisitor.visitOutputClause(merge.getOutputClause(), context);
return null;
}
@Override
public <S> T visit(AlterView alterView, S context) {
return null;
}
@Override
public <S> T visit(Upsert upsert, S context) {
return null;
}
@Override
public <S> T visit(UseStatement use, S context) {
return null;
}
@Override
public <S> T visit(Block block, S context) {
return null;
}
@Override
public <S> T visit(DescribeStatement describe, S context) {
return null;
}
@Override
public <S> T visit(ExplainStatement explainStatement, S context) {
return null;
}
@Override
public <S> T visit(ShowStatement showStatement, S context) {
return null;
}
@Override
public <S> T visit(ShowColumnsStatement showColumnsStatement, S context) {
return null;
}
@Override
public <S> T visit(ShowIndexStatement showIndexStatement, S context) {
return null;
}
@Override
public <S> T visit(ShowTablesStatement showTables, S context) {
return null;
}
@Override
public <S> T visit(DeclareStatement declareStatement, S context) {
return null;
}
@Override
public <S> T visit(Grant grant, S context) {
return null;
}
@Override
public <S> T visit(CreateSequence createSequence, S context) {
return null;
}
@Override
public <S> T visit(AlterSequence alterSequence, S context) {
return null;
}
@Override
public <S> T visit(CreateFunctionalStatement createFunctionalStatement, S context) {
return null;
}
@Override
public <S> T visit(CreateSynonym createSynonym, S context) {
return null;
}
@Override
public <S> T visit(Analyze analyze, S context) {
return null;
}
@Override
public <S> T visit(SavepointStatement savepointStatement, S context) {
// @todo: do something usefully here
return null;
}
@Override
public <S> T visit(RollbackStatement rollbackStatement, S context) {
// @todo: do something usefully here
return null;
}
@Override
public <S> T visit(AlterSession alterSession, S context) {
// @todo: do something usefully here
return null;
}
@Override
public <S> T visit(IfElseStatement ifElseStatement, S context) {
ifElseStatement.getIfStatement().accept(this, context);
if (ifElseStatement.getElseStatement() != null) {
ifElseStatement.getElseStatement().accept(this, context);
}
return null;
}
@Override
public <S> T visit(RenameTableStatement renameTableStatement, S context) {
return null;
}
@Override
public <S> T visit(PurgeStatement purgeStatement, S context) {
return null;
}
@Override
public <S> T visit(AlterSystemStatement alterSystemStatement, S context) {
return null;
}
@Override
public <S> T visit(UnsupportedStatement unsupportedStatement, S context) {
return null;
}
@Override
public <S> T visit(RefreshMaterializedViewStatement materializedView, S context) {
return null;
}
@Override
public <S> T visit(Import imprt, S context) {
return null;
}
@Override
public <S> T visit(Export export, S context) {
return null;
}
}