diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java index 789007521a0..c3e315d0b31 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java @@ -35,7 +35,7 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** - * Generic utility methods for working with SQL scripts. + * Generic utility methods for working with SQL scripts in conjunction with JDBC. * *
Mainly for internal use within the framework. * @@ -50,6 +50,7 @@ import org.springframework.util.StringUtils; * @author Nicolas Debeissat * @author Phillip Webb * @since 4.0.3 + * @see org.springframework.r2dbc.connection.init.ScriptUtils */ public abstract class ScriptUtils { @@ -102,197 +103,207 @@ public abstract class ScriptUtils { /** - * Split an SQL script into separate statements delimited by the provided - * separator character. Each individual statement will be added to the - * provided {@code List}. - *
Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the - * comment prefix; any text beginning with the comment prefix and extending to - * the end of the line will be omitted from the output. Similarly, - * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and - * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the - * start and end block comment delimiters: any text enclosed - * in a block comment will be omitted from the output. In addition, multiple - * adjacent whitespace characters will be collapsed into a single space. - * @param script the SQL script - * @param separator character separating each statement (typically a ';') - * @param statements the list that will contain the individual statements - * @throws ScriptException if an error occurred while splitting the SQL script - * @see #splitSqlScript(String, String, List) - * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List) + * Execute the given SQL script using default settings for statement + * separators, comment delimiters, and exception handling flags. + *
Statement separators and comments will be removed before executing + * individual statements within the supplied script. + *
Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the JDBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource to load the SQL script from; encoded with the
+ * current platform's default encoding
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #DEFAULT_COMMENT_PREFIX
+ * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
+ * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
*/
- public static void splitSqlScript(String script, char separator, List Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
- * comment prefix; any text beginning with the comment prefix and extending to
- * the end of the line will be omitted from the output. Similarly,
- * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
- * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
- * start and end block comment delimiters: any text enclosed
- * in a block comment will be omitted from the output. In addition, multiple
- * adjacent whitespace characters will be collapsed into a single space.
- * @param script the SQL script
- * @param separator text separating each statement
- * (typically a ';' or newline character)
- * @param statements the list that will contain the individual statements
- * @throws ScriptException if an error occurred while splitting the SQL script
- * @see #splitSqlScript(String, char, List)
- * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
+ * Execute the given SQL script using default settings for statement
+ * separators, comment delimiters, and exception handling flags.
+ * Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the JDBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #DEFAULT_COMMENT_PREFIX
+ * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
+ * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
*/
- public static void splitSqlScript(String script, String separator, List Within the script, the provided {@code commentPrefix} will be honored:
- * any text beginning with the comment prefix and extending to the end of the
- * line will be omitted from the output. Similarly, the provided
- * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
- * delimiters will be honored: any text enclosed in a block comment will be
- * omitted from the output. In addition, multiple adjacent whitespace characters
- * will be collapsed into a single space.
- * @param resource the resource from which the script was read
- * @param script the SQL script
- * @param separator text separating each statement
- * (typically a ';' or newline character)
- * @param commentPrefix the prefix that identifies SQL line comments
- * (typically "--")
- * @param blockCommentStartDelimiter the start block comment delimiter;
- * never {@code null} or empty
- * @param blockCommentEndDelimiter the end block comment delimiter;
- * never {@code null} or empty
- * @param statements the list that will contain the individual statements
- * @throws ScriptException if an error occurred while splitting the SQL script
+ * Execute the given SQL script.
+ * Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the JDBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @param continueOnError whether or not to continue without throwing an exception
+ * in the event of an error
+ * @param ignoreFailedDrops whether or not to continue in the event of specifically
+ * an error on a {@code DROP} statement
+ * @param commentPrefix the prefix that identifies single-line comments in the
+ * SQL script (typically "--")
+ * @param separator the script statement separator; defaults to
+ * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
+ * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
+ * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
+ * single statement without a separator
+ * @param blockCommentStartDelimiter the start block comment delimiter
+ * @param blockCommentEndDelimiter the end block comment delimiter
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #FALLBACK_STATEMENT_SEPARATOR
+ * @see #EOF_STATEMENT_SEPARATOR
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
*/
- public static void splitSqlScript(@Nullable EncodedResource resource, String script,
- String separator, String commentPrefix, String blockCommentStartDelimiter,
- String blockCommentEndDelimiter, List Within the script, the provided {@code commentPrefixes} will be honored:
- * any text beginning with one of the comment prefixes and extending to the
- * end of the line will be omitted from the output. Similarly, the provided
- * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
- * delimiters will be honored: any text enclosed in a block comment will be
- * omitted from the output. In addition, multiple adjacent whitespace characters
- * will be collapsed into a single space.
- * @param resource the resource from which the script was read
- * @param script the SQL script
- * @param separator text separating each statement
- * (typically a ';' or newline character)
- * @param commentPrefixes the prefixes that identify SQL line comments
- * (typically "--")
- * @param blockCommentStartDelimiter the start block comment delimiter;
- * never {@code null} or empty
- * @param blockCommentEndDelimiter the end block comment delimiter;
- * never {@code null} or empty
- * @param statements the list that will contain the individual statements
- * @throws ScriptException if an error occurred while splitting the SQL script
+ * Execute the given SQL script.
+ * Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the JDBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @param continueOnError whether or not to continue without throwing an exception
+ * in the event of an error
+ * @param ignoreFailedDrops whether or not to continue in the event of specifically
+ * an error on a {@code DROP} statement
+ * @param commentPrefixes the prefixes that identify single-line comments in the
+ * SQL script (typically "--")
+ * @param separator the script statement separator; defaults to
+ * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
+ * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
+ * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
+ * single statement without a separator
+ * @param blockCommentStartDelimiter the start block comment delimiter
+ * @param blockCommentEndDelimiter the end block comment delimiter
+ * @throws ScriptException if an error occurred while executing the SQL script
* @since 5.2
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #FALLBACK_STATEMENT_SEPARATOR
+ * @see #EOF_STATEMENT_SEPARATOR
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
+ * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
*/
- public static void splitSqlScript(@Nullable EncodedResource resource, String script,
- String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
- String blockCommentEndDelimiter, List This method is intended to be used to find the string delimiting each
@@ -441,10 +443,185 @@ public abstract class ScriptUtils {
* (typically Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
+ * comment prefix; any text beginning with the comment prefix and extending to
+ * the end of the line will be omitted from the output. Similarly,
+ * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
+ * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
+ * start and end block comment delimiters: any text enclosed
+ * in a block comment will be omitted from the output. In addition, multiple
+ * adjacent whitespace characters will be collapsed into a single space.
+ * @param script the SQL script
+ * @param separator character separating each statement (typically a ';')
+ * @param statements the list that will contain the individual statements
+ * @throws ScriptException if an error occurred while splitting the SQL script
+ * @see #splitSqlScript(String, String, List)
+ * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
+ */
+ public static void splitSqlScript(String script, char separator, List Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
+ * comment prefix; any text beginning with the comment prefix and extending to
+ * the end of the line will be omitted from the output. Similarly,
+ * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
+ * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
+ * start and end block comment delimiters: any text enclosed
+ * in a block comment will be omitted from the output. In addition, multiple
+ * adjacent whitespace characters will be collapsed into a single space.
+ * @param script the SQL script
+ * @param separator text separating each statement
+ * (typically a ';' or newline character)
+ * @param statements the list that will contain the individual statements
+ * @throws ScriptException if an error occurred while splitting the SQL script
+ * @see #splitSqlScript(String, char, List)
+ * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
+ */
+ public static void splitSqlScript(String script, String separator, List Within the script, the provided {@code commentPrefix} will be honored:
+ * any text beginning with the comment prefix and extending to the end of the
+ * line will be omitted from the output. Similarly, the provided
+ * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
+ * delimiters will be honored: any text enclosed in a block comment will be
+ * omitted from the output. In addition, multiple adjacent whitespace characters
+ * will be collapsed into a single space.
+ * @param resource the resource from which the script was read
+ * @param script the SQL script
+ * @param separator text separating each statement
+ * (typically a ';' or newline character)
+ * @param commentPrefix the prefix that identifies SQL line comments
+ * (typically "--")
+ * @param blockCommentStartDelimiter the start block comment delimiter;
+ * never {@code null} or empty
+ * @param blockCommentEndDelimiter the end block comment delimiter;
+ * never {@code null} or empty
+ * @param statements the list that will contain the individual statements
+ * @throws ScriptException if an error occurred while splitting the SQL script
+ */
+ public static void splitSqlScript(@Nullable EncodedResource resource, String script,
+ String separator, String commentPrefix, String blockCommentStartDelimiter,
+ String blockCommentEndDelimiter, List Within the script, the provided {@code commentPrefixes} will be honored:
+ * any text beginning with one of the comment prefixes and extending to the
+ * end of the line will be omitted from the output. Similarly, the provided
+ * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
+ * delimiters will be honored: any text enclosed in a block comment will be
+ * omitted from the output. In addition, multiple adjacent whitespace characters
+ * will be collapsed into a single space.
+ * @param resource the resource from which the script was read
+ * @param script the SQL script
+ * @param separator text separating each statement
+ * (typically a ';' or newline character)
+ * @param commentPrefixes the prefixes that identify SQL line comments
+ * (typically "--")
+ * @param blockCommentStartDelimiter the start block comment delimiter;
+ * never {@code null} or empty
+ * @param blockCommentEndDelimiter the end block comment delimiter;
+ * never {@code null} or empty
+ * @param statements the list that will contain the individual statements
+ * @throws ScriptException if an error occurred while splitting the SQL script
+ * @since 5.2
+ */
+ public static void splitSqlScript(@Nullable EncodedResource resource, String script,
String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
- String blockCommentEndDelimiter) throws ScriptException {
+ String blockCommentEndDelimiter, List Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the JDBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource to load the SQL script from; encoded with the
- * current platform's default encoding
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #DEFAULT_COMMENT_PREFIX
- * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
- * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
- * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
- * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
- */
- public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException {
- executeSqlScript(connection, new EncodedResource(resource));
- }
-
- /**
- * Execute the given SQL script using default settings for statement
- * separators, comment delimiters, and exception handling flags.
- * Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the JDBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #DEFAULT_COMMENT_PREFIX
- * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
- * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
- * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
- * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
- */
- public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
- executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR,
- DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
- }
-
- /**
- * Execute the given SQL script.
- * Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the JDBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @param continueOnError whether or not to continue without throwing an exception
- * in the event of an error
- * @param ignoreFailedDrops whether or not to continue in the event of specifically
- * an error on a {@code DROP} statement
- * @param commentPrefix the prefix that identifies single-line comments in the
- * SQL script (typically "--")
- * @param separator the script statement separator; defaults to
- * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
- * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
- * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
- * single statement without a separator
- * @param blockCommentStartDelimiter the start block comment delimiter
- * @param blockCommentEndDelimiter the end block comment delimiter
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #FALLBACK_STATEMENT_SEPARATOR
- * @see #EOF_STATEMENT_SEPARATOR
- * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
- * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
- */
- public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError,
- boolean ignoreFailedDrops, String commentPrefix, @Nullable String separator,
- String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException {
-
- executeSqlScript(connection, resource, continueOnError, ignoreFailedDrops,
- new String[] { commentPrefix }, separator, blockCommentStartDelimiter,
- blockCommentEndDelimiter);
- }
-
- /**
- * Execute the given SQL script.
- * Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the JDBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @param continueOnError whether or not to continue without throwing an exception
- * in the event of an error
- * @param ignoreFailedDrops whether or not to continue in the event of specifically
- * an error on a {@code DROP} statement
- * @param commentPrefixes the prefixes that identify single-line comments in the
- * SQL script (typically "--")
- * @param separator the script statement separator; defaults to
- * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
- * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
- * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
- * single statement without a separator
- * @param blockCommentStartDelimiter the start block comment delimiter
- * @param blockCommentEndDelimiter the end block comment delimiter
- * @throws ScriptException if an error occurred while executing the SQL script
- * @since 5.2
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #FALLBACK_STATEMENT_SEPARATOR
- * @see #EOF_STATEMENT_SEPARATOR
- * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
- * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
- */
- public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError,
- boolean ignoreFailedDrops, String[] commentPrefixes, @Nullable String separator,
- String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException {
-
- try {
- if (logger.isDebugEnabled()) {
- logger.debug("Executing SQL script from " + resource);
- }
- long startTime = System.currentTimeMillis();
-
- String script;
- try {
- script = readScript(resource, separator, commentPrefixes, blockCommentEndDelimiter);
- }
- catch (IOException ex) {
- throw new CannotReadScriptException(resource, ex);
- }
-
- if (separator == null) {
- separator = DEFAULT_STATEMENT_SEPARATOR;
- }
- if (!EOF_STATEMENT_SEPARATOR.equals(separator) &&
- !containsStatementSeparator(resource, script, separator, commentPrefixes,
- blockCommentStartDelimiter, blockCommentEndDelimiter)) {
- separator = FALLBACK_STATEMENT_SEPARATOR;
- }
-
- List Mainly for internal use within the framework.
*
@@ -59,6 +59,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @author Mark Paluch
* @since 5.3
+ * @see org.springframework.jdbc.datasource.init.ScriptUtils
*/
public abstract class ScriptUtils {
@@ -105,119 +106,171 @@ public abstract class ScriptUtils {
/**
- * Split an SQL script into separate statements delimited by the provided
- * separator string and return a {@code List} containing each individual
- * statement.
- * Within the script, the provided {@code commentPrefixes} will be honored:
- * any text beginning with one of the comment prefixes and extending to the
- * end of the line will be omitted from the output. Similarly, the provided
- * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
- * delimiters will be honored: any text enclosed in a block comment will be
- * omitted from the output. In addition, multiple adjacent whitespace characters
- * will be collapsed into a single space.
- * @param resource the resource from which the script was read
- * @param script the SQL script
- * @param separator text separating each statement
- * (typically a ';' or newline character)
- * @param commentPrefixes the prefixes that identify SQL line comments
- * (typically "--")
- * @param blockCommentStartDelimiter the start block comment delimiter;
- * never {@code null} or empty
- * @param blockCommentEndDelimiter the end block comment delimiter;
- * never {@code null} or empty
- * @return a list of statements
- * @throws ScriptException if an error occurred while splitting the SQL script
+ * Execute the given SQL script using default settings for statement
+ * separators, comment delimiters, and exception handling flags.
+ * Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the R2DBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource to load the SQL script from; encoded with the
+ * current platform's default encoding
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #DEFAULT_COMMENT_PREFIXES
+ * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
+ * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
*/
- static List Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the R2DBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #DEFAULT_COMMENT_PREFIXES
+ * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
+ * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
+ */
+ public static Mono Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the R2DBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @param dataBufferFactory the factory to create data buffers with
+ * @param continueOnError whether or not to continue without throwing an exception
+ * in the event of an error
+ * @param ignoreFailedDrops whether or not to continue in the event of specifically
+ * an error on a {@code DROP} statement
+ * @param commentPrefix the prefix that identifies single-line comments in the
+ * SQL script (typically "--")
+ * @param separator the script statement separator; defaults to
+ * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
+ * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
+ * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
+ * single statement without a separator
+ * @param blockCommentStartDelimiter the start block comment delimiter
+ * @param blockCommentEndDelimiter the end block comment delimiter
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #FALLBACK_STATEMENT_SEPARATOR
+ * @see #EOF_STATEMENT_SEPARATOR
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
+ */
+ public static Mono Statement separators and comments will be removed before executing
+ * individual statements within the supplied script.
+ * Warning: this method does not release the
+ * provided {@link Connection}.
+ * @param connection the R2DBC connection to use to execute the script; already
+ * configured and ready to use
+ * @param resource the resource (potentially associated with a specific encoding)
+ * to load the SQL script from
+ * @param dataBufferFactory the factory to create data buffers with
+ * @param continueOnError whether or not to continue without throwing an exception
+ * in the event of an error
+ * @param ignoreFailedDrops whether or not to continue in the event of specifically
+ * an error on a {@code DROP} statement
+ * @param commentPrefixes the prefixes that identify single-line comments in the
+ * SQL script (typically "--")
+ * @param separator the script statement separator; defaults to
+ * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
+ * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
+ * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
+ * single statement without a separator
+ * @param blockCommentStartDelimiter the start block comment delimiter
+ * @param blockCommentEndDelimiter the end block comment delimiter
+ * @throws ScriptException if an error occurred while executing the SQL script
+ * @see #DEFAULT_STATEMENT_SEPARATOR
+ * @see #FALLBACK_STATEMENT_SEPARATOR
+ * @see #EOF_STATEMENT_SEPARATOR
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
+ * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
+ */
+ public static Mono This method is intended to be used to find the string separating each
@@ -379,171 +423,128 @@ public abstract class ScriptUtils {
}
/**
- * Execute the given SQL script using default settings for statement
- * separators, comment delimiters, and exception handling flags.
- * Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the R2DBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource to load the SQL script from; encoded with the
- * current platform's default encoding
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #DEFAULT_COMMENT_PREFIXES
- * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
- * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
- */
- public static Mono Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the R2DBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String)
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #DEFAULT_COMMENT_PREFIXES
- * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
- * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
- */
- public static Mono Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the R2DBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @param dataBufferFactory the factory to create data buffers with
- * @param continueOnError whether or not to continue without throwing an exception
- * in the event of an error
- * @param ignoreFailedDrops whether or not to continue in the event of specifically
- * an error on a {@code DROP} statement
- * @param commentPrefix the prefix that identifies single-line comments in the
- * SQL script (typically "--")
- * @param separator the script statement separator; defaults to
- * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
- * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
- * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
- * single statement without a separator
- * @param blockCommentStartDelimiter the start block comment delimiter
- * @param blockCommentEndDelimiter the end block comment delimiter
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #FALLBACK_STATEMENT_SEPARATOR
- * @see #EOF_STATEMENT_SEPARATOR
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
- */
- public static Mono Statement separators and comments will be removed before executing
- * individual statements within the supplied script.
- * Warning: this method does not release the
- * provided {@link Connection}.
- * @param connection the R2DBC connection to use to execute the script; already
- * configured and ready to use
- * @param resource the resource (potentially associated with a specific encoding)
- * to load the SQL script from
- * @param dataBufferFactory the factory to create data buffers with
- * @param continueOnError whether or not to continue without throwing an exception
- * in the event of an error
- * @param ignoreFailedDrops whether or not to continue in the event of specifically
- * an error on a {@code DROP} statement
- * @param commentPrefixes the prefixes that identify single-line comments in the
- * SQL script (typically "--")
- * @param separator the script statement separator; defaults to
- * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to
- * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to
- * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a
- * single statement without a separator
- * @param blockCommentStartDelimiter the start block comment delimiter
- * @param blockCommentEndDelimiter the end block comment delimiter
- * @throws ScriptException if an error occurred while executing the SQL script
- * @see #DEFAULT_STATEMENT_SEPARATOR
- * @see #FALLBACK_STATEMENT_SEPARATOR
- * @see #EOF_STATEMENT_SEPARATOR
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection
- * @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
+ * Split an SQL script into separate statements delimited by the provided
+ * separator string and return a {@code List} containing each individual
+ * statement.
+ * Within the script, the provided {@code commentPrefixes} will be honored:
+ * any text beginning with one of the comment prefixes and extending to the
+ * end of the line will be omitted from the output. Similarly, the provided
+ * {@code blockCommentStartDelimiter} and {@code blockCommentEndDelimiter}
+ * delimiters will be honored: any text enclosed in a block comment will be
+ * omitted from the output. In addition, multiple adjacent whitespace characters
+ * will be collapsed into a single space.
+ * @param resource the resource from which the script was read
+ * @param script the SQL script
+ * @param separator text separating each statement
+ * (typically a ';' or newline character)
+ * @param commentPrefixes the prefixes that identify SQL line comments
+ * (typically "--")
+ * @param blockCommentStartDelimiter the start block comment delimiter;
+ * never {@code null} or empty
+ * @param blockCommentEndDelimiter the end block comment delimiter;
+ * never {@code null} or empty
+ * @return a list of statements
+ * @throws ScriptException if an error occurred while splitting the SQL script
*/
- public static Mono"*/")
* @since 5.2.16
*/
- private static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script,
+ private static boolean containsStatementSeparator(@Nullable EncodedResource resource, String script,
+ String separator, String[] commentPrefixes, String blockCommentStartDelimiter,
+ String blockCommentEndDelimiter) throws ScriptException {
+
+ boolean inSingleQuote = false;
+ boolean inDoubleQuote = false;
+ boolean inEscape = false;
+
+ for (int i = 0; i < script.length(); i++) {
+ char c = script.charAt(i);
+ if (inEscape) {
+ inEscape = false;
+ continue;
+ }
+ // MySQL style escapes
+ if (c == '\\') {
+ inEscape = true;
+ continue;
+ }
+ if (!inDoubleQuote && (c == '\'')) {
+ inSingleQuote = !inSingleQuote;
+ }
+ else if (!inSingleQuote && (c == '"')) {
+ inDoubleQuote = !inDoubleQuote;
+ }
+ if (!inSingleQuote && !inDoubleQuote) {
+ if (script.startsWith(separator, i)) {
+ 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);
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Split an SQL script into separate statements delimited by the provided
+ * separator character. Each individual statement will be added to the
+ * provided {@code List}.
+ *