Skip to content

Commit 91cd84e

Browse files
c-frasergregturn
authored andcommitted
Handle multiline subquery removal.
Closes #2582. Related: #2563, #2557, #2603
1 parent 4b26700 commit 91cd84e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Darin Manica
8080
* @author Simon Paradies
8181
* @author Vladislav Yukharin
82+
* @author Chris Fraser
8283
*/
8384
public abstract class QueryUtils {
8485

@@ -104,7 +105,7 @@ public abstract class QueryUtils {
104105
private static final Pattern ALIAS_MATCH;
105106
private static final Pattern COUNT_MATCH;
106107
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
107-
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);
108109
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
109110
Pattern.CASE_INSENSITIVE);
110111

Diff for: spring-data-jpa/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

@@ -178,6 +179,56 @@ void testRemoveSubqueries() throws Exception {
178179
.isEqualTo("(select u from User u where not exists ( ))");
179180
}
180181

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

0 commit comments

Comments
 (0)