Skip to content

Commit

Permalink
Backoff JSqlParserQueryEnhancer if query type is not a supported quer…
Browse files Browse the repository at this point in the history
…y type.

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

Closes #3038
  • Loading branch information
mp911de committed Jun 26, 2023
1 parent e8d08dd commit 57a6f72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private ParsedType detectParsedType() {
} 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);
Expand Down Expand Up @@ -314,10 +314,10 @@ private String detectAlias(String query) {
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;
Expand Down Expand Up @@ -516,10 +516,11 @@ public DeclaredQuery getQuery() {
* <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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ void multipleWithStatementsWorks() {
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) {
Expand Down

0 comments on commit 57a6f72

Please sign in to comment.