diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index e9ad5db78a..b837c95069 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -78,6 +78,7 @@ * @author Jędrzej Biedrzycki * @author Darin Manica * @author Simon Paradies + * @author Chris Fraser */ public abstract class QueryUtils { @@ -103,7 +104,7 @@ public abstract class QueryUtils { private static final Pattern ALIAS_MATCH; private static final Pattern COUNT_MATCH; private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\("); - private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE); + private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))", CASE_INSENSITIVE | DOTALL | MULTILINE); private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from", Pattern.CASE_INSENSITIVE); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 5b65b647f0..4f4118e4f8 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -45,6 +45,7 @@ * @author Greg Turnquist * @author Jędrzej Biedrzycki * @author Darin Manica + * @author Chris Fraser */ class QueryUtilsUnitTests { @@ -178,6 +179,56 @@ void testRemoveSubqueries() throws Exception { .isEqualTo("(select u from User u where not exists ( ))"); } + @Test // GH-2581 + void testRemoveMultilineSubqueries() { + + assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" + + " where not exists (\n" + + " from User u2\n" + + " )"))) + .isEqualTo("select u from User u where not exists"); + assertThat(normalizeWhitespace(removeSubqueries("(\n" + + " select u from User u \n" + + " where not exists (\n" + + " from User u2\n" + + " )\n" + + ")"))) + .isEqualTo("( select u from User u where not exists )"); + assertThat(normalizeWhitespace( + removeSubqueries("select u from User u \n" + + " where not exists (\n" + + " from User u2 \n" + + " where not exists (\n" + + " from User u3\n" + + " )\n" + + " )"))) + .isEqualTo("select u from User u where not exists"); + assertThat(normalizeWhitespace( + removeSubqueries("select u from User u \n" + + " where not exists (\n" + + " (\n" + + " from User u2 \n" + + " where not exists (\n" + + " from User u3\n" + + " )\n" + + " )\n" + + " )"))) + .isEqualTo("select u from User u where not exists ( )"); + assertThat(normalizeWhitespace( + removeSubqueries("(\n" + + " select u from User u \n" + + " where not exists (\n" + + " (\n" + + " from User u2 \n" + + " where not exists (\n" + + " from User u3\n" + + " )\n" + + " )\n" + + " )\n" + + ")"))) + .isEqualTo("( select u from User u where not exists ( ) )"); + } + private String normalizeWhitespace(String s) { Matcher matcher = MULTI_WHITESPACE.matcher(s); if (matcher.find()) {