DATAJPA-420 - Fixed count projection for manual queries with projections.

Original pull request: #52.
This commit is contained in:
Komi Serge Innocent
2013-11-14 15:37:33 +00:00
committed by Oliver Gierke
parent ae10332b18
commit a0a98dfc57
2 changed files with 11 additions and 1 deletions

View File

@@ -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));

View File

@@ -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));
}