Class ScriptUtils
Mainly for internal use within the framework.
- Since:
- 4.0.3
- Author:
- Thomas Risberg, Sam Brannen, Juergen Hoeller, Keith Donald, Dave Syer, Chris Beams, Oliver Gierke, Chris Baldwin, Nicolas Debeissat, Phillip Webb
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Default end delimiter for block comments within SQL scripts:"*/"
.static final String
Default start delimiter for block comments within SQL scripts:"/*"
.static final String
Default prefix for single-line comments within SQL scripts:"--"
.static final String[]
Default prefixes for single-line comments within SQL scripts:["--"]
.static final String
Default statement separator within SQL scripts:";"
.static final String
End of file (EOF) SQL statement separator:"^^^ END OF SCRIPT ^^^"
.static final String
Fallback statement separator within SQL scripts:"\n"
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsSqlScriptDelimiters
(String script, String delimiter) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static void
executeSqlScript
(Connection connection, Resource resource) Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.static void
executeSqlScript
(Connection connection, EncodedResource resource) Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.static void
executeSqlScript
(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) Execute the given SQL script.static void
executeSqlScript
(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) Execute the given SQL script.static String
readScript
(LineNumberReader lineNumberReader, String[] commentPrefixes, String separator, String blockCommentEndDelimiter) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static String
readScript
(LineNumberReader lineNumberReader, String commentPrefix, String separator, String blockCommentEndDelimiter) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static void
splitSqlScript
(String script, char separator, List<String> statements) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static void
splitSqlScript
(String script, String separator, List<String> statements) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static void
splitSqlScript
(EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.static void
splitSqlScript
(EncodedResource resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) Deprecated.as of Spring Framework 5.2.16 with no plans for replacement.
-
Field Details
-
DEFAULT_STATEMENT_SEPARATOR
Default statement separator within SQL scripts:";"
.- See Also:
-
FALLBACK_STATEMENT_SEPARATOR
Fallback statement separator within SQL scripts:"\n"
.Used if neither a custom separator nor the
DEFAULT_STATEMENT_SEPARATOR
is present in a given script.- See Also:
-
EOF_STATEMENT_SEPARATOR
End of file (EOF) SQL statement separator:"^^^ END OF SCRIPT ^^^"
.This value may be supplied as the
separator
toexecuteSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
to denote that an SQL script contains a single statement (potentially spanning multiple lines) with no explicit statement separator. Note that such a script should not actually contain this value; it is merely a virtual statement separator.- See Also:
-
DEFAULT_COMMENT_PREFIX
Default prefix for single-line comments within SQL scripts:"--"
.- See Also:
-
DEFAULT_COMMENT_PREFIXES
Default prefixes for single-line comments within SQL scripts:["--"]
.- Since:
- 5.2
-
DEFAULT_BLOCK_COMMENT_START_DELIMITER
Default start delimiter for block comments within SQL scripts:"/*"
.- See Also:
-
DEFAULT_BLOCK_COMMENT_END_DELIMITER
Default end delimiter for block comments within SQL scripts:"*/"
.- See Also:
-
-
Constructor Details
-
ScriptUtils
public ScriptUtils()
-
-
Method Details
-
executeSqlScript
public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException 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
Connection
.- Parameters:
connection
- the JDBC connection to use to execute the script; already configured and ready to useresource
- 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 Also:
-
executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
DEFAULT_STATEMENT_SEPARATOR
DEFAULT_COMMENT_PREFIX
DEFAULT_BLOCK_COMMENT_START_DELIMITER
DEFAULT_BLOCK_COMMENT_END_DELIMITER
DataSourceUtils.getConnection(javax.sql.DataSource)
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)
-
executeSqlScript
public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException 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
Connection
.- Parameters:
connection
- the JDBC connection to use to execute the script; already configured and ready to useresource
- 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 Also:
-
executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
DEFAULT_STATEMENT_SEPARATOR
DEFAULT_COMMENT_PREFIX
DEFAULT_BLOCK_COMMENT_START_DELIMITER
DEFAULT_BLOCK_COMMENT_END_DELIMITER
DataSourceUtils.getConnection(javax.sql.DataSource)
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)
-
executeSqlScript
public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException 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
Connection
.- Parameters:
connection
- the JDBC connection to use to execute the script; already configured and ready to useresource
- the resource (potentially associated with a specific encoding) to load the SQL script fromcontinueOnError
- whether to continue without throwing an exception in the event of an errorignoreFailedDrops
- whether to continue in the event of specifically an error on aDROP
statementcommentPrefix
- the prefix that identifies single-line comments in the SQL script (typically "--")separator
- the script statement separator; defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to "^^^ END OF SCRIPT ^^^" to signal that the script contains a single statement without a separatorblockCommentStartDelimiter
- the start block comment delimiterblockCommentEndDelimiter
- the end block comment delimiter- Throws:
ScriptException
- if an error occurred while executing the SQL script- See Also:
-
executeSqlScript
public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException 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
Connection
.- Parameters:
connection
- the JDBC connection to use to execute the script; already configured and ready to useresource
- the resource (potentially associated with a specific encoding) to load the SQL script fromcontinueOnError
- whether to continue without throwing an exception in the event of an errorignoreFailedDrops
- whether to continue in the event of specifically an error on aDROP
statementcommentPrefixes
- the prefixes that identify single-line comments in the SQL script (typically "--")separator
- the script statement separator; defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to "^^^ END OF SCRIPT ^^^" to signal that the script contains a single statement without a separatorblockCommentStartDelimiter
- the start block comment delimiterblockCommentEndDelimiter
- the end block comment delimiter- Throws:
ScriptException
- if an error occurred while executing the SQL script- Since:
- 5.2
- See Also:
-
readScript
@Deprecated public static String readScript(LineNumberReader lineNumberReader, @Nullable String commentPrefix, @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Read a script from the providedLineNumberReader
, using the supplied comment prefix and statement separator, and build aString
containing the lines.Lines beginning with the comment prefix are excluded from the results; however, line comments anywhere else — for example, within a statement — will be included in the results.
- Parameters:
lineNumberReader
- theLineNumberReader
containing the script to be processedcommentPrefix
- the prefix that identifies comments in the SQL script (typically "--")separator
- the statement separator in the SQL script (typically ";")blockCommentEndDelimiter
- the end block comment delimiter- Returns:
- a
String
containing the script lines - Throws:
IOException
- in case of I/O errors
-
readScript
@Deprecated public static String readScript(LineNumberReader lineNumberReader, @Nullable String[] commentPrefixes, @Nullable String separator, @Nullable String blockCommentEndDelimiter) throws IOException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Read a script from the providedLineNumberReader
, using the supplied comment prefixes and statement separator, and build aString
containing the lines.Lines beginning with one of the comment prefixes are excluded from the results; however, line comments anywhere else — for example, within a statement — will be included in the results.
- Parameters:
lineNumberReader
- theLineNumberReader
containing the script to be processedcommentPrefixes
- the prefixes that identify comments in the SQL script (typically "--")separator
- the statement separator in the SQL script (typically ";")blockCommentEndDelimiter
- the end block comment delimiter- Returns:
- a
String
containing the script lines - Throws:
IOException
- in case of I/O errors- Since:
- 5.2
-
containsSqlScriptDelimiters
Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Determine if the provided SQL script contains the specified delimiter.This method is intended to be used to find the string delimiting each SQL statement — for example, a ';' character.
Any occurrence of the delimiter within the script will be ignored if it is within a literal block of text enclosed in single quotes (
'
) or double quotes ("
), if it is escaped with a backslash (\
), or if it is within a single-line comment or block comment.- Parameters:
script
- the SQL script to search withindelimiter
- the statement delimiter to search for- See Also:
-
splitSqlScript
@Deprecated public static void splitSqlScript(String script, char separator, List<String> statements) throws ScriptException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Split an SQL script into separate statements delimited by the provided separator character. Each individual statement will be added to the providedList
.Within the script, "--" 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, "/*" and "*/" 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.
- Parameters:
script
- the SQL scriptseparator
- character separating each statement (typically a ';')statements
- the list that will contain the individual statements- Throws:
ScriptException
- if an error occurred while splitting the SQL script- See Also:
-
splitSqlScript
@Deprecated public static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Split an SQL script into separate statements delimited by the provided separator string. Each individual statement will be added to the providedList
.Within the script, "--" 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, "/*" and "*/" 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.
- Parameters:
script
- the SQL scriptseparator
- text separating each statement (typically a ';' or newline character)statements
- the list that will contain the individual statements- Throws:
ScriptException
- if an error occurred while splitting the SQL script- See Also:
-
splitSqlScript
@Deprecated public static void splitSqlScript(@Nullable EncodedResource resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) throws ScriptException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Split an SQL script into separate statements delimited by the provided separator string. Each individual statement will be added to the providedList
.Within the script, the provided
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 providedblockCommentStartDelimiter
andblockCommentEndDelimiter
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.- Parameters:
resource
- the resource from which the script was readscript
- the SQL scriptseparator
- text separating each statement (typically a ';' or newline character)commentPrefix
- the prefix that identifies SQL line comments (typically "--")blockCommentStartDelimiter
- the start block comment delimiter; nevernull
or emptyblockCommentEndDelimiter
- the end block comment delimiter; nevernull
or emptystatements
- the list that will contain the individual statements- Throws:
ScriptException
- if an error occurred while splitting the SQL script
-
splitSqlScript
@Deprecated public static void splitSqlScript(@Nullable EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) throws ScriptException Deprecated.as of Spring Framework 5.2.16 with no plans for replacement. This is an internal API and will likely be removed in Spring Framework 6.0.Split an SQL script into separate statements delimited by the provided separator string. Each individual statement will be added to the providedList
.Within the script, the provided
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 providedblockCommentStartDelimiter
andblockCommentEndDelimiter
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.- Parameters:
resource
- the resource from which the script was readscript
- the SQL scriptseparator
- text separating each statement (typically a ';' or newline character)commentPrefixes
- the prefixes that identify SQL line comments (typically "--")blockCommentStartDelimiter
- the start block comment delimiter; nevernull
or emptyblockCommentEndDelimiter
- the end block comment delimiter; nevernull
or emptystatements
- the list that will contain the individual statements- Throws:
ScriptException
- if an error occurred while splitting the SQL script- Since:
- 5.2
-