1818import static org .springframework .data .jpa .repository .query .JSqlParserUtils .*;
1919import static org .springframework .data .jpa .repository .query .QueryUtils .*;
2020
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+
2135import net .sf .jsqlparser .JSQLParserException ;
2236import net .sf .jsqlparser .expression .Alias ;
2337import net .sf .jsqlparser .expression .Expression ;
2640import net .sf .jsqlparser .schema .Column ;
2741import net .sf .jsqlparser .statement .Statement ;
2842import net .sf .jsqlparser .statement .delete .Delete ;
29- import net .sf .jsqlparser .statement .merge .Merge ;
3043import net .sf .jsqlparser .statement .insert .Insert ;
44+ import net .sf .jsqlparser .statement .merge .Merge ;
3145import net .sf .jsqlparser .statement .select .OrderByElement ;
3246import net .sf .jsqlparser .statement .select .PlainSelect ;
3347import net .sf .jsqlparser .statement .select .Select ;
3953import net .sf .jsqlparser .statement .update .Update ;
4054import net .sf .jsqlparser .statement .values .ValuesStatement ;
4155
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-
5656/**
5757 * The implementation of {@link QueryEnhancer} using JSqlParser.
5858 *
@@ -304,24 +304,28 @@ public String detectAlias() {
304304 @ Nullable
305305 private String detectAlias (String query ) {
306306
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+ }
312323
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 );
321326 }
322327
323- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
324- return detectAlias (selectBody );
328+ return null ;
325329 }
326330
327331 /**
@@ -332,7 +336,7 @@ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) i
332336 * @return Might return {@literal null}.
333337 */
334338 @ Nullable
335- private static String detectAlias (PlainSelect selectBody ) {
339+ private String detectAlias (PlainSelect selectBody ) {
336340
337341 if (selectBody .getFromItem () == null ) {
338342 return null ;
@@ -342,6 +346,18 @@ private static String detectAlias(PlainSelect selectBody) {
342346 return alias == null ? null : alias .getName ();
343347 }
344348
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+
345361 @ Override
346362 public String createCountQueryFor (@ Nullable String countProjection ) {
347363
@@ -449,15 +465,25 @@ public Set<String> getJoinAliases() {
449465 * @param query the query to parse
450466 * @return the parsed query
451467 */
452- private static Select parseSelectStatement (String query ) {
468+ private < T extends Statement > T parseSelectStatement (String query , Class < T > classOfT ) {
453469
454470 try {
455- return ( Select ) CCJSqlParserUtil .parse (query );
471+ return classOfT . cast ( CCJSqlParserUtil .parse (query ) );
456472 } catch (JSQLParserException e ) {
457473 throw new IllegalArgumentException ("The query you provided is not a valid SQL Query" , e );
458474 }
459475 }
460476
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+
461487 /**
462488 * Checks whether a given projection only contains a single column definition (aka without functions, etc.)
463489 *
0 commit comments