Skip to content

Commit

Permalink
[#26284] Support select-distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Jul 9, 2022
1 parent fde2bde commit 974235f
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
if (query.trim().toLowerCase().startsWith("select ")) {
String trimmedQuery = query.trim();
int endSelect = trimmedQuery.toLowerCase().indexOf(" from ");
String selectClause = trimmedQuery.substring("SELECT ".length(), endSelect);
String from = trimmedQuery.substring(endSelect);
StringBuilder newQuery = new StringBuilder("SELECT new ")
StringBuilder newQuery = new StringBuilder("select ");
// Handle select-distinct. HQL example: select distinct new org.acme.ProjectionClass...
boolean distinctQuery = selectClause.trim().toLowerCase().startsWith("distinct ");
if (distinctQuery) {
selectClause = selectClause.trim().substring("distinct ".length()).trim();
newQuery.append("distinct ");
}
newQuery
.append("new ")
.append(type.getName())
.append(" (")
.append(trimmedQuery.substring("SELECT ".length(), endSelect))
.append("(")
.append(selectClause)
.append(")")
.append(from);

Expand Down

0 comments on commit 974235f

Please sign in to comment.