Skip to content

Commit 4cdc421

Browse files
edudarchristophstrobl
authored andcommitted
Fix count query creation when order by clause contains newlines.
Closes: #3329 Original Pull Request: #3330
1 parent 06b59d7 commit 4cdc421

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
* @author Chris Fraser
8383
* @author Donghun Shin
8484
* @author Pranav HS
85+
* @author Eduard Dudar
8586
*/
8687
public abstract class QueryUtils {
8788

@@ -102,7 +103,7 @@ public abstract class QueryUtils {
102103
private static final String SIMPLE_COUNT_VALUE = "$2";
103104
private static final String COMPLEX_COUNT_VALUE = "$3 $6";
104105
private static final String COMPLEX_COUNT_LAST_VALUE = "$6";
105-
private static final String ORDER_BY_PART = "(?iu)\\s+order\\s+by\\s+.*";
106+
private static final Pattern ORDER_BY_PART = Pattern.compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
106107

107108
private static final Pattern ALIAS_MATCH;
108109
private static final Pattern COUNT_MATCH;
@@ -634,7 +635,7 @@ static String createCountQueryFor(String originalQuery, @Nullable String countPr
634635
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, countProjection));
635636
}
636637

637-
return countQuery.replaceFirst(ORDER_BY_PART, "");
638+
return ORDER_BY_PART.matcher(countQuery).replaceFirst("");
638639
}
639640

640641
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Grégoire Druant
4141
* @author Mohammad Hewedy
4242
* @author Greg Turnquist
43+
* @author Eduard Dudar
4344
*/
4445
class DefaultQueryUtilsUnitTests {
4546

@@ -452,7 +453,15 @@ void createCountQuerySupportsWhitespaceCharacters() {
452453
assertThat(createCountQueryFor("select * from User user\n" + //
453454
" where user.age = 18\n" + //
454455
" order by user.name\n ")).isEqualTo("select count(user) from User user\n" + //
455-
" where user.age = 18\n ");
456+
" where user.age = 18");
457+
}
458+
459+
@Test // GH-3329
460+
void createCountQuerySupportsNewLineCharacters() {
461+
assertThat(createCountQueryFor("select * from User user\n" + //
462+
" where user.age = 18\n" + //
463+
" order by user.name,\n user.age DESC")).isEqualTo("select count(user) from User user\n" + //
464+
" where user.age = 18");
456465
}
457466

458467
@Test
@@ -463,7 +472,7 @@ void createCountQuerySupportsLineBreaksInSelectClause() {
463472
" from User user\n" + //
464473
" where user.age = 18\n" + //
465474
" order\nby\nuser.name\n ")).isEqualTo("select count(user) from User user\n" + //
466-
" where user.age = 18\n ");
475+
" where user.age = 18");
467476
}
468477

469478
@Test // DATAJPA-1061

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Michał Pachucki
5151
* @author Erik Pellizzon
5252
* @author Pranav HS
53+
* @author Eduard Dudar
5354
*/
5455
class QueryUtilsUnitTests {
5556

@@ -584,8 +585,19 @@ void createCountQuerySupportsWhitespaceCharacters() {
584585
order by user.name
585586
\s""")).isEqualTo("""
586587
select count(user) from User user
588+
where user.age = 18""");
589+
}
590+
591+
@Test // GH-3329
592+
void createCountQuerySupportsNewLineCharacters() {
593+
assertThat(createCountQueryFor("""
594+
select * from User user
587595
where user.age = 18
588-
\s""");
596+
order by user.name,
597+
user.age DESC
598+
\s""")).isEqualTo("""
599+
select count(user) from User user
600+
where user.age = 18""");
589601
}
590602

591603
@Test // GH-2341
@@ -606,8 +618,7 @@ void createCountQuerySupportsLineBreaksInSelectClause() {
606618
user.name
607619
\s""")).isEqualTo("""
608620
select count(user) from User user
609-
where user.age = 18
610-
\s""");
621+
where user.age = 18""");
611622
}
612623

613624
@Test // DATAJPA-1061

0 commit comments

Comments
 (0)