Skip to content

Commit

Permalink
Fix NullPointer when deriving a count query for non-SELECT statements.
Browse files Browse the repository at this point in the history
Closes #2812
  • Loading branch information
mp911de authored and gregturn committed Feb 23, 2023
1 parent 4dd9369 commit 37ca755
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ default boolean usesPaging() {

/**
* Return whether the query is a native query of not.
*
*
* @return <code>true</code> if native query otherwise <code>false</code>
*/
default boolean isNativeQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ static String createCountQueryFor(String originalQuery, @Nullable String countPr

String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;

if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {
if (variable != null && (nativeQuery && (variable.contains(",") || "*".equals(variable)))) {
replacement = "1";
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ void createsCountQueryForDistinctQueries() {
"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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -236,6 +234,15 @@ void createsCountQueryForNativeScalarSelects() {
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() {

Expand Down

0 comments on commit 37ca755

Please sign in to comment.