Fix NullPointer when deriving a count query for non-SELECT statements.

Closes #2812
This commit is contained in:
Mark Paluch
2023-02-23 15:56:02 +01:00
committed by Greg L. Turnquist
parent 7a9e55f8cf
commit ff9e081fc0
4 changed files with 20 additions and 2 deletions

View File

@@ -105,7 +105,7 @@ interface DeclaredQuery {
/**
* Return whether the query is a native query of not.
*
*
* @return <code>true</code> if native query otherwise <code>false</code>
*/
default boolean isNativeQuery() {

View File

@@ -617,7 +617,7 @@ public abstract class QueryUtils {
String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {
if (variable != null && (nativeQuery && (variable.contains(",") || "*".equals(variable)))) {
replacement = "1";
} else {

View File

@@ -70,6 +70,15 @@ class DefaultQueryUtilsUnitTests {
"select count(distinct u) from User u where u.foo = ?");
}
@Test // GH-2812
void createsCountQueryForDeleteQuery() {
String result = createCountQueryFor("delete from some_table where id in :ids", null, true);
// ح(•̀ж•́)ง †
assertThat(result).isEqualTo("deleteselect count(where) from some_table where id in :ids");
}
@Test
void createsCountQueryForConstructorQueries() {

View File

@@ -176,6 +176,15 @@ class QueryEnhancerUnitTests {
endsIgnoringCase(query, "ORDER BY p.firstname, p.lastname asc");
}
@Test // GH-2812
void createCountQueryFromDeleteQuery() {
StringQuery query = new StringQuery("delete from some_table where id in :ids", true);
assertThat(getEnhancer(query).createCountQueryFor("p.lastname"))
.isEqualToIgnoringCase("delete from some_table where id in :ids");
}
@Test // DATAJPA-456
void createCountQueryFromTheGivenCountProjection() {