DATAJPA-420 - Fixed count projection for manual queries with projections.
Original pull request: #52.
This commit is contained in:
committed by
Oliver Gierke
parent
ae10332b18
commit
a0a98dfc57
@@ -64,6 +64,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Oliver Gierke
|
||||
* @author Kevin Raymond
|
||||
* @author Thomas Darimont
|
||||
* @author Komi Innocent
|
||||
*/
|
||||
public abstract class QueryUtils {
|
||||
|
||||
@@ -341,7 +342,7 @@ public abstract class QueryUtils {
|
||||
Matcher matcher = COUNT_MATCH.matcher(originalQuery);
|
||||
String variable = matcher.matches() ? matcher.group(4) : null;
|
||||
boolean useVariable = StringUtils.hasText(variable) && !variable.startsWith("new")
|
||||
&& !variable.startsWith("count(");
|
||||
&& !variable.startsWith("count(") && !variable.contains(",");
|
||||
|
||||
String countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, useVariable ? SIMPLE_COUNT_VALUE
|
||||
: COMPLEX_COUNT_VALUE));
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.data.domain.Sort;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Komi Innocent
|
||||
*/
|
||||
public class QueryUtilsUnitTests {
|
||||
|
||||
@@ -263,6 +264,14 @@ public class QueryUtilsUnitTests {
|
||||
assertCountQuery("select a.b from A a", "select count(a.b) from A a");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-420
|
||||
*/
|
||||
@Test
|
||||
public void createsCountQueryForScalarSelects() {
|
||||
assertCountQuery("select p.lastname,p.firstname from Person p", "select count(p) from Person p");
|
||||
}
|
||||
|
||||
private void assertCountQuery(String originalQuery, String countQuery) {
|
||||
assertThat(createCountQueryFor(originalQuery), is(countQuery));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user