@ -418,12 +418,44 @@ public abstract class ScriptUtils {
* < p > This method is intended to be used to find the string delimiting each
* < p > This method is intended to be used to find the string delimiting each
* SQL statement & mdash ; for example , a ';' character .
* SQL statement & mdash ; for example , a ';' character .
* < p > Any occurrence of the delimiter within the script will be ignored if it
* < p > Any occurrence of the delimiter within the script will be ignored if it
* is enclosed within single quotes ( { @code ' } ) or double quotes ( { @code " } )
* is within a < em > literal < / em > block of text enclosed in single quotes
* or if it is escaped with a backslash ( { @code \ } ) .
* ( { @code ' } ) or double quotes ( { @code " } ) , if it is escaped with a backslash
* ( { @code \ } ) , or if it is within a single - line comment or block comment .
* @param script the SQL script to search within
* @param script the SQL script to search within
* @param delimiter the delimiter to search for
* @param delimiter the statement delimiter to search for
* @see # DEFAULT_COMMENT_PREFIXES
* @see # DEFAULT_BLOCK_COMMENT_START_DELIMITER
* @see # DEFAULT_BLOCK_COMMENT_END_DELIMITER
* /
* /
public static boolean containsSqlScriptDelimiters ( String script , String delimiter ) {
public static boolean containsSqlScriptDelimiters ( String script , String delimiter ) {
return containsStatementSeparator ( null , script , delimiter , DEFAULT_COMMENT_PREFIXES ,
DEFAULT_BLOCK_COMMENT_START_DELIMITER , DEFAULT_BLOCK_COMMENT_END_DELIMITER ) ;
}
/ * *
* Determine if the provided SQL script contains the specified statement separator .
* < p > This method is intended to be used to find the string separating each
* SQL statement & mdash ; for example , a ';' character .
* < p > Any occurrence of the separator within the script will be ignored if it
* is within a < em > literal < / em > block of text enclosed in single quotes
* ( { @code ' } ) or double quotes ( { @code " } ) , if it is escaped with a backslash
* ( { @code \ } ) , or if it is within a single - line comment or block comment .
* @param resource the resource from which the script was read , or { @code null }
* if unknown
* @param script the SQL script to search within
* @param separator the statement separator to search for
* @param commentPrefixes the prefixes that identify single - line comments
* ( typically { @code "--" } )
* @param blockCommentStartDelimiter the < em > start < / em > block comment delimiter
* ( typically { @code "/*" } )
* @param blockCommentEndDelimiter the < em > end < / em > block comment delimiter
* ( typically < code > "*/" < / code > )
* @since 5 . 2 . 16
* /
private static boolean containsStatementSeparator ( @Nullable EncodedResource resource , String script ,
String separator , String [ ] commentPrefixes , String blockCommentStartDelimiter ,
String blockCommentEndDelimiter ) throws ScriptException {
boolean inSingleQuote = false ;
boolean inSingleQuote = false ;
boolean inDoubleQuote = false ;
boolean inDoubleQuote = false ;
boolean inEscape = false ;
boolean inEscape = false ;
@ -446,9 +478,33 @@ public abstract class ScriptUtils {
inDoubleQuote = ! inDoubleQuote ;
inDoubleQuote = ! inDoubleQuote ;
}
}
if ( ! inSingleQuote & & ! inDoubleQuote ) {
if ( ! inSingleQuote & & ! inDoubleQuote ) {
if ( script . startsWith ( delimite r, i ) ) {
if ( script . startsWith ( separato r, i ) ) {
return true ;
return true ;
}
}
else if ( startsWithAny ( script , commentPrefixes , i ) ) {
// Skip over any content from the start of the comment to the EOL
int indexOfNextNewline = script . indexOf ( '\n' , i ) ;
if ( indexOfNextNewline > i ) {
i = indexOfNextNewline ;
continue ;
}
else {
// If there's no EOL, we must be at the end of the script, so stop here.
break ;
}
}
else if ( script . startsWith ( blockCommentStartDelimiter , i ) ) {
// Skip over any block comments
int indexOfCommentEnd = script . indexOf ( blockCommentEndDelimiter , i ) ;
if ( indexOfCommentEnd > i ) {
i = indexOfCommentEnd + blockCommentEndDelimiter . length ( ) - 1 ;
continue ;
}
else {
throw new ScriptParseException (
"Missing block comment end delimiter: " + blockCommentEndDelimiter , resource ) ;
}
}
}
}
}
}
@ -595,7 +651,9 @@ public abstract class ScriptUtils {
if ( separator = = null ) {
if ( separator = = null ) {
separator = DEFAULT_STATEMENT_SEPARATOR ;
separator = DEFAULT_STATEMENT_SEPARATOR ;
}
}
if ( ! EOF_STATEMENT_SEPARATOR . equals ( separator ) & & ! containsSqlScriptDelimiters ( script , separator ) ) {
if ( ! EOF_STATEMENT_SEPARATOR . equals ( separator ) & &
! containsStatementSeparator ( resource , script , separator , commentPrefixes ,
blockCommentStartDelimiter , blockCommentEndDelimiter ) ) {
separator = FALLBACK_STATEMENT_SEPARATOR ;
separator = FALLBACK_STATEMENT_SEPARATOR ;
}
}