Browse Source

Polish contribution

Issue: SPR-17120
pull/1928/head
Sam Brannen 6 years ago
parent
commit
bdac39150f
  1. 13
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java
  2. 8
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ScriptUtilsUnitTests.java

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

@ -344,18 +344,18 @@ public abstract class ScriptUtils { @@ -344,18 +344,18 @@ 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++) {
for (int i = 0; i < script.length(); i++) {
char c = script.charAt(i);
if (c == '\\') {
inEscape = !inEscape;
if (inEscape) {
inEscape = false;
continue;
}
else if (inEscape) {
inEscape = false;
// MySQL style escapes
if (c == '\\') {
inEscape = true;
continue;
}
if (c == '\'') {
inLiteral = !inLiteral;
}
@ -363,6 +363,7 @@ public abstract class ScriptUtils { @@ -363,6 +363,7 @@ public abstract class ScriptUtils {
return true;
}
}
return false;
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -166,12 +166,16 @@ public class ScriptUtilsUnitTests { @@ -166,12 +166,16 @@ public class ScriptUtilsUnitTests {
public void containsDelimiters() {
assertFalse(containsSqlScriptDelimiters("select 1\n select ';'", ";"));
assertTrue(containsSqlScriptDelimiters("select 1; select 2", ";"));
assertFalse(containsSqlScriptDelimiters("select 1; select '\\n\n';", "\n"));
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');", ";"));
// MySQL style escapes '\\'
assertFalse(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('a\\\\', 'b;')", ";"));
assertTrue(containsSqlScriptDelimiters("insert into users(first_name, last_name)\nvalues('Charles', 'd\\'Artagnan'); select 1;", ";"));
}
private String readScript(String path) throws Exception {

Loading…
Cancel
Save