Skip to content

Commit 038ac99

Browse files
DiegoKrupitzagregturn
authored andcommitted
Fixes * bug in createCountQueryFor.
In commit 3e64d9a a bug got introduced that uses the next symbol after the table name for the count function. With this commit this should be now resolved. The count query will use `*` when there is no alias present nor a variable. Related tickets #2341, #2177, #2260, #2511
1 parent 7106f8f commit 038ac99

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,19 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
526526
boolean useVariable = StringUtils.hasText(variable) //
527527
&& !variable.startsWith(" new") //
528528
&& !variable.startsWith("count(") //
529-
&& !variable.contains(",") //
530-
&& !variable.contains("*");
529+
&& !variable.contains(",");
531530

532531
String complexCountValue = matcher.matches() && StringUtils.hasText(matcher.group(COMPLEX_COUNT_FIRST_INDEX))
533532
? COMPLEX_COUNT_VALUE
534533
: COMPLEX_COUNT_LAST_VALUE;
535534

536535
String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;
536+
537+
String alias = QueryUtils.detectAlias(originalQuery);
538+
if("*".equals(variable) && alias != null) {
539+
replacement = alias;
540+
}
541+
537542
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, replacement));
538543
} else {
539544
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, countProjection));

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,21 @@ void applySortingAccountsForNativeWindowFunction() {
640640
"select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc, age desc");
641641
}
642642

643+
@Test //GH-2511
644+
void countQueryUsesCorrectVariable() {
645+
String countQueryFor = createCountQueryFor("SELECT * FROM User WHERE created_at > $1");
646+
assertThat(countQueryFor).isEqualTo("select count(*) FROM User WHERE created_at > $1");
647+
648+
countQueryFor = createCountQueryFor("SELECT * FROM mytable WHERE nr = :number AND kon = :kon AND datum >= '2019-01-01'");
649+
assertThat(countQueryFor).isEqualTo("select count(*) FROM mytable WHERE nr = :number AND kon = :kon AND datum >= '2019-01-01'");
650+
651+
countQueryFor = createCountQueryFor("SELECT * FROM context ORDER BY time");
652+
assertThat(countQueryFor).isEqualTo("select count(*) FROM context");
653+
654+
countQueryFor = createCountQueryFor("select * FROM users_statuses WHERE (user_created_at BETWEEN $1 AND $2)");
655+
assertThat(countQueryFor).isEqualTo("select count(*) FROM users_statuses WHERE (user_created_at BETWEEN $1 AND $2)");
656+
657+
countQueryFor = createCountQueryFor("SELECT * FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)");
658+
assertThat(countQueryFor).isEqualTo("select count(us) FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)");
659+
}
643660
}

0 commit comments

Comments
 (0)