Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ public static List<String> splitSqlScript(String script, char delim) {
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
char[] content = script.toCharArray();
int openDelimiter = -1, lastCharacter = -1;
for (int i = 0; i < script.length(); i++) {
if (content[i] == '"') {
inLiteral = !inLiteral;
if (content[i] == '"' || content[i] == '\'') {
if (openDelimiter == -1) {
// We were not inside a literal; store the delimiter's value.
openDelimiter = content[i];
inLiteral = true;
} else if (openDelimiter == content[i] && lastCharacter != '\\') {
// We exit from the literal only on the same character
// AND unless we're being escaped by a bachslash.
inLiteral = false;
openDelimiter = -1;
}
}
if (content[i] == delim && !inLiteral) {
if (sb.length() > 0) {
Expand All @@ -32,6 +42,12 @@ public static List<String> splitSqlScript(String script, char delim) {
} else {
sb.append(content[i]);
}
if (lastCharacter == '\\' && content[i] == '\\') {
// It was an escaped backslash, don't accumulate.
lastCharacter = -1;
} else {
lastCharacter = content[i];
}
}
if (sb.length() > 0) {
statements.add(sb.toString().trim());
Expand Down