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

Implements count() for named queries #10455

Merged
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 @@ -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