@@ -307,24 +307,28 @@ public String detectAlias() {
307
307
@ Nullable
308
308
private String detectAlias (String query ) {
309
309
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
+ }
315
326
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 );
324
329
}
325
330
326
- PlainSelect selectBody = (PlainSelect ) selectStatement .getSelectBody ();
327
- return detectAlias (selectBody );
331
+ return null ;
328
332
}
329
333
330
334
/**
@@ -335,7 +339,7 @@ For all the other types ({@link ValuesStatement} and {@link SetOperationList}) i
335
339
* @return Might return {@literal null}.
336
340
*/
337
341
@ Nullable
338
- private static String detectAlias (PlainSelect selectBody ) {
342
+ private String detectAlias (PlainSelect selectBody ) {
339
343
340
344
if (selectBody .getFromItem () == null ) {
341
345
return null ;
@@ -345,6 +349,18 @@ private static String detectAlias(PlainSelect selectBody) {
345
349
return alias == null ? null : alias .getName ();
346
350
}
347
351
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
+
348
364
@ Override
349
365
public String createCountQueryFor (@ Nullable String countProjection ) {
350
366
@@ -453,15 +469,25 @@ public Set<String> getJoinAliases() {
453
469
* @param query the query to parse
454
470
* @return the parsed query
455
471
*/
456
- private static Select parseSelectStatement (String query ) {
472
+ private < T extends Statement > T parseSelectStatement (String query , Class < T > classOfT ) {
457
473
458
474
try {
459
- return ( Select ) CCJSqlParserUtil .parse (query );
475
+ return classOfT . cast ( CCJSqlParserUtil .parse (query ) );
460
476
} catch (JSQLParserException e ) {
461
477
throw new IllegalArgumentException ("The query you provided is not a valid SQL Query!" , e );
462
478
}
463
479
}
464
480
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
+
465
491
/**
466
492
* Checks whether a given projection only contains a single column definition (aka without functions, etc.)
467
493
*
0 commit comments