Backoff JSqlParserQueryEnhancer if query type is not a supported query type.

We now backoff from enhancing queries if the query type is not supported (e.g. TRUNCATE).

Closes #3038
This commit is contained in:
Mark Paluch
2023-06-26 09:24:36 +02:00
parent bcddc94c85
commit f67dd9b655
2 changed files with 24 additions and 6 deletions

View File

@@ -96,7 +96,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
} else if (statement instanceof Merge) {
return ParsedType.MERGE;
} else {
return ParsedType.SELECT;
return ParsedType.OTHER;
}
} catch (JSQLParserException e) {
throw new IllegalArgumentException("The query you provided is not a valid SQL Query!", e);
@@ -314,10 +314,10 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
Select selectStatement = parseSelectStatement(query);
/*
For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
alias since:
* ValuesStatement has no alias
* SetOperation can have multiple alias for each operation item
* For all the other types ({@link ValuesStatement} and {@link SetOperationList}) it does not make sense to provide
* alias since:
* ValuesStatement has no alias
* SetOperation can have multiple alias for each operation item
*/
if (!(selectStatement.getSelectBody() instanceof PlainSelect)) {
return null;
@@ -516,10 +516,11 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
* <li>{@code ParsedType.SELECT}: means the top level statement is {@link Select}</li>
* <li>{@code ParsedType.INSERT}: means the top level statement is {@link Insert}</li>
* <li>{@code ParsedType.MERGE}: means the top level statement is {@link Merge}</li>
* <li>{@code ParsedType.OTHER}: means the top level statement is a different top-level type</li>
* </ul>
*/
enum ParsedType {
DELETE, UPDATE, SELECT, INSERT, MERGE;
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER;
}
}

View File

@@ -191,6 +191,23 @@ public class JSqlParserQueryEnhancerUnitTests extends QueryEnhancerTckTests {
assertThat(queryEnhancer.hasConstructorExpression()).isFalse();
}
@Test // GH-3038
void truncateStatementShouldWork() {
StringQuery stringQuery = new StringQuery("TRUNCATE TABLE foo", true);
QueryEnhancer queryEnhancer = QueryEnhancerFactory.forQuery(stringQuery);
assertThat(stringQuery.getAlias()).isNull();
assertThat(stringQuery.getProjection()).isEmpty();
assertThat(stringQuery.hasConstructorExpression()).isFalse();
assertThat(queryEnhancer.applySorting(Sort.by("day").descending())).isEqualTo("TRUNCATE TABLE foo");
assertThat(queryEnhancer.getJoinAliases()).isEmpty();
assertThat(queryEnhancer.detectAlias()).isNull();
assertThat(queryEnhancer.getProjection()).isEmpty();
assertThat(queryEnhancer.hasConstructorExpression()).isFalse();
}
@ParameterizedTest // GH-2641
@MethodSource("mergeStatementWorksSource")
void mergeStatementWorksWithJSqlParser(String query, String alias) {