Skip to content

Commit 045c671

Browse files
committed
Handle newlines when searching for subqueries
1 parent 782bcf3 commit 045c671

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
* @author Jędrzej Biedrzycki
7979
* @author Darin Manica
8080
* @author Simon Paradies
81+
* @author Michał Pachucki
8182
*/
8283
public abstract class QueryUtils {
8384

@@ -103,7 +104,7 @@ public abstract class QueryUtils {
103104
private static final Pattern ALIAS_MATCH;
104105
private static final Pattern COUNT_MATCH;
105106
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
106-
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE);
107+
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE | DOTALL);
107108
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
108109
Pattern.CASE_INSENSITIVE);
109110

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

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @author Greg Turnquist
4646
* @author Jędrzej Biedrzycki
4747
* @author Darin Manica
48+
* @author Michał Pachucki
4849
*/
4950
class QueryUtilsUnitTests {
5051

@@ -710,6 +711,15 @@ void applySortingAccountsForNativeWindowFunction() {
710711
"select * from (select * from user order by 1, 2, 3 desc limit 10) u order by u.active asc, age desc");
711712
}
712713

714+
@Test
715+
void applySortingAccountsForNewlinesInSubselect() {
716+
717+
Sort sort = Sort.by(Order.desc("age"));
718+
719+
assertThat(QueryUtils.applySorting("select u\nfrom user u\nwhere exists (select u2\nfrom user u2\n)\n", sort))
720+
.isEqualTo("select u\nfrom user u\nwhere exists (select u2\nfrom user u2\n)\n order by u.age desc");
721+
}
722+
713723
@Test // GH-2511
714724
void countQueryUsesCorrectVariable() {
715725

0 commit comments

Comments
 (0)