Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix select distinct projection with Panache #29121

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
String trimmedQuery = query.trim();
// 7 is the length of "select "
String selectClause = trimmedQuery.substring(7, endSelect);
String selectClause = trimmedQuery.substring(7, endSelect).trim();
String from = trimmedQuery.substring(endSelect);
StringBuilder newQuery = new StringBuilder("select ");
// Handle select-distinct. HQL example: select distinct new org.acme.ProjectionClass...
String lowerCasedTrimmedSelect = selectClause.trim().toLowerCase();
boolean distinctQuery = lowerCasedTrimmedSelect.startsWith("distinct ");
boolean distinctQuery = selectClause.toLowerCase().startsWith("distinct ");
if (distinctQuery) {
// 9 is the length of "distinct "
selectClause = lowerCasedTrimmedSelect.substring(9).trim();
selectClause = selectClause.substring(9).trim();
newQuery.append("distinct ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
String trimmedQuery = query.trim();
// 7 is the length of "select "
String selectClause = trimmedQuery.substring(7, endSelect);
String selectClause = trimmedQuery.substring(7, endSelect).trim();
String from = trimmedQuery.substring(endSelect);
StringBuilder newQuery = new StringBuilder("select ");
// Handle select-distinct. HQL example: select distinct new org.acme.ProjectionClass...
String lowerCasedTrimmedSelect = selectClause.trim().toLowerCase();
boolean distinctQuery = lowerCasedTrimmedSelect.startsWith("distinct ");
boolean distinctQuery = selectClause.toLowerCase().startsWith("distinct ");
if (distinctQuery) {
// 9 is the length of "distinct "
selectClause = lowerCasedTrimmedSelect.substring(9).trim();
selectClause = selectClause.substring(9).trim();
newQuery.append("distinct ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,18 @@ public String testProjection() {
long countDistinct = projectionDistinctQuery.count();
Assertions.assertEquals(1L, countDistinct);

// We are checking that not everything gets lowercased
PanacheQuery<CatProjectionBean> letterCaseQuery = Cat
// The spaces at the beginning are intentional
.find(" SELECT disTINct 'GARFIELD', 'JoN ArBuCkLe' from Cat c where name = :NamE group by name ",
Parameters.with("NamE", bubulle.name))
.project(CatProjectionBean.class);

CatProjectionBean catView = letterCaseQuery.firstResult();
// Must keep the letter case
Assertions.assertEquals("GARFIELD", catView.getName());
Assertions.assertEquals("JoN ArBuCkLe", catView.getOwnerName());

Cat.deleteAll();
CatOwner.deleteAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,16 @@ public Uni<String> testProjection2() {
Assertions.assertTrue(
exception.getMessage().startsWith("Unable to perform a projection on a 'select new' query"));
})
.chain(() -> Cat
.find(" SELECT disTINct 'GARFIELD', 'JoN ArBuCkLe' from Cat c where name = :NamE group by name ",
Parameters.with("NamE", catName))
.project(CatProjectionBean.class)
.<CatProjectionBean> firstResult())
.invoke(catView -> {
// Must keep the letter case
Assertions.assertEquals("GARFIELD", catView.name);
Assertions.assertEquals("JoN ArBuCkLe", catView.ownerName);
})
.replaceWith("OK");
}

Expand Down