18
18
import static org .springframework .data .jpa .repository .query .JSqlParserUtils .*;
19
19
import static org .springframework .data .jpa .repository .query .QueryUtils .*;
20
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 ;
34
+
21
35
import net .sf .jsqlparser .JSQLParserException ;
22
36
import net .sf .jsqlparser .expression .Alias ;
23
37
import net .sf .jsqlparser .expression .Expression ;
26
40
import net .sf .jsqlparser .schema .Column ;
27
41
import net .sf .jsqlparser .statement .Statement ;
28
42
import net .sf .jsqlparser .statement .delete .Delete ;
29
- import net .sf .jsqlparser .statement .merge .Merge ;
30
43
import net .sf .jsqlparser .statement .insert .Insert ;
44
+ import net .sf .jsqlparser .statement .merge .Merge ;
31
45
import net .sf .jsqlparser .statement .select .OrderByElement ;
32
46
import net .sf .jsqlparser .statement .select .PlainSelect ;
33
47
import net .sf .jsqlparser .statement .select .Select ;
39
53
import net .sf .jsqlparser .statement .update .Update ;
40
54
import net .sf .jsqlparser .statement .values .ValuesStatement ;
41
55
42
- import java .util .ArrayList ;
43
- import java .util .Collections ;
44
- import java .util .HashSet ;
45
- import java .util .List ;
46
- import java .util .Objects ;
47
- import java .util .Set ;
48
- import java .util .stream .Collectors ;
49
-
50
- import org .springframework .data .domain .Sort ;
51
- import org .springframework .lang .Nullable ;
52
- import org .springframework .util .Assert ;
53
- import org .springframework .util .CollectionUtils ;
54
- import org .springframework .util .StringUtils ;
55
-
56
56
/**
57
57
* The implementation of {@link QueryEnhancer} using JSqlParser.
58
58
*
@@ -304,24 +304,28 @@ public String detectAlias() {
304
304
@ Nullable
305
305
private String detectAlias (String query ) {
306
306
307
- if (this .parsedType != ParsedType .SELECT ) {
308
- return null ;
309
- }
310
-
311
- Select selectStatement = parseSelectStatement (query );
307
+ if (ParsedType .MERGE .equals (this .parsedType )) {
308
+ Merge mergeStatement = parseSelectStatement (query , Merge .class );
309
+ return detectAlias (mergeStatement );
310
+
311
+ } else if (ParsedType .SELECT .equals (this .parsedType )) {
312
+ Select selectStatement = parseSelectStatement (query );
313
+
314
+ /*
315
+ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
316
+ alias since:
317
+ * ValuesStatement has no alias
318
+ * SetOperation can have multiple alias for each operation item
319
+ */
320
+ if (!(selectStatement .getSelectBody () instanceof PlainSelect )) {
321
+ return null ;
322
+ }
312
323
313
- /*
314
- For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
315
- alias since:
316
- * ValuesStatement has no alias
317
- * SetOperation can have multiple alias for each operation item
318
- */
319
- if (!(selectStatement .getSelectBody () instanceof PlainSelect )) {
320
- return null ;
324
+ PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
325
+ return detectAlias (selectBody );
321
326
}
322
327
323
- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
324
- return detectAlias (selectBody );
328
+ return null ;
325
329
}
326
330
327
331
/**
@@ -332,7 +336,7 @@ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) i
332
336
* @return Might return {@literal null}.
333
337
*/
334
338
@ Nullable
335
- private static String detectAlias (PlainSelect selectBody ) {
339
+ private String detectAlias (PlainSelect selectBody ) {
336
340
337
341
if (selectBody .getFromItem () == null ) {
338
342
return null ;
@@ -342,6 +346,18 @@ private static String detectAlias(PlainSelect selectBody) {
342
346
return alias == null ? null : alias .getName ();
343
347
}
344
348
349
+ /**
350
+ * Resolves the alias for the given {@link Merge} statement.
351
+ *
352
+ * @param mergeStatement must not be {@literal null}.
353
+ * @return Might return {@literal null}.
354
+ */
355
+ @ Nullable
356
+ private String detectAlias (Merge mergeStatement ) {
357
+ Alias alias = mergeStatement .getUsingAlias ();
358
+ return alias == null ? null : alias .getName ();
359
+ }
360
+
345
361
@ Override
346
362
public String createCountQueryFor (@ Nullable String countProjection ) {
347
363
@@ -449,15 +465,25 @@ public Set<String> getJoinAliases() {
449
465
* @param query the query to parse
450
466
* @return the parsed query
451
467
*/
452
- private static Select parseSelectStatement (String query ) {
468
+ private < T extends Statement > T parseSelectStatement (String query , Class < T > classOfT ) {
453
469
454
470
try {
455
- return ( Select ) CCJSqlParserUtil .parse (query );
471
+ return classOfT . cast ( CCJSqlParserUtil .parse (query ) );
456
472
} catch (JSQLParserException e ) {
457
473
throw new IllegalArgumentException ("The query you provided is not a valid SQL Query" , e );
458
474
}
459
475
}
460
476
477
+ /**
478
+ * Parses a query string with JSqlParser.
479
+ *
480
+ * @param query the query to parse
481
+ * @return the parsed query
482
+ */
483
+ private Select parseSelectStatement (String query ) {
484
+ return parseSelectStatement (query , Select .class );
485
+ }
486
+
461
487
/**
462
488
* Checks whether a given projection only contains a single column definition (aka without functions, etc.)
463
489
*
0 commit comments