Handle multiline subquery removal.

Closes #2582.
Related: #2563, #2557, #2603
This commit is contained in:
Chris Fraser
2022-06-28 13:59:27 -04:00
committed by Greg L. Turnquist
parent 4b267006aa
commit 91cd84e706
2 changed files with 53 additions and 1 deletions

View File

@@ -79,6 +79,7 @@ import org.springframework.util.StringUtils;
* @author Darin Manica
* @author Simon Paradies
* @author Vladislav Yukharin
* @author Chris Fraser
*/
public abstract class QueryUtils {
@@ -104,7 +105,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);

View File

@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
* @author Greg Turnquist
* @author Jędrzej Biedrzycki
* @author Darin Manica
* @author Chris Fraser
*/
class QueryUtilsUnitTests {
@@ -178,6 +179,56 @@ class QueryUtilsUnitTests {
.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()) {