Skip to content

Commit

Permalink
[quarkusio#29089] Fix error with projections in Panache
Browse files Browse the repository at this point in the history
When trying to run a select distinct query with projection, Panache will
lowercase the field names.
  • Loading branch information
DavideD authored and pedroh-pereira committed Nov 14, 2022
1 parent 66e4928 commit db293f1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
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

0 comments on commit db293f1

Please sign in to comment.