Skip to content

Commit

Permalink
Merge pull request #44188 from mbladel/panache-count
Browse files Browse the repository at this point in the history
Use SelectionQuery#getResultCount() in Panache entity count operations
  • Loading branch information
yrodiere authored Nov 4, 2024
2 parents f23d420 + d65314f commit cde819c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,39 +323,33 @@ public Stream<?> streamAll(Class<?> entityClass, Sort sort) {

public long count(Class<?> entityClass) {
return getSession(entityClass)
.createSelectionQuery("SELECT COUNT(*) FROM " + PanacheJpaUtil.getEntityName(entityClass), Long.class)
.getSingleResult();
.createSelectionQuery("FROM " + PanacheJpaUtil.getEntityName(entityClass), entityClass)
.getResultCount();
}

public long count(Class<?> entityClass, String panacheQuery, Object... params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
SelectionQuery namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
SelectionQuery<?> namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
return (long) bindParameters(namedQuery, params).getSingleResult();
}

try {
return bindParameters(
getSession(entityClass)
.createSelectionQuery(
PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)), Long.class),
params).getSingleResult();
String query = PanacheJpaUtil.createQueryForCount(entityClass, panacheQuery, paramCount(params));
return bindParameters(getSession(entityClass).createSelectionQuery(query, Object.class), params).getResultCount();
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}

public long count(Class<?> entityClass, String panacheQuery, Map<String, Object> params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
SelectionQuery namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
SelectionQuery<?> namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
return (long) bindParameters(namedQuery, params).getSingleResult();
}

try {
return bindParameters(
getSession(entityClass)
.createSelectionQuery(
PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)), Long.class),
params).getSingleResult();
String query = PanacheJpaUtil.createQueryForCount(entityClass, panacheQuery, paramCount(params));
return bindParameters(getSession(entityClass).createSelectionQuery(query, Object.class), params).getResultCount();
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public Uni<List<?>> listAll(Class<?> entityClass, Sort sort) {
public Uni<Long> count(Class<?> entityClass) {
return getSession()
.chain(session -> session
.createSelectionQuery("SELECT COUNT(*) FROM " + PanacheJpaUtil.getEntityName(entityClass), Long.class)
.getSingleResult());
.createSelectionQuery("FROM " + PanacheJpaUtil.getEntityName(entityClass), entityClass)
.getResultCount());
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -213,9 +213,9 @@ public Uni<Long> count(Class<?> entityClass, String panacheQuery, Object... para
});

return getSession().chain(session -> bindParameters(
session.createSelectionQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)),
Long.class),
params).getSingleResult())
session.createSelectionQuery(PanacheJpaUtil.createQueryForCount(entityClass, panacheQuery, paramCount(params)),
Object.class),
params).getResultCount())
.onFailure(RuntimeException.class)
.transform(x -> NamedQueryUtil.checkForNamedQueryMistake((RuntimeException) x, panacheQuery));
}
Expand All @@ -230,9 +230,9 @@ public Uni<Long> count(Class<?> entityClass, String panacheQuery, Map<String, Ob
});

return getSession().chain(session -> bindParameters(
session.createSelectionQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)),
Long.class),
params).getSingleResult())
session.createSelectionQuery(PanacheJpaUtil.createQueryForCount(entityClass, panacheQuery, paramCount(params)),
Object.class),
params).getResultCount())
.onFailure(RuntimeException.class)
.transform(x -> NamedQueryUtil.checkForNamedQueryMistake((RuntimeException) x, panacheQuery));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,31 @@ public static boolean isNamedQuery(String query) {
return query.charAt(0) == '#';
}

public static String createCountQuery(Class<?> entityClass, String query, int paramCount) {
if (query == null)
return "SELECT COUNT(*) FROM " + getEntityName(entityClass);
public static String createQueryForCount(Class<?> entityClass, String query, int paramCount) {
if (query == null || query.isEmpty())
return "FROM " + getEntityName(entityClass);

String trimmedForAnalysis = trimForAnalysis(query);
if (trimmedForAnalysis.isEmpty())
return "SELECT COUNT(*) FROM " + getEntityName(entityClass);
return "FROM " + getEntityName(entityClass);

// assume these have valid select clauses and let them through
if (trimmedForAnalysis.startsWith("select ")
|| trimmedForAnalysis.startsWith("with ")) {
|| trimmedForAnalysis.startsWith("with ")
|| trimmedForAnalysis.startsWith("from ")) {
return query;
}
if (trimmedForAnalysis.startsWith("from ")) {
return "SELECT COUNT(*) " + query;
}
if (trimmedForAnalysis.startsWith("where ")) {
return "SELECT COUNT(*) FROM " + getEntityName(entityClass) + " " + query;
return "FROM " + getEntityName(entityClass) + " " + query;
}
if (trimmedForAnalysis.startsWith("order by ")) {
// ignore it
return "SELECT COUNT(*) FROM " + getEntityName(entityClass);
return "FROM " + getEntityName(entityClass);
}
if (trimmedForAnalysis.indexOf(' ') == -1 && trimmedForAnalysis.indexOf('=') == -1 && paramCount == 1) {
query += " = ?1";
}
return "SELECT COUNT(*) FROM " + getEntityName(entityClass) + " WHERE " + query;
return "FROM " + getEntityName(entityClass) + " WHERE " + query;
}

public static String createUpdateQuery(Class<?> entityClass, String query, int paramCount) {
Expand Down

0 comments on commit cde819c

Please sign in to comment.