Skip to content

Commit

Permalink
Merge pull request quarkusio#10455 from loicmathieu/hibernate-panache…
Browse files Browse the repository at this point in the history
…-count-named-query

Implements count() for named queries
  • Loading branch information
gastaldi authored Jul 7, 2020
2 parents f229e17 + 513df59 commit b4f6b6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,14 @@ public void withHint(String hintName, Object value) {

@SuppressWarnings("unchecked")
public long count() {
if (AbstractJpaOperations.isNamedQuery(query)) {
throw new PanacheQueryException("Unable to perform a count operation on a named query");
}

if (count == null) {
Query countQuery = em.createQuery(countQuery());
String selectQuery = query;
if (AbstractJpaOperations.isNamedQuery(query)) {
org.hibernate.query.Query q = (org.hibernate.query.Query) em.createNamedQuery(query.substring(1));
selectQuery = q.getQueryString();
}

Query countQuery = em.createQuery(countQuery(selectQuery));
if (paramsArrayOrMap instanceof Map)
AbstractJpaOperations.bindParameters(countQuery, (Map<String, Object>) paramsArrayOrMap);
else
Expand All @@ -223,13 +225,13 @@ public long count() {
return count;
}

private String countQuery() {
private String countQuery(String selectQuery) {
if (countQuery != null) {
return countQuery;
}

// try to generate a good count query from the existing query
Matcher selectMatcher = SELECT_PATTERN.matcher(query);
Matcher selectMatcher = SELECT_PATTERN.matcher(selectQuery);
String countQuery;
if (selectMatcher.matches()) {
// this one cannot be null
Expand All @@ -239,17 +241,17 @@ private String countQuery() {
String secondSelection = selectMatcher.group(2);
// we can only count distinct single columns
if (secondSelection != null && !secondSelection.trim().isEmpty()) {
throw new PanacheQueryException("Count query not supported for select query: " + query);
throw new PanacheQueryException("Count query not supported for select query: " + selectQuery);
}
countQuery = "SELECT COUNT(" + firstSelection + ") " + selectMatcher.group(3);
} else {
// it's not distinct, forget the column list
countQuery = "SELECT COUNT(*) " + selectMatcher.group(3);
}
} else if (FROM_PATTERN.matcher(query).matches()) {
countQuery = "SELECT COUNT(*) " + query;
} else if (FROM_PATTERN.matcher(selectQuery).matches()) {
countQuery = "SELECT COUNT(*) " + selectQuery;
} else {
throw new PanacheQueryException("Count query not supported for select query: " + query);
throw new PanacheQueryException("Count query not supported for select query: " + selectQuery);
}

// remove the order by clause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public String testModel() {
persons = Person.find("#Person.getByName", Parameters.with("name", "stef")).list();
Assertions.assertEquals(1, persons.size());
Assertions.assertEquals(person, persons.get(0));
Assertions.assertEquals(1, Person.find("#Person.getByName", Parameters.with("name", "stef")).count());
Assertions.assertThrows(PanacheQueryException.class, () -> Person.find("#Person.namedQueryNotFound").list());
NamedQueryEntity.find("#NamedQueryMappedSuperClass.getAll").list();
NamedQueryEntity.find("#NamedQueryEntity.getAll").list();
Expand Down

0 comments on commit b4f6b6c

Please sign in to comment.