@@ -307,24 +307,28 @@ public String detectAlias() {
307307 @ Nullable
308308 private String detectAlias (String query ) {
309309
310- if (this .parsedType != ParsedType .SELECT ) {
311- return null ;
312- }
313-
314- Select selectStatement = parseSelectStatement (query );
310+ if (ParsedType .MERGE .equals (this .parsedType )) {
311+ Merge mergeStatement = parseSelectStatement (query , Merge .class );
312+ return detectAlias (mergeStatement );
313+
314+ } else if (ParsedType .SELECT .equals (this .parsedType )) {
315+ Select selectStatement = parseSelectStatement (query );
316+
317+ /*
318+ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
319+ alias since:
320+ * ValuesStatement has no alias
321+ * SetOperation can have multiple alias for each operation item
322+ */
323+ if (!(selectStatement .getSelectBody () instanceof PlainSelect )) {
324+ return null ;
325+ }
315326
316- /*
317- For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
318- alias since:
319- * ValuesStatement has no alias
320- * SetOperation can have multiple alias for each operation item
321- */
322- if (!(selectStatement .getSelectBody () instanceof PlainSelect )) {
323- return null ;
327+ PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
328+ return detectAlias (selectBody );
324329 }
325330
326- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
327- return detectAlias (selectBody );
331+ return null ;
328332 }
329333
330334 /**
@@ -335,7 +339,7 @@ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) i
335339 * @return Might return {@literal null}.
336340 */
337341 @ Nullable
338- private static String detectAlias (PlainSelect selectBody ) {
342+ private String detectAlias (PlainSelect selectBody ) {
339343
340344 if (selectBody .getFromItem () == null ) {
341345 return null ;
@@ -345,6 +349,18 @@ private static String detectAlias(PlainSelect selectBody) {
345349 return alias == null ? null : alias .getName ();
346350 }
347351
352+ /**
353+ * Resolves the alias for the given {@link Merge} statement.
354+ *
355+ * @param mergeStatement must not be {@literal null}.
356+ * @return Might return {@literal null}.
357+ */
358+ @ Nullable
359+ private String detectAlias (Merge mergeStatement ) {
360+ Alias alias = mergeStatement .getUsingAlias ();
361+ return alias == null ? null : alias .getName ();
362+ }
363+
348364 @ Override
349365 public String createCountQueryFor (@ Nullable String countProjection ) {
350366
@@ -453,15 +469,25 @@ public Set<String> getJoinAliases() {
453469 * @param query the query to parse
454470 * @return the parsed query
455471 */
456- private static Select parseSelectStatement (String query ) {
472+ private < T extends Statement > T parseSelectStatement (String query , Class < T > classOfT ) {
457473
458474 try {
459- return ( Select ) CCJSqlParserUtil .parse (query );
475+ return classOfT . cast ( CCJSqlParserUtil .parse (query ) );
460476 } catch (JSQLParserException e ) {
461477 throw new IllegalArgumentException ("The query you provided is not a valid SQL Query!" , e );
462478 }
463479 }
464480
481+ /**
482+ * Parses a query string with JSqlParser.
483+ *
484+ * @param query the query to parse
485+ * @return the parsed query
486+ */
487+ private Select parseSelectStatement (String query ) {
488+ return parseSelectStatement (query , Select .class );
489+ }
490+
465491 /**
466492 * Checks whether a given projection only contains a single column definition (aka without functions, etc.)
467493 *
0 commit comments