Skip to content

Commit bdac391

Browse files
committed
Polish contribution
Issue: SPR-17120
1 parent 24ed6de commit bdac391

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,26 @@ private static void appendSeparatorToScriptIfNecessary(StringBuilder scriptBuild
344344
public static boolean containsSqlScriptDelimiters(String script, String delim) {
345345
boolean inLiteral = false;
346346
boolean inEscape = false;
347-
for (int i = 0; i < script.length(); i++) {
348347

348+
for (int i = 0; i < script.length(); i++) {
349349
char c = script.charAt(i);
350-
if (c == '\\') {
351-
inEscape = !inEscape;
350+
if (inEscape) {
351+
inEscape = false;
352352
continue;
353353
}
354-
else if (inEscape) {
355-
inEscape = false;
354+
// MySQL style escapes
355+
if (c == '\\') {
356+
inEscape = true;
356357
continue;
357358
}
358-
359359
if (c == '\'') {
360360
inLiteral = !inLiteral;
361361
}
362362
if (!inLiteral && script.startsWith(delim, i)) {
363363
return true;
364364
}
365365
}
366+
366367
return false;
367368
}
368369

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -166,12 +166,16 @@ public void readAndSplitScriptContainingMuliLineComments() throws Exception {
166166
public void containsDelimiters() {
167167
assertFalse(containsSqlScriptDelimiters("select 1\n select ';'", ";"));
168168
assertTrue(containsSqlScriptDelimiters("select 1; select 2", ";"));
169+
169170
assertFalse(containsSqlScriptDelimiters("select 1; select '\\n\n';", "\n"));
170171
assertTrue(containsSqlScriptDelimiters("select 1\n select 2", "\n"));
172+
171173
assertFalse(containsSqlScriptDelimiters("select 1\n select 2", "\n\n"));
172174
assertTrue(containsSqlScriptDelimiters("select 1\n\n select 2", "\n\n"));
173-
assertTrue(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('Charles', 'd\\'Artagnan');", ";"));
175+
176+
// MySQL style escapes '\\'
174177
assertFalse(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('a\\\\', 'b;')", ";"));
178+
assertTrue(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('Charles', 'd\\'Artagnan'); select 1;", ";"));
175179
}
176180

177181
private String readScript(String path) throws Exception {

0 commit comments

Comments
 (0)