Fixes * bug in createCountQueryFor.
In commit 3e64d9ad9b 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
This commit is contained in:
committed by
Greg L. Turnquist
parent
3f40705eeb
commit
e4e153d273
@@ -525,14 +525,19 @@ public abstract class QueryUtils {
|
||||
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));
|
||||
|
||||
@@ -712,6 +712,26 @@ class QueryEnhancerUnitTests {
|
||||
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( //
|
||||
|
||||
@@ -638,4 +638,21 @@ class QueryUtilsUnitTests {
|
||||
"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)");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user