Skip to content

Commit

Permalink
Fixes * bug in createCountQueryFor.
Browse files Browse the repository at this point in the history
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 spring-projects#2177, spring-projects#2260, spring-projects#2511
  • Loading branch information
DiegoKrupitza committed Apr 28, 2022
1 parent a70fff4 commit 6f8170b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,19 @@ public static String createCountQueryFor(String originalQuery, @Nullable String
boolean useVariable = StringUtils.hasText(variable) //
&& !variable.startsWith(" new") //
&& !variable.startsWith("count(") //
&& !variable.contains(",") //
&& !variable.contains("*");
&& !variable.contains(",");

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

String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;

String alias = QueryUtils.detectAlias(originalQuery);
if("*".equals(variable) && alias != null) {
replacement = alias;
}

countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, replacement));
} else {
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, countProjection));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,26 @@ void correctApplySortOnComplexNestedFunctionQuery() {
assertThat(result).containsIgnoringCase("order by dd.institutesIds");
}


@Test //GH-2511
void countQueryUsesCorrectVariable() {
StringQuery nativeQuery = new StringQuery("SELECT * FROM User WHERE created_at > $1", true);
QueryEnhancer queryEnhancer = getEnhancer(nativeQuery);
String countQueryFor = queryEnhancer.createCountQueryFor();
assertThat(countQueryFor).isEqualTo("SELECT count(*) FROM User WHERE created_at > $1");

nativeQuery = new StringQuery("SELECT * FROM (select * from test) ",true);
queryEnhancer = getEnhancer(nativeQuery);
countQueryFor = queryEnhancer.createCountQueryFor();
assertThat(countQueryFor).isEqualTo("SELECT count(*) FROM (SELECT * FROM test)");

nativeQuery = new StringQuery("SELECT * FROM (select * from test) as test",true);
queryEnhancer = getEnhancer(nativeQuery);
countQueryFor = queryEnhancer.createCountQueryFor();
assertThat(countQueryFor).isEqualTo("SELECT count(test) FROM (SELECT * FROM test) AS test");
}


public static Stream<Arguments> detectsJoinAliasesCorrectlySource() {

return Stream.of( //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,21 @@ void applySortingAccountsForNativeWindowFunction() {
"select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc, age desc");
}

@Test //GH-2511
void countQueryUsesCorrectVariable() {
String countQueryFor = createCountQueryFor("SELECT * FROM User WHERE created_at > $1");
assertThat(countQueryFor).isEqualTo("select count(*) FROM User WHERE created_at > $1");

countQueryFor = createCountQueryFor("SELECT * FROM mytable WHERE nr = :number AND kon = :kon AND datum >= '2019-01-01'");
assertThat(countQueryFor).isEqualTo("select count(*) FROM mytable WHERE nr = :number AND kon = :kon AND datum >= '2019-01-01'");

countQueryFor = createCountQueryFor("SELECT * FROM context ORDER BY time");
assertThat(countQueryFor).isEqualTo("select count(*) FROM context");

countQueryFor = createCountQueryFor("select * FROM users_statuses WHERE (user_created_at BETWEEN $1 AND $2)");
assertThat(countQueryFor).isEqualTo("select count(*) FROM users_statuses WHERE (user_created_at BETWEEN $1 AND $2)");

countQueryFor = createCountQueryFor("SELECT * FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)");
assertThat(countQueryFor).isEqualTo("select count(us) FROM users_statuses us WHERE (user_created_at BETWEEN :fromDate AND :toDate)");
}
}

0 comments on commit 6f8170b

Please sign in to comment.