Skip to content

Commit

Permalink
Merge pull request #29121 from DavideD/29089-lowercase
Browse files Browse the repository at this point in the history
Fix select distinct projection with Panache
  • Loading branch information
geoand authored Nov 11, 2022
2 parents 8c1b1f6 + 9becd8a commit b0a1dea
Show file tree
Hide file tree
Showing 4 changed files with 28 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
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

0 comments on commit b0a1dea

Please sign in to comment.