15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
18
- import static org .springframework .data .jpa .repository .query .JSqlParserUtils .*;
19
- import static org .springframework .data .jpa .repository .query .QueryUtils .*;
20
-
21
- import java .util .ArrayList ;
22
- import java .util .Collections ;
23
- import java .util .HashSet ;
24
- import java .util .List ;
25
- import java .util .Objects ;
26
- import java .util .Set ;
27
- import java .util .stream .Collectors ;
28
-
29
- import org .springframework .data .domain .Sort ;
30
- import org .springframework .lang .Nullable ;
31
- import org .springframework .util .Assert ;
32
- import org .springframework .util .CollectionUtils ;
33
- import org .springframework .util .StringUtils ;
18
+ import static org .springframework .data .jpa .repository .query .JSqlParserUtils .getJSqlCount ;
19
+ import static org .springframework .data .jpa .repository .query .JSqlParserUtils .getJSqlLower ;
20
+ import static org .springframework .data .jpa .repository .query .QueryUtils .checkSortExpression ;
34
21
35
22
import net .sf .jsqlparser .JSQLParserException ;
36
23
import net .sf .jsqlparser .expression .Alias ;
42
29
import net .sf .jsqlparser .statement .delete .Delete ;
43
30
import net .sf .jsqlparser .statement .insert .Insert ;
44
31
import net .sf .jsqlparser .statement .merge .Merge ;
45
- import net .sf .jsqlparser .statement .select .OrderByElement ;
46
- import net .sf .jsqlparser .statement .select .PlainSelect ;
47
- import net .sf .jsqlparser .statement .select .Select ;
48
- import net .sf .jsqlparser .statement .select .SelectBody ;
49
- import net .sf .jsqlparser .statement .select .SelectExpressionItem ;
50
- import net .sf .jsqlparser .statement .select .SelectItem ;
51
- import net .sf .jsqlparser .statement .select .SetOperationList ;
52
- import net .sf .jsqlparser .statement .select .WithItem ;
32
+ import net .sf .jsqlparser .statement .select .*;
53
33
import net .sf .jsqlparser .statement .update .Update ;
54
34
import net .sf .jsqlparser .statement .values .ValuesStatement ;
55
35
36
+ import java .util .*;
37
+ import java .util .stream .Collectors ;
38
+
39
+ import org .springframework .data .domain .Sort ;
40
+ import org .springframework .lang .Nullable ;
41
+ import org .springframework .util .Assert ;
42
+ import org .springframework .util .CollectionUtils ;
43
+ import org .springframework .util .StringUtils ;
44
+
56
45
/**
57
46
* The implementation of {@link QueryEnhancer} using JSqlParser.
58
47
*
@@ -147,7 +136,7 @@ public String applySorting(Sort sort, @Nullable String alias) {
147
136
148
137
/**
149
138
* Returns the {@link SetOperationList} as a string query with {@link Sort}s applied in the right order.
150
- *
139
+ *
151
140
* @param setOperationListStatement
152
141
* @param sort
153
142
* @return
@@ -305,14 +294,16 @@ public String detectAlias() {
305
294
private String detectAlias (String query ) {
306
295
307
296
if (ParsedType .MERGE .equals (this .parsedType )) {
297
+
308
298
Merge mergeStatement = parseSelectStatement (query , Merge .class );
309
299
return detectAlias (mergeStatement );
310
300
311
301
} else if (ParsedType .SELECT .equals (this .parsedType )) {
302
+
312
303
Select selectStatement = parseSelectStatement (query );
313
304
314
305
/*
315
- For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
306
+ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
316
307
alias since:
317
308
* ValuesStatement has no alias
318
309
* SetOperation can have multiple alias for each operation item
@@ -354,6 +345,7 @@ private String detectAlias(PlainSelect selectBody) {
354
345
*/
355
346
@ Nullable
356
347
private String detectAlias (Merge mergeStatement ) {
348
+
357
349
Alias alias = mergeStatement .getUsingAlias ();
358
350
return alias == null ? null : alias .getName ();
359
351
}
@@ -382,6 +374,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
382
374
selectBody .setOrderByElements (null );
383
375
384
376
if (StringUtils .hasText (countProjection )) {
377
+
385
378
Function jSqlCount = getJSqlCount (Collections .singletonList (countProjection ), false );
386
379
selectBody .setSelectItems (Collections .singletonList (new SelectExpressionItem (jSqlCount )));
387
380
return selectBody .toString ();
@@ -396,6 +389,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
396
389
List <SelectItem > selectItems = selectBody .getSelectItems ();
397
390
398
391
if (onlyASingleColumnProjection (selectItems )) {
392
+
399
393
SelectExpressionItem singleProjection = (SelectExpressionItem ) selectItems .get (0 );
400
394
401
395
Column column = (Column ) singleProjection .getExpression ();
@@ -440,6 +434,7 @@ public String getProjection() {
440
434
SelectBody selectBody = selectStatement .getSelectBody ();
441
435
442
436
if (selectStatement .getSelectBody ()instanceof SetOperationList setOperationList ) {
437
+
443
438
// using the first one since for setoperations the projection has to be the same
444
439
selectBody = setOperationList .getSelects ().get (0 );
445
440
0 commit comments