forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelect.java
More file actions
483 lines (394 loc) · 12.9 KB
/
Select.java
File metadata and controls
483 lines (394 loc) · 12.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
471
472
473
474
475
476
477
478
479
480
481
482
483
/*-
* #%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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
public abstract class Select extends ASTNodeAccessImpl implements Statement, Expression, FromItem {
protected Table forUpdateTable = null;
protected List<WithItem<?>> withItemsList;
Limit limitBy;
Limit limit;
Offset offset;
Fetch fetch;
WithIsolation isolation;
boolean oracleSiblings = false;
ForClause forClause = null;
List<OrderByElement> orderByElements;
ForMode forMode = null;
private boolean skipLocked;
private Wait wait;
private boolean noWait = false;
Alias alias;
Pivot pivot;
UnPivot unPivot;
public static String orderByToString(List<OrderByElement> orderByElements) {
return orderByToString(false, orderByElements);
}
public static String orderByToString(boolean oracleSiblings,
List<OrderByElement> orderByElements) {
return getFormattedList(orderByElements, oracleSiblings ? "ORDER SIBLINGS BY" : "ORDER BY");
}
public static String getFormattedList(List<?> list, String expression) {
return getFormattedList(list, expression, true, false);
}
public static String getFormattedList(List<?> list, String expression, boolean useComma,
boolean useBrackets) {
String sql = getStringList(list, useComma, useBrackets);
if (!sql.isEmpty()) {
if (!expression.isEmpty()) {
sql = " " + expression + " " + sql;
} else {
sql = " " + sql;
}
}
return sql;
}
/**
* List the toString out put of the objects in the List comma separated. If the List is null or
* empty an empty string is returned.
* <p>
* The same as getStringList(list, true, false)
*
* @param list list of objects with toString methods
* @return comma separated list of the elements in the list
* @see #getStringList(List, boolean, boolean)
*/
public static String getStringList(List<?> list) {
return getStringList(list, true, false);
}
/**
* List the toString out put of the objects in the List that can be comma separated. If the List
* is null or empty an empty string is returned.
*
* @param list list of objects with toString methods
* @param useComma true if the list has to be comma separated
* @param useBrackets true if the list has to be enclosed in brackets
* @return comma separated list of the elements in the list
*/
public static String getStringList(List<?> list, boolean useComma, boolean useBrackets) {
return appendStringListTo(new StringBuilder(), list, useComma, useBrackets).toString();
}
/**
* Append the toString out put of the objects in the List (that can be comma separated). If the
* List is null or empty an empty string is returned.
*
* @param list list of objects with toString methods
* @param useComma true if the list has to be comma separated
* @param useBrackets true if the list has to be enclosed in brackets
* @return comma separated list of the elements in the list
*/
public static StringBuilder appendStringListTo(StringBuilder builder, List<?> list,
boolean useComma, boolean useBrackets) {
if (list != null) {
String comma = useComma ? ", " : " ";
if (useBrackets) {
builder.append("(");
}
int size = list.size();
for (int i = 0; i < size; i++) {
builder.append(list.get(i)).append(i < size - 1 ? comma : "");
}
if (useBrackets) {
builder.append(")");
}
}
return builder;
}
public List<WithItem<?>> getWithItemsList() {
return withItemsList;
}
public void setWithItemsList(List<WithItem<?>> withItemsList) {
this.withItemsList = withItemsList;
}
public Select withWithItemsList(List<WithItem<?>> withItemsList) {
this.setWithItemsList(withItemsList);
return this;
}
public Select addWithItemsList(Collection<? extends WithItem<?>> withItemsList) {
List<WithItem<?>> collection =
Optional.ofNullable(getWithItemsList()).orElseGet(ArrayList::new);
collection.addAll(withItemsList);
return this.withWithItemsList(collection);
}
public Select addWithItemsList(WithItem<?>... withItemsList) {
return addWithItemsList(Arrays.asList(withItemsList));
}
public boolean isOracleSiblings() {
return oracleSiblings;
}
public void setOracleSiblings(boolean oracleSiblings) {
this.oracleSiblings = oracleSiblings;
}
public boolean isNoWait() {
return this.noWait;
}
public void setNoWait(boolean noWait) {
this.noWait = noWait;
}
public Select withOracleSiblings(boolean oracleSiblings) {
this.setOracleSiblings(oracleSiblings);
return this;
}
public ForClause getForClause() {
return forClause;
}
public Select setForClause(ForClause forClause) {
this.forClause = forClause;
return this;
}
public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
public void setOrderByElements(List<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
}
public Select withOrderByElements(List<OrderByElement> orderByElements) {
this.setOrderByElements(orderByElements);
return this;
}
public Select addOrderByElements(Collection<? extends OrderByElement> orderByElements) {
List<OrderByElement> collection =
Optional.ofNullable(getOrderByElements()).orElseGet(ArrayList::new);
collection.addAll(orderByElements);
return this.withOrderByElements(collection);
}
public Select addOrderByElements(OrderByElement... orderByElements) {
return this.addOrderByElements(Arrays.asList(orderByElements));
}
public Select addOrderByExpressions(Collection<Expression> orderByExpressions) {
for (Expression e : orderByExpressions) {
addOrderByElements(new OrderByElement().withExpression(e));
}
return this;
}
public Select addOrderByElements(Expression... orderByExpressions) {
return addOrderByExpressions(Arrays.asList(orderByExpressions));
}
public Limit getLimit() {
return limit;
}
public void setLimit(Limit limit) {
this.limit = limit;
}
public Select withLimit(Limit limit) {
this.setLimit(limit);
return this;
}
public Limit getLimitBy() {
return limitBy;
}
public void setLimitBy(Limit limitBy) {
this.limitBy = limitBy;
}
public <E extends Select> E withLimitBy(Class<E> type, Limit limitBy) {
this.setLimitBy(limitBy);
return type.cast(this);
}
public Offset getOffset() {
return offset;
}
public void setOffset(Offset offset) {
this.offset = offset;
}
public Select withOffset(Offset offset) {
this.setOffset(offset);
return this;
}
public Fetch getFetch() {
return fetch;
}
public void setFetch(Fetch fetch) {
this.fetch = fetch;
}
public Select withFetch(Fetch fetch) {
this.setFetch(fetch);
return this;
}
public WithIsolation getIsolation() {
return isolation;
}
public void setIsolation(WithIsolation isolation) {
this.isolation = isolation;
}
public Select withIsolation(WithIsolation isolation) {
this.setIsolation(isolation);
return this;
}
public ForMode getForMode() {
return this.forMode;
}
public void setForMode(ForMode forMode) {
this.forMode = forMode;
}
public Table getForUpdateTable() {
return this.forUpdateTable;
}
public void setForUpdateTable(Table forUpdateTable) {
this.forUpdateTable = forUpdateTable;
}
/**
* Returns the value of the {@link Wait} set for this SELECT
*
* @return the value of the {@link Wait} set for this SELECT
*/
public Wait getWait() {
return wait;
}
/**
* Sets the {@link Wait} for this SELECT
*
* @param wait the {@link Wait} for this SELECT
*/
public void setWait(final Wait wait) {
this.wait = wait;
}
public boolean isSkipLocked() {
return skipLocked;
}
public void setSkipLocked(boolean skipLocked) {
this.skipLocked = skipLocked;
}
@Override
public Alias getAlias() {
return alias;
}
@Override
public void setAlias(Alias alias) {
this.alias = alias;
}
public Select withAlias(Alias alias) {
this.setAlias(alias);
return this;
}
@Override
public Pivot getPivot() {
return pivot;
}
@Override
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}
public UnPivot getUnPivot() {
return unPivot;
}
public void setUnPivot(UnPivot unPivot) {
this.unPivot = unPivot;
}
public StringBuilder appendSelectBodyTo(StringBuilder builder) {
return builder;
};
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public StringBuilder appendTo(StringBuilder builder) {
if (withItemsList != null && !withItemsList.isEmpty()) {
builder.append("WITH ");
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
WithItem withItem = iter.next();
builder.append(withItem);
if (iter.hasNext()) {
builder.append(",");
}
builder.append(" ");
}
}
appendSelectBodyTo(builder);
appendTo(builder, alias, null, pivot, unPivot);
builder.append(orderByToString(oracleSiblings, orderByElements));
if (forClause != null) {
forClause.appendTo(builder);
}
if (limitBy != null) {
builder.append(limitBy);
}
if (limit != null) {
builder.append(limit);
}
if (offset != null) {
builder.append(offset);
}
if (fetch != null) {
builder.append(fetch);
}
if (isolation != null) {
builder.append(isolation);
}
if (forMode != null) {
builder.append(" FOR ");
builder.append(forMode.getValue());
if (getForUpdateTable() != null) {
builder.append(" OF ").append(forUpdateTable);
}
if (wait != null) {
// Wait's toString will do the formatting for us
builder.append(wait);
}
if (isNoWait()) {
builder.append(" NOWAIT");
} else if (isSkipLocked()) {
builder.append(" SKIP LOCKED");
}
}
return builder;
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
public abstract <T, S> T accept(SelectVisitor<T> selectVisitor, S context);
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}
@Deprecated
public Select getSelectBody() {
return this;
}
public Values getValues() {
return (Values) this;
}
public PlainSelect getPlainSelect() {
return (PlainSelect) this;
}
public SetOperationList getSetOperationList() {
return (SetOperationList) this;
}
public <E extends Select> E as(Class<E> type) {
return type.cast(this);
}
public Select withForMode(ForMode forMode) {
this.setForMode(forMode);
return this;
}
public Select withForUpdateTable(Table forUpdateTable) {
this.setForUpdateTable(forUpdateTable);
return this;
}
public Select withSkipLocked(boolean skipLocked) {
this.setSkipLocked(skipLocked);
return this;
}
public Select withWait(Wait wait) {
this.setWait(wait);
return this;
}
}