Render entire statement after applying sort.

This commit makes sure to render the entire statement after applying the sort expression via the QueryEnhancer. Previously only parts, the actual statement body, had been considered.

Closes: #3263
Original Pull Request: #3264
This commit is contained in:
Christoph Strobl
2023-12-11 10:44:08 +01:00
parent 4924feabf4
commit fdda1f9c4e
2 changed files with 12 additions and 1 deletions

View File

@@ -141,7 +141,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
selectBody.getOrderByElements().addAll(orderByElements);
return selectBody.toString();
return selectStatement.toString();
}

View File

@@ -179,6 +179,17 @@ class QueryEnhancerUnitTests {
assertThat(query).endsWithIgnoringCase("ORDER BY p.firstname, p.lastname asc");
}
@Test // GH-3263
void preserveSourceQueryWhenAddingSort() {
StringQuery query = new StringQuery("WITH all_projects AS (SELECT * FROM projects) SELECT * FROM all_projects p",
true);
assertThat(getEnhancer(query).applySorting(Sort.by("name"), "p")) //
.startsWithIgnoringCase(query.getQueryString())
.endsWithIgnoringCase("ORDER BY p.name ASC");
}
@Test // GH-2812
void createCountQueryFromDeleteQuery() {