Fix NullPointer when deriving a count query for non-SELECT statements.
Closes #2812
This commit is contained in:
committed by
Greg L. Turnquist
parent
4dd936978e
commit
37ca75519a
@@ -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() {
|
||||
|
||||
@@ -604,7 +604,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 {
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
|
||||
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -236,6 +234,15 @@ class QueryEnhancerUnitTests {
|
||||
assertCountQuery("select p.lastname,p.firstname from Person p", "select count(1) from Person p", true);
|
||||
}
|
||||
|
||||
@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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user