Browse Source

Add backslash escape support to containsSqlScriptDelimiters

Prior to this commit, ScriptUtils supported MySQL-style escapes ('\\')
when splitting a script into statements; however, MySQL-style escapes
were not supported when determining if a given script contained a
specified statement delimiter. This caused executeSqlScript() to
erroneously fallback to a newline as the statement separator in such
cases.

This commit fixes this issue by implementing the same check for
MySQL-style escapes in containsSqlScriptDelimiters() that was already
present in splitSqlScript().

Issue: SPR-17120
pull/1928/head
Chris Harding 6 years ago committed by Sam Brannen
parent
commit
24ed6de6aa
  1. 14
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java
  2. 2
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java

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

@ -343,8 +343,20 @@ public abstract class ScriptUtils { @@ -343,8 +343,20 @@ public abstract class ScriptUtils {
*/
public static boolean containsSqlScriptDelimiters(String script, String delim) {
boolean inLiteral = false;
boolean inEscape = false;
for (int i = 0; i < script.length(); i++) {
if (script.charAt(i) == '\'') {
char c = script.charAt(i);
if (c == '\\') {
inEscape = !inEscape;
continue;
}
else if (inEscape) {
inEscape = false;
continue;
}
if (c == '\'') {
inLiteral = !inLiteral;
}
if (!inLiteral && script.startsWith(delim, i)) {

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

@ -170,6 +170,8 @@ public class ScriptUtilsUnitTests { @@ -170,6 +170,8 @@ public class ScriptUtilsUnitTests {
assertTrue(containsSqlScriptDelimiters("select 1\n select 2", "\n"));
assertFalse(containsSqlScriptDelimiters("select 1\n select 2", "\n\n"));
assertTrue(containsSqlScriptDelimiters("select 1\n\n select 2", "\n\n"));
assertTrue(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('Charles', 'd\\'Artagnan');", ";"));
assertFalse(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('a\\\\', 'b;')", ";"));
}
private String readScript(String path) throws Exception {

Loading…
Cancel
Save