31
31
import net .sf .jsqlparser .statement .select .OrderByElement ;
32
32
import net .sf .jsqlparser .statement .select .PlainSelect ;
33
33
import net .sf .jsqlparser .statement .select .Select ;
34
- import net .sf .jsqlparser .statement .select .SelectBody ;
35
- import net .sf .jsqlparser .statement .select .SelectExpressionItem ;
36
34
import net .sf .jsqlparser .statement .select .SelectItem ;
37
35
import net .sf .jsqlparser .statement .select .SetOperationList ;
38
- import net .sf .jsqlparser .statement .select .WithItem ;
36
+ import net .sf .jsqlparser .statement .select .Values ;
39
37
import net .sf .jsqlparser .statement .update .Update ;
40
- import net .sf .jsqlparser .statement .values .ValuesStatement ;
41
38
42
39
import java .util .ArrayList ;
43
40
import java .util .Collections ;
59
56
* @author Diego Krupitza
60
57
* @author Greg Turnquist
61
58
* @author Geoffrey Deremetz
59
+ * @author Yanming Zhou
62
60
* @since 2.7.0
63
61
*/
64
62
public class JSqlParserQueryEnhancer implements QueryEnhancer {
@@ -120,13 +118,13 @@ public String applySorting(Sort sort, @Nullable String alias) {
120
118
121
119
Select selectStatement = parseSelectStatement (queryString );
122
120
123
- if (selectStatement . getSelectBody () instanceof SetOperationList setOperationList ) {
121
+ if (selectStatement instanceof SetOperationList setOperationList ) {
124
122
return applySortingToSetOperationList (setOperationList , sort );
125
- } else if (!(selectStatement .getSelectBody () instanceof PlainSelect )) {
126
- return queryString ;
127
123
}
128
124
129
- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
125
+ if (!(selectStatement instanceof PlainSelect selectBody )) {
126
+ return queryString ;
127
+ }
130
128
131
129
Set <String > joinAliases = getJoinAliases (selectBody );
132
130
Set <String > selectionAliases = getSelectionAliases (selectBody );
@@ -155,7 +153,7 @@ public String applySorting(Sort sort, @Nullable String alias) {
155
153
private String applySortingToSetOperationList (SetOperationList setOperationListStatement , Sort sort ) {
156
154
157
155
// special case: ValuesStatements are detected as nested OperationListStatements
158
- if (setOperationListStatement .getSelects ().stream ().anyMatch (ValuesStatement .class ::isInstance )) {
156
+ if (setOperationListStatement .getSelects ().stream ().anyMatch (Values .class ::isInstance )) {
159
157
return setOperationListStatement .toString ();
160
158
}
161
159
@@ -185,8 +183,8 @@ private Set<String> getSelectionAliases(PlainSelect selectBody) {
185
183
}
186
184
187
185
return selectBody .getSelectItems ().stream () //
188
- .filter (SelectExpressionItem .class ::isInstance ) //
189
- .map (item -> ((SelectExpressionItem ) item ).getAlias ()) //
186
+ .filter (SelectItem .class ::isInstance ) //
187
+ .map (item -> ((SelectItem ) item ).getAlias ()) //
190
188
.filter (Objects ::nonNull ) //
191
189
.map (Alias ::getName ) //
192
190
.collect (Collectors .toSet ());
@@ -203,9 +201,7 @@ Set<String> getSelectionAliases() {
203
201
return new HashSet <>();
204
202
}
205
203
206
- Select selectStatement = (Select ) statement ;
207
- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
208
- return this .getSelectionAliases (selectBody );
204
+ return this .getSelectionAliases ((PlainSelect ) statement );
209
205
}
210
206
211
207
/**
@@ -221,7 +217,7 @@ private Set<String> getJoinAliases(String query) {
221
217
}
222
218
223
219
Select selectStatement = (Select ) statement ;
224
- if (selectStatement . getSelectBody () instanceof PlainSelect selectBody ) {
220
+ if (selectStatement instanceof PlainSelect selectBody ) {
225
221
return getJoinAliases (selectBody );
226
222
}
227
223
@@ -319,7 +315,7 @@ private String detectAlias(String query) {
319
315
* ValuesStatement has no alias
320
316
* SetOperation can have multiple alias for each operation item
321
317
*/
322
- if (!(selectStatement . getSelectBody () instanceof PlainSelect selectBody )) {
318
+ if (!(selectStatement instanceof PlainSelect selectBody )) {
323
319
return null ;
324
320
}
325
321
@@ -374,7 +370,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
374
370
/*
375
371
We only support count queries for {@link PlainSelect}.
376
372
*/
377
- if (!(selectStatement . getSelectBody () instanceof PlainSelect selectBody )) {
373
+ if (!(selectStatement instanceof PlainSelect selectBody )) {
378
374
return this .query .getQueryString ();
379
375
}
380
376
@@ -384,7 +380,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
384
380
if (StringUtils .hasText (countProjection )) {
385
381
386
382
Function jSqlCount = getJSqlCount (Collections .singletonList (countProjection ), false );
387
- selectBody .setSelectItems (Collections .singletonList (new SelectExpressionItem (jSqlCount )));
383
+ selectBody .setSelectItems (Collections .singletonList (new SelectItem (jSqlCount )));
388
384
return selectBody .toString ();
389
385
}
390
386
@@ -394,34 +390,26 @@ public String createCountQueryFor(@Nullable String countProjection) {
394
390
String tableAlias = detectAlias (selectBody );
395
391
396
392
// is never null
397
- List <SelectItem > selectItems = selectBody .getSelectItems ();
393
+ List <SelectItem <?> > selectItems = selectBody .getSelectItems ();
398
394
399
395
if (onlyASingleColumnProjection (selectItems )) {
400
396
401
- SelectExpressionItem singleProjection = (SelectExpressionItem ) selectItems .get (0 );
397
+ SelectItem <?> singleProjection = (SelectItem <?> ) selectItems .get (0 );
402
398
403
399
Column column = (Column ) singleProjection .getExpression ();
404
400
String countProp = column .getFullyQualifiedName ();
405
401
406
402
Function jSqlCount = getJSqlCount (Collections .singletonList (countProp ), distinct );
407
- selectBody .setSelectItems (Collections .singletonList (new SelectExpressionItem (jSqlCount )));
403
+ selectBody .setSelectItems (Collections .singletonList (new SelectItem <> (jSqlCount )));
408
404
return selectBody .toString ();
409
405
}
410
406
411
407
String countProp = query .isNativeQuery () ? (distinct ? "*" : "1" ) : tableAlias == null ? "*" : tableAlias ;
412
408
413
409
Function jSqlCount = getJSqlCount (Collections .singletonList (countProp ), distinct );
414
- selectBody .setSelectItems (Collections .singletonList (new SelectExpressionItem (jSqlCount )));
415
-
416
- if (CollectionUtils .isEmpty (selectStatement .getWithItemsList ())) {
417
- return selectBody .toString ();
418
- }
419
-
420
- String withStatements = selectStatement .getWithItemsList ().stream () //
421
- .map (WithItem ::toString ) //
422
- .collect (Collectors .joining ("," ));
410
+ selectBody .setSelectItems (Collections .singletonList (new SelectItem (jSqlCount )));
423
411
424
- return "with " + withStatements + " \n " + selectBody ;
412
+ return selectBody . toString () ;
425
413
}
426
414
427
415
@ Override
@@ -435,13 +423,13 @@ public String getProjection() {
435
423
436
424
Select selectStatement = (Select ) statement ;
437
425
438
- if (selectStatement . getSelectBody () instanceof ValuesStatement ) {
426
+ if (selectStatement instanceof Values ) {
439
427
return "" ;
440
428
}
441
429
442
- SelectBody selectBody = selectStatement . getSelectBody () ;
430
+ Select selectBody = selectStatement ;
443
431
444
- if (selectStatement . getSelectBody () instanceof SetOperationList setOperationList ) {
432
+ if (selectStatement instanceof SetOperationList setOperationList ) {
445
433
446
434
// using the first one since for setoperations the projection has to be the same
447
435
selectBody = setOperationList .getSelects ().get (0 );
@@ -493,11 +481,11 @@ private Select parseSelectStatement(String query) {
493
481
* @param projection the projection to analyse
494
482
* @return <code>true</code> when the projection only contains a single column definition otherwise <code>false</code>
495
483
*/
496
- private boolean onlyASingleColumnProjection (List <SelectItem > projection ) {
484
+ private boolean onlyASingleColumnProjection (List <SelectItem <?> > projection ) {
497
485
498
486
// this is unfortunately the only way to check without any hacky & hard string regex magic
499
- return projection .size () == 1 && projection .get (0 ) instanceof SelectExpressionItem
500
- && (((SelectExpressionItem ) projection .get (0 )).getExpression ()) instanceof Column ;
487
+ return projection .size () == 1 && projection .get (0 ) instanceof SelectItem <?>
488
+ && (((SelectItem <?> ) projection .get (0 )).getExpression ()) instanceof Column ;
501
489
}
502
490
503
491
@ Override
0 commit comments