Repository navigation
Expand file tree
/
Copy pathDelete.java
More file actions
617 lines (517 loc) · 18 KB
/
Copy pathDelete.java
File metadata and controls
617 lines (517 loc) · 18 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*-
* #%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.delete;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.expression.PreferringClause;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.schema.TableReference;
import net.sf.jsqlparser.statement.ForPortionClause;
import net.sf.jsqlparser.statement.OutputClause;
import net.sf.jsqlparser.statement.ReturningClause;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OptionClause;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.WithItem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.joining;
public class Delete extends ASTNodeAccessImpl implements Statement {
private List<WithItem<?>> withItemsList;
private TableReference tableReference = new TableReference(null);
private ForPortionClause forPortionClause;
private OracleHint oracleHint = null;
private List<Table> tables;
private List<FromItem> usingFromItemList;
private List<Join> usingJoins;
private List<Join> joins;
private Expression where;
private String cursorName;
private PreferringClause preferringClause;
private Limit limit;
private OptionClause option;
private List<OrderByElement> orderByElements;
private boolean hasFrom = true;
private DeleteModifierPriority modifierPriority;
private boolean modifierIgnore;
private boolean modifierQuick;
private ReturningClause returningClause;
private OutputClause outputClause;
public OutputClause getOutputClause() {
return outputClause;
}
public void setOutputClause(OutputClause outputClause) {
this.outputClause = outputClause;
}
public ReturningClause getReturningClause() {
return returningClause;
}
public Delete setReturningClause(ReturningClause returningClause) {
this.returningClause = returningClause;
return this;
}
public List<WithItem<?>> getWithItemsList() {
return withItemsList;
}
public void setWithItemsList(List<WithItem<?>> withItemsList) {
this.withItemsList = withItemsList;
}
public Delete withWithItemsList(List<WithItem<?>> withItemsList) {
this.setWithItemsList(withItemsList);
return this;
}
public Delete addWithItemsList(WithItem<?>... withItemsList) {
List<WithItem<?>> collection =
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
Collections.addAll(collection, withItemsList);
return this.withWithItemsList(collection);
}
public Delete addWithItemsList(Collection<? extends WithItem<?>> withItemsList) {
List<WithItem<?>> collection =
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
collection.addAll(withItemsList);
return this.withWithItemsList(collection);
}
public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
public void setOrderByElements(List<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
}
@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
public ForPortionClause getForPortionClause() {
return forPortionClause;
}
public void setForPortionClause(ForPortionClause forPortionClause) {
this.forPortionClause = forPortionClause;
}
public Delete withForPortionClause(ForPortionClause forPortionClause) {
setForPortionClause(forPortionClause);
return this;
}
public Table getTable() {
return tableReference.getTable();
}
/** Replaces the target table and keeps the inheritance scope of the target. */
public void setTable(Table name) {
tableReference.setTable(name);
}
/**
* The target as PostgreSQL's relation_expr_opt_alias: the table of {@link #getTable()} with its
* alias, and whether ONLY or * describes its inheritance scope.
*/
public TableReference getTableReference() {
return tableReference;
}
public void setTableReference(TableReference tableReference) {
this.tableReference = Objects.requireNonNull(tableReference);
}
public Delete withTableReference(TableReference tableReference) {
this.setTableReference(tableReference);
return this;
}
/**
* The cursor of {@code WHERE CURRENT OF cursor}, which is written in place of the condition of
* {@link #getWhere()}.
*/
public String getCursorName() {
return cursorName;
}
public void setCursorName(String cursorName) {
this.cursorName = cursorName;
}
public Delete withCursorName(String cursorName) {
this.setCursorName(cursorName);
return this;
}
public Expression getWhere() {
return where;
}
public void setWhere(Expression expression) {
where = expression;
}
public PreferringClause getPreferringClause() {
return preferringClause;
}
public void setPreferringClause(PreferringClause preferringClause) {
this.preferringClause = preferringClause;
}
public OracleHint getOracleHint() {
return oracleHint;
}
public void setOracleHint(OracleHint oracleHint) {
this.oracleHint = oracleHint;
}
public Limit getLimit() {
return limit;
}
public OptionClause getOption() {
return option;
}
public Delete setOption(OptionClause option) {
this.option = option;
return this;
}
public Delete withOption(OptionClause option) {
return setOption(option);
}
public void setLimit(Limit limit) {
this.limit = limit;
}
public List<Table> getTables() {
return tables;
}
public void setTables(List<Table> tables) {
this.tables = tables;
}
/**
* This is compatible with the old logic. When calling this method, you need to ensure that the
* specific table is used after using.
*
* @return Table collection used in using.
*/
@Deprecated
public List<Table> getUsingList() {
if (usingFromItemList == null || usingFromItemList.isEmpty()) {
return new ArrayList<>();
}
return usingFromItemList.stream().map(ele -> (Table) ele).collect(Collectors.toList());
}
/**
* This is compatible with the old logic. When calling this method, you need to ensure that the
* specific table is used after using.
*
* @param usingList Table collection used in using.
*/
@Deprecated
public void setUsingList(List<Table> usingList) {
this.usingFromItemList = new ArrayList<>(usingList);
}
public List<Join> getJoins() {
return joins;
}
public void setJoins(List<Join> joins) {
this.joins = joins;
}
public boolean isHasFrom() {
return this.hasFrom;
}
public void setHasFrom(boolean hasFrom) {
this.hasFrom = hasFrom;
}
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public String toString() {
StringBuilder b = new StringBuilder();
if (withItemsList != null && !withItemsList.isEmpty()) {
b.append("WITH ");
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem<?> withItem = iter.next();
b.append(withItem);
if (iter.hasNext()) {
b.append(",");
}
b.append(" ");
}
}
b.append("DELETE");
if (oracleHint != null) {
b.append(oracleHint).append(" ");
}
if (modifierPriority != null) {
b.append(" ").append(modifierPriority.name());
}
if (modifierQuick) {
b.append(" QUICK");
}
if (modifierIgnore) {
b.append(" IGNORE");
}
if (tables != null && tables.size() > 0) {
b.append(" ");
b.append(tables.stream()
.map(Table::toString)
.collect(joining(", ")));
}
if (outputClause != null) {
outputClause.appendTo(b);
}
if (hasFrom) {
b.append(" FROM");
}
tableReference.appendWithAliasTo(b.append(" "));
if (forPortionClause != null) {
b.append(' ').append(forPortionClause);
}
appendJoinsTo(b, joins);
if (usingFromItemList != null && !usingFromItemList.isEmpty()) {
b.append(" USING ");
b.append(usingFromItemList.stream()
.map(Object::toString)
.collect(joining(", ")));
appendJoinsTo(b, usingJoins);
}
if (cursorName != null) {
b.append(" WHERE CURRENT OF ").append(cursorName);
} else if (where != null) {
b.append(" WHERE ").append(where);
}
if (preferringClause != null) {
b.append(" ").append(preferringClause);
}
if (orderByElements != null) {
b.append(PlainSelect.orderByToString(orderByElements));
}
if (limit != null) {
b.append(limit);
}
if (option != null) {
b.append(option);
}
if (returningClause != null) {
returningClause.appendTo(b);
}
return b.toString();
}
public Delete withTables(List<Table> tables) {
this.setTables(tables);
return this;
}
/**
* The old method has been replaced by withUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#withUsingFromItemList
*/
@Deprecated
public Delete withUsingList(List<Table> usingList) {
this.setUsingList(usingList);
return this;
}
/**
* New using syntax method.Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete withUsingFromItemList(List<FromItem> usingFromItemList) {
this.setUsingFromItemList(usingFromItemList);
return this;
}
public Delete withJoins(List<Join> joins) {
this.setJoins(joins);
return this;
}
public Delete withLimit(Limit limit) {
this.setLimit(limit);
return this;
}
public Delete withOrderByElements(List<OrderByElement> orderByElements) {
this.setOrderByElements(orderByElements);
return this;
}
public Delete withTable(Table table) {
this.setTable(table);
return this;
}
public Delete withWhere(Expression where) {
this.setWhere(where);
return this;
}
public Delete withPreferringClause(PreferringClause preferringClause) {
this.setPreferringClause(preferringClause);
return this;
}
public Delete withHasFrom(boolean hasFrom) {
this.setHasFrom(hasFrom);
return this;
}
public Delete withModifierPriority(DeleteModifierPriority modifierPriority) {
this.setModifierPriority(modifierPriority);
return this;
}
public Delete withModifierIgnore(boolean modifierIgnore) {
this.setModifierIgnore(modifierIgnore);
return this;
}
public Delete withModifierQuick(boolean modifierQuick) {
this.setModifierQuick(modifierQuick);
return this;
}
public DeleteModifierPriority getModifierPriority() {
return modifierPriority;
}
public void setModifierPriority(DeleteModifierPriority modifierPriority) {
this.modifierPriority = modifierPriority;
}
public boolean isModifierIgnore() {
return modifierIgnore;
}
public void setModifierIgnore(boolean modifierIgnore) {
this.modifierIgnore = modifierIgnore;
}
public boolean isModifierQuick() {
return modifierQuick;
}
public void setModifierQuick(boolean modifierQuick) {
this.modifierQuick = modifierQuick;
}
public Delete addTables(Table... tables) {
List<Table> collection = Optional.ofNullable(getTables()).orElseGet(ArrayList::new);
Collections.addAll(collection, tables);
return this.withTables(collection);
}
public Delete addTables(Collection<? extends Table> tables) {
List<Table> collection = Optional.ofNullable(getTables()).orElseGet(ArrayList::new);
collection.addAll(tables);
return this.withTables(collection);
}
/**
* The old method has been replaced by addUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#addUsingFromItemList
*/
@Deprecated
public Delete addUsingList(Table... usingList) {
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
Collections.addAll(collection, usingList);
return this.withUsingList(collection);
}
/**
* New using syntax method.Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete addUsingFromItemList(FromItem... usingFromItemList) {
List<FromItem> collection =
Optional.ofNullable(getUsingFromItemList()).orElseGet(ArrayList::new);
Collections.addAll(collection, usingFromItemList);
return this.withUsingFromItemList(collection);
}
/**
* The old method has been replaced by addUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#addUsingFromItemList
*/
@Deprecated
public Delete addUsingList(Collection<? extends Table> usingList) {
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
collection.addAll(usingList);
return this.withUsingList(collection);
}
/**
* New using syntax method. Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete addUsingFromItemList(Collection<? extends Table> usingFromItemList) {
List<FromItem> collection =
Optional.ofNullable(getUsingFromItemList()).orElseGet(ArrayList::new);
collection.addAll(usingFromItemList);
return this.withUsingFromItemList(collection);
}
public Delete addJoins(Join... joins) {
List<Join> collection = Optional.ofNullable(getJoins()).orElseGet(ArrayList::new);
Collections.addAll(collection, joins);
return this.withJoins(collection);
}
public Delete addJoins(Collection<? extends Join> joins) {
List<Join> collection = Optional.ofNullable(getJoins()).orElseGet(ArrayList::new);
collection.addAll(joins);
return this.withJoins(collection);
}
public Delete addOrderByElements(OrderByElement... orderByElements) {
List<OrderByElement> collection =
Optional.ofNullable(getOrderByElements()).orElseGet(ArrayList::new);
Collections.addAll(collection, orderByElements);
return this.withOrderByElements(collection);
}
public Delete addOrderByElements(Collection<? extends OrderByElement> orderByElements) {
List<OrderByElement> collection =
Optional.ofNullable(getOrderByElements()).orElseGet(ArrayList::new);
collection.addAll(orderByElements);
return this.withOrderByElements(collection);
}
public <E extends Expression> E getWhere(Class<E> type) {
return type.cast(getWhere());
}
/**
* Return the content after using. Supports the complete using syntax of pg, such as subqueries,
* etc.
*
* @return
*/
public List<FromItem> getUsingFromItemList() {
return usingFromItemList;
}
/**
* Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList The content after using.
*/
public void setUsingFromItemList(List<FromItem> usingFromItemList) {
this.usingFromItemList = usingFromItemList;
}
/**
* Returns the joins after the USING items, as in MySQL's
* {@code DELETE FROM t1 USING t1 JOIN t2 ON t1.id = t2.id} or PostgreSQL's
* {@code DELETE FROM t USING s JOIN u ON s.id = u.id WHERE ...}, or {@code null}. They are
* rendered only when {@link #getUsingFromItemList()} is not empty. {@link #getJoins()} holds
* the joins of the target table, written before USING.
*/
public List<Join> getUsingJoins() {
return usingJoins;
}
public void setUsingJoins(List<Join> usingJoins) {
this.usingJoins = usingJoins;
}
public Delete withUsingJoins(List<Join> usingJoins) {
setUsingJoins(usingJoins);
return this;
}
public Delete addUsingJoins(Join... usingJoins) {
List<Join> collection = Optional.ofNullable(getUsingJoins()).orElseGet(ArrayList::new);
Collections.addAll(collection, usingJoins);
return this.withUsingJoins(collection);
}
private static void appendJoinsTo(StringBuilder b, List<Join> joins) {
if (joins != null) {
for (Join join : joins) {
if (join.isSimple()) {
b.append(", ").append(join);
} else {
b.append(" ").append(join);
}
}
}
}
}