Support single quotes nested in double quotes in SQL scripts

Some databases such as Oracle permit double quoted column aliases that
contain case-sensitive characters, single quotes, and other special
characters; however, prior to this commit, SqlScripts interpreted a
single quote nested within double quotes as the start of a string
literal resulting in improper parsing.

This commit addresses this issue by ensuring that double quoted strings
such as column aliases are properly parsed even when containing single
quotes.

Issue: SPR-13218
This commit is contained in:
ndebeiss
2015-07-10 14:45:18 +02:00
committed by Sam Brannen
parent de6bbe7797
commit 629bcb6599
4 changed files with 51 additions and 9 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
* @author Chris Beams
* @author Oliver Gierke
* @author Chris Baldwin
* @author Nicolas Debeissat
* @since 4.0.3
*/
public abstract class ScriptUtils {
@@ -173,7 +174,8 @@ public abstract class ScriptUtils {
Assert.hasText(blockCommentEndDelimiter, "blockCommentEndDelimiter must not be null or empty");
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
boolean inSingleQuote = false;
boolean inDoubleQuote = false;
boolean inEscape = false;
char[] content = script.toCharArray();
for (int i = 0; i < script.length(); i++) {
@@ -189,10 +191,13 @@ public abstract class ScriptUtils {
sb.append(c);
continue;
}
if (c == '\'') {
inLiteral = !inLiteral;
if (!inDoubleQuote && (c == '\'')) {
inSingleQuote = !inSingleQuote;
}
if (!inLiteral) {
else if (!inSingleQuote && (c == '"')) {
inDoubleQuote = !inDoubleQuote;
}
if (!inSingleQuote && !inDoubleQuote) {
if (script.startsWith(separator, i)) {
// we've reached the end of the current statement
if (sb.length() > 0) {