Skip to content

Commit 1ae67c8

Browse files
c-frasergregturn
authored andcommitted
Handle multiline subquery removal.
Closes #2582. Related: #2563, #2557, #2603
1 parent 02f155c commit 1ae67c8

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Diff for: src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
* @author Jędrzej Biedrzycki
7979
* @author Darin Manica
8080
* @author Simon Paradies
81+
* @author Vladislav Yukharin
82+
* @author Chris Fraser
8183
*/
8284
public abstract class QueryUtils {
8385

@@ -103,7 +105,7 @@ public abstract class QueryUtils {
103105
private static final Pattern ALIAS_MATCH;
104106
private static final Pattern COUNT_MATCH;
105107
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
106-
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE);
108+
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE | DOTALL | MULTILINE);
107109
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
108110
Pattern.CASE_INSENSITIVE);
109111

Diff for: src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

+51
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 Chris Fraser
4849
*/
4950
class QueryUtilsUnitTests {
5051

@@ -180,6 +181,56 @@ void testRemoveSubqueries() throws Exception {
180181
.isEqualTo("(select u from User u where not exists ( ))");
181182
}
182183

184+
@Test // GH-2581
185+
void testRemoveMultilineSubqueries() {
186+
187+
assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n"
188+
+ " where not exists (\n"
189+
+ " from User u2\n"
190+
+ " )")))
191+
.isEqualTo("select u from User u where not exists");
192+
assertThat(normalizeWhitespace(removeSubqueries("(\n"
193+
+ " select u from User u \n"
194+
+ " where not exists (\n"
195+
+ " from User u2\n"
196+
+ " )\n"
197+
+ ")")))
198+
.isEqualTo("( select u from User u where not exists )");
199+
assertThat(normalizeWhitespace(
200+
removeSubqueries("select u from User u \n"
201+
+ " where not exists (\n"
202+
+ " from User u2 \n"
203+
+ " where not exists (\n"
204+
+ " from User u3\n"
205+
+ " )\n"
206+
+ " )")))
207+
.isEqualTo("select u from User u where not exists");
208+
assertThat(normalizeWhitespace(
209+
removeSubqueries("select u from User u \n"
210+
+ " where not exists (\n"
211+
+ " (\n"
212+
+ " from User u2 \n"
213+
+ " where not exists (\n"
214+
+ " from User u3\n"
215+
+ " )\n"
216+
+ " )\n"
217+
+ " )")))
218+
.isEqualTo("select u from User u where not exists ( )");
219+
assertThat(normalizeWhitespace(
220+
removeSubqueries("(\n"
221+
+ " select u from User u \n"
222+
+ " where not exists (\n"
223+
+ " (\n"
224+
+ " from User u2 \n"
225+
+ " where not exists (\n"
226+
+ " from User u3\n"
227+
+ " )\n"
228+
+ " )\n"
229+
+ " )\n"
230+
+ ")")))
231+
.isEqualTo("( select u from User u where not exists ( ) )");
232+
}
233+
183234
private String normalizeWhitespace(String s) {
184235
Matcher matcher = MULTI_WHITESPACE.matcher(s);
185236
if (matcher.find()) {

0 commit comments

Comments
 (0)