Fix count query creation when order by clause contains newlines.

Closes: #3329
Original Pull Request: #3330
This commit is contained in:
Eduard Dudar
2024-01-23 13:03:10 -08:00
committed by Christoph Strobl
parent 06b59d7bb6
commit 4cdc421c62
3 changed files with 28 additions and 7 deletions

View File

@@ -82,6 +82,7 @@ import org.springframework.util.StringUtils;
* @author Chris Fraser
* @author Donghun Shin
* @author Pranav HS
* @author Eduard Dudar
*/
public abstract class QueryUtils {
@@ -102,7 +103,7 @@ public abstract class QueryUtils {
private static final String SIMPLE_COUNT_VALUE = "$2";
private static final String COMPLEX_COUNT_VALUE = "$3 $6";
private static final String COMPLEX_COUNT_LAST_VALUE = "$6";
private static final String ORDER_BY_PART = "(?iu)\\s+order\\s+by\\s+.*";
private static final Pattern ORDER_BY_PART = Pattern.compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
private static final Pattern ALIAS_MATCH;
private static final Pattern COUNT_MATCH;
@@ -634,7 +635,7 @@ public abstract class QueryUtils {
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, countProjection));
}
return countQuery.replaceFirst(ORDER_BY_PART, "");
return ORDER_BY_PART.matcher(countQuery).replaceFirst("");
}
/**

View File

@@ -40,6 +40,7 @@ import org.springframework.data.jpa.domain.JpaSort;
* @author Grégoire Druant
* @author Mohammad Hewedy
* @author Greg Turnquist
* @author Eduard Dudar
*/
class DefaultQueryUtilsUnitTests {
@@ -452,7 +453,15 @@ class DefaultQueryUtilsUnitTests {
assertThat(createCountQueryFor("select * from User user\n" + //
" where user.age = 18\n" + //
" order by user.name\n ")).isEqualTo("select count(user) from User user\n" + //
" where user.age = 18\n ");
" where user.age = 18");
}
@Test // GH-3329
void createCountQuerySupportsNewLineCharacters() {
assertThat(createCountQueryFor("select * from User user\n" + //
" where user.age = 18\n" + //
" order by user.name,\n user.age DESC")).isEqualTo("select count(user) from User user\n" + //
" where user.age = 18");
}
@Test
@@ -463,7 +472,7 @@ class DefaultQueryUtilsUnitTests {
" from User user\n" + //
" where user.age = 18\n" + //
" order\nby\nuser.name\n ")).isEqualTo("select count(user) from User user\n" + //
" where user.age = 18\n ");
" where user.age = 18");
}
@Test // DATAJPA-1061

View File

@@ -50,6 +50,7 @@ import org.springframework.data.jpa.domain.JpaSort;
* @author Michał Pachucki
* @author Erik Pellizzon
* @author Pranav HS
* @author Eduard Dudar
*/
class QueryUtilsUnitTests {
@@ -584,8 +585,19 @@ class QueryUtilsUnitTests {
order by user.name
\s""")).isEqualTo("""
select count(user) from User user
where user.age = 18""");
}
@Test // GH-3329
void createCountQuerySupportsNewLineCharacters() {
assertThat(createCountQueryFor("""
select * from User user
where user.age = 18
\s""");
order by user.name,
user.age DESC
\s""")).isEqualTo("""
select count(user) from User user
where user.age = 18""");
}
@Test // GH-2341
@@ -606,8 +618,7 @@ class QueryUtilsUnitTests {
user.name
\s""")).isEqualTo("""
select count(user) from User user
where user.age = 18
\s""");
where user.age = 18""");
}
@Test // DATAJPA-1061