Skip to content

Commit

Permalink
Do not split SQL on delimiters within BEGIN/END blocks
Browse files Browse the repository at this point in the history
LB-958
#1553
  • Loading branch information
nvoxland committed Nov 20, 2020
1 parent 87e89b7 commit a3575f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions liquibase-core/src/main/java/liquibase/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static String trimToNull(String string) {
return returnString;
}
}

/**
* Removes any comments from multiple line SQL using {@link #stripComments(String)}
* and then extracts each individual statement using {@link #splitSQL(String, String)}.
Expand All @@ -68,8 +68,16 @@ public static String[] processMutliLineSQL(String multiLineSQL, boolean stripCom
String previousPiece = null;
boolean previousDelimiter = false;
List<Object> parsedArray = Arrays.asList(parsed.toArray(true));
boolean isInClause = false;
for (Object piece : mergeTokens(parsedArray, endDelimiter)) {
if (splitStatements && (piece instanceof String) && isDelimiter((String) piece, previousPiece, endDelimiter)) {
if (piece instanceof String && ((String) piece).equalsIgnoreCase("BEGIN")) {
isInClause = true;
}
if (piece instanceof String && ((String) piece).equalsIgnoreCase("END")) {
isInClause = false;
}

if (!isInClause && splitStatements && (piece instanceof String) && isDelimiter((String) piece, previousPiece, endDelimiter)) {
String trimmedString = StringUtil.trimToNull(currentString.toString());
if (trimmedString != null) {
returnArray.add(trimmedString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class StringUtilTest extends Specification {
true | true | null | "statement 1;\nstatement 2;\nGO\n\nstatement 3; statement 4;" | ["statement 1", "statement 2", "statement 3", "statement 4"]
true | true | "\\nGO" | "statement 1 \nGO\nstatement 2" | ["statement 1", "statement 2"]
true | true | "\\nGO" | "CREATE OR REPLACE PACKAGE emp_actions AS -- spec\nTYPE EmpRecTyp IS RECORD (emp_id INT, salary REAL);\nCURSOR desc_salary RETURN EmpRecTyp);\nEND emp_actions;\nGO\nanother statement;here\nGO\n" | ["CREATE OR REPLACE PACKAGE emp_actions AS \nTYPE EmpRecTyp IS RECORD (emp_id INT, salary REAL);\nCURSOR desc_salary RETURN EmpRecTyp);\nEND emp_actions;", "another statement;here"]
true | true | null | "CREATE OR REPLACE PACKAGE emp_actions AS BEGIN\n statement 1;\nanother statement;here; END;" | ["CREATE OR REPLACE PACKAGE emp_actions AS BEGIN\n statement 1;\nanother statement;here; END"]

}

Expand Down

0 comments on commit a3575f8

Please sign in to comment.