Skip to content

Commit

Permalink
Reuse parsed SQL statement for alias and projection detection.
Browse files Browse the repository at this point in the history
Closes #3039
  • Loading branch information
mp911de committed Jun 26, 2023
1 parent 57a6f72 commit 13a4661
Showing 1 changed file with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
public class JSqlParserQueryEnhancer implements QueryEnhancer {

private final DeclaredQuery query;
private final Statement statement;
private final ParsedType parsedType;

/**
Expand All @@ -72,34 +73,34 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
public JSqlParserQueryEnhancer(DeclaredQuery query) {

this.query = query;
this.parsedType = detectParsedType();
try {
this.statement = CCJSqlParserUtil.parse(this.query.getQueryString());
} catch (JSQLParserException e) {
throw new IllegalArgumentException("The query is not a valid SQL Query", e);
}

this.parsedType = detectParsedType(statement);
}

/**
* Detects what type of query is provided.
*
* @return the parsed type
*/
private ParsedType detectParsedType() {

try {
Statement statement = CCJSqlParserUtil.parse(this.query.getQueryString());

if (statement instanceof Insert) {
return ParsedType.INSERT;
} else if (statement instanceof Update) {
return ParsedType.UPDATE;
} else if (statement instanceof Delete) {
return ParsedType.DELETE;
} else if (statement instanceof Select) {
return ParsedType.SELECT;
} else if (statement instanceof Merge) {
return ParsedType.MERGE;
} else {
return ParsedType.OTHER;
}
} catch (JSQLParserException e) {
throw new IllegalArgumentException("The query you provided is not a valid SQL Query!", e);
private static ParsedType detectParsedType(Statement statement) {

if (statement instanceof Insert) {
return ParsedType.INSERT;
} else if (statement instanceof Update) {
return ParsedType.UPDATE;
} else if (statement instanceof Delete) {
return ParsedType.DELETE;
} else if (statement instanceof Select) {
return ParsedType.SELECT;
} else if (statement instanceof Merge) {
return ParsedType.MERGE;
} else {
return ParsedType.OTHER;
}
}

Expand Down Expand Up @@ -127,9 +128,8 @@ public String applySorting(Sort sort, @Nullable String alias) {

PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();

final Set<String> joinAliases = getJoinAliases(selectBody);

final Set<String> selectionAliases = getSelectionAliases(selectBody);
Set<String> joinAliases = getJoinAliases(selectBody);
Set<String> selectionAliases = getSelectionAliases(selectBody);

List<OrderByElement> orderByElements = sort.stream() //
.map(order -> getOrderClause(joinAliases, selectionAliases, alias, order)) //
Expand Down Expand Up @@ -203,7 +203,7 @@ Set<String> getSelectionAliases() {
return new HashSet<>();
}

Select selectStatement = parseSelectStatement(this.query.getQueryString());
Select selectStatement = (Select) statement;
PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();
return this.getSelectionAliases(selectBody);
}
Expand All @@ -220,7 +220,7 @@ private Set<String> getJoinAliases(String query) {
return new HashSet<>();
}

Select selectStatement = parseSelectStatement(query);
Select selectStatement = (Select) statement;
if (selectStatement.getSelectBody()instanceof PlainSelect selectBody) {
return getJoinAliases(selectBody);
}
Expand Down Expand Up @@ -306,24 +306,23 @@ private String detectAlias(String query) {

if (ParsedType.MERGE.equals(this.parsedType)) {

Merge mergeStatement = parseSelectStatement(query, Merge.class);
Merge mergeStatement = (Merge) statement;
return detectAlias(mergeStatement);

} else if (ParsedType.SELECT.equals(this.parsedType)) {

Select selectStatement = parseSelectStatement(query);
Select selectStatement = (Select) statement;

/*
* 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)) {
if (!(selectStatement.getSelectBody()instanceof PlainSelect selectBody)) {
return null;
}

PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();
return detectAlias(selectBody);
}

Expand Down Expand Up @@ -375,12 +374,10 @@ public String createCountQueryFor(@Nullable String countProjection) {
/*
We only support count queries for {@link PlainSelect}.
*/
if (!(selectStatement.getSelectBody() instanceof PlainSelect)) {
if (!(selectStatement.getSelectBody()instanceof PlainSelect selectBody)) {
return this.query.getQueryString();
}

PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();

// remove order by
selectBody.setOrderByElements(null);

Expand Down Expand Up @@ -436,7 +433,7 @@ public String getProjection() {

Assert.hasText(query.getQueryString(), "Query must not be null or empty");

Select selectStatement = parseSelectStatement(query.getQueryString());
Select selectStatement = (Select) statement;

if (selectStatement.getSelectBody() instanceof ValuesStatement) {
return "";
Expand Down

0 comments on commit 13a4661

Please sign in to comment.