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 #2341, #2177, #2260, #2511
  • Loading branch information
DiegoKrupitza authored and gregturn committed May 4, 2022
1 parent 32bc2a5 commit a69aa22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,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 @@ -640,4 +640,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 a69aa22

Please sign in to comment.