public abstract class ScriptUtils
extends java.lang.Object
Mainly for internal use within the framework.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_BLOCK_COMMENT_END_DELIMITER
Default end delimiter for block comments within SQL scripts:
"*/" . |
static java.lang.String |
DEFAULT_BLOCK_COMMENT_START_DELIMITER
Default start delimiter for block comments within SQL scripts:
"/*" . |
static java.lang.String |
DEFAULT_COMMENT_PREFIX
Default prefix for single-line comments within SQL scripts:
"--" . |
static java.lang.String |
DEFAULT_STATEMENT_SEPARATOR
Default statement separator within SQL scripts:
";" . |
static java.lang.String |
EOF_STATEMENT_SEPARATOR
End of file (EOF) SQL statement separator:
"^^^ END OF SCRIPT ^^^" . |
static java.lang.String |
FALLBACK_STATEMENT_SEPARATOR
Fallback statement separator within SQL scripts:
"\n" . |
Constructor and Description |
---|
ScriptUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsSqlScriptDelimiters(java.lang.String script,
java.lang.String delim)
Does the provided SQL script contain the specified delimiter?
|
static void |
executeSqlScript(java.sql.Connection connection,
EncodedResource resource)
Execute the given SQL script using default settings for statement
separators, comment delimiters, and exception handling flags.
|
static void |
executeSqlScript(java.sql.Connection connection,
EncodedResource resource,
boolean continueOnError,
boolean ignoreFailedDrops,
java.lang.String commentPrefix,
java.lang.String separator,
java.lang.String blockCommentStartDelimiter,
java.lang.String blockCommentEndDelimiter)
Execute the given SQL script.
|
static void |
executeSqlScript(java.sql.Connection connection,
Resource resource)
Execute the given SQL script using default settings for statement
separators, comment delimiters, and exception handling flags.
|
static java.lang.String |
readScript(java.io.LineNumberReader lineNumberReader,
java.lang.String commentPrefix,
java.lang.String separator)
Read a script from the provided
LineNumberReader , using the supplied
comment prefix and statement separator, and build a String containing
the lines. |
static void |
splitSqlScript(EncodedResource resource,
java.lang.String script,
java.lang.String separator,
java.lang.String commentPrefix,
java.lang.String blockCommentStartDelimiter,
java.lang.String blockCommentEndDelimiter,
java.util.List<java.lang.String> statements)
Split an SQL script into separate statements delimited by the provided
separator string.
|
static void |
splitSqlScript(java.lang.String script,
char separator,
java.util.List<java.lang.String> statements)
Split an SQL script into separate statements delimited by the provided
separator character.
|
static void |
splitSqlScript(java.lang.String script,
java.lang.String separator,
java.util.List<java.lang.String> statements)
Split an SQL script into separate statements delimited by the provided
separator string.
|
public static final java.lang.String DEFAULT_STATEMENT_SEPARATOR
";"
.public static final java.lang.String FALLBACK_STATEMENT_SEPARATOR
"\n"
.
Used if neither a custom separator nor the
DEFAULT_STATEMENT_SEPARATOR
is present in a given script.
public static final java.lang.String EOF_STATEMENT_SEPARATOR
"^^^ END OF SCRIPT ^^^"
.
This value may be supplied as the separator
to executeSqlScript(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.
public static final java.lang.String DEFAULT_COMMENT_PREFIX
"--"
.public static final java.lang.String DEFAULT_BLOCK_COMMENT_START_DELIMITER
"/*"
.public static final java.lang.String DEFAULT_BLOCK_COMMENT_END_DELIMITER
"*/"
.public static void splitSqlScript(java.lang.String script, char separator, java.util.List<java.lang.String> statements) throws ScriptException
List
.
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.
script
- the SQL scriptseparator
- character separating each statement — typically a ';'statements
- the list that will contain the individual statementsScriptException
- if an error occurred while splitting the SQL scriptsplitSqlScript(String, String, List)
,
splitSqlScript(EncodedResource, String, String, String, String, String, List)
public static void splitSqlScript(java.lang.String script, java.lang.String separator, java.util.List<java.lang.String> statements) throws ScriptException
List
.
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.
script
- the SQL scriptseparator
- text separating each statement — typically a ';' or newline characterstatements
- the list that will contain the individual statementsScriptException
- if an error occurred while splitting the SQL scriptsplitSqlScript(String, char, List)
,
splitSqlScript(EncodedResource, String, String, String, String, String, List)
public static void splitSqlScript(@Nullable EncodedResource resource, java.lang.String script, java.lang.String separator, java.lang.String commentPrefix, java.lang.String blockCommentStartDelimiter, java.lang.String blockCommentEndDelimiter, java.util.List<java.lang.String> statements) throws ScriptException
List
.
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 provided
blockCommentStartDelimiter
and 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.
resource
- the resource from which the script was readscript
- the SQL script; never null
or emptyseparator
- text separating each statement — typically a ';' or
newline character; never null
commentPrefix
- the prefix that identifies SQL line comments —
typically "--"; never null
or emptyblockCommentStartDelimiter
- the start block comment delimiter;
never null
or emptyblockCommentEndDelimiter
- the end block comment delimiter;
never null
or emptystatements
- the list that will contain the individual statementsScriptException
- if an error occurred while splitting the SQL scriptpublic static java.lang.String readScript(java.io.LineNumberReader lineNumberReader, @Nullable java.lang.String commentPrefix, @Nullable java.lang.String separator) throws java.io.IOException
LineNumberReader
, using the supplied
comment prefix and statement separator, and build a String
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.
lineNumberReader
- the LineNumberReader
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 ";"String
containing the script linesjava.io.IOException
- in case of I/O errorspublic static boolean containsSqlScriptDelimiters(java.lang.String script, java.lang.String delim)
script
- the SQL scriptdelim
- String delimiting each statement - typically a ';' characterpublic static void executeSqlScript(java.sql.Connection connection, Resource resource) throws ScriptException
Statement separators and comments will be removed before executing individual statements within the supplied script.
Warning: this method does not release the
provided Connection
.
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 encodingScriptException
- if an error occurred while executing the SQL scriptexecuteSqlScript(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)
public static void executeSqlScript(java.sql.Connection connection, EncodedResource resource) throws ScriptException
Statement separators and comments will be removed before executing individual statements within the supplied script.
Warning: this method does not release the
provided Connection
.
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 fromScriptException
- if an error occurred while executing the SQL scriptexecuteSqlScript(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)
public static void executeSqlScript(java.sql.Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, java.lang.String commentPrefix, @Nullable java.lang.String separator, java.lang.String blockCommentStartDelimiter, java.lang.String blockCommentEndDelimiter) throws ScriptException
Statement separators and comments will be removed before executing individual statements within the supplied script.
Warning: this method does not release the
provided Connection
.
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 or not to continue without throwing an exception
in the event of an errorignoreFailedDrops
- whether or not to continue in the event of specifically
an error on a DROP
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 delimiter; never
null
or emptyblockCommentEndDelimiter
- the end block comment delimiter; never
null
or emptyScriptException
- if an error occurred while executing the SQL scriptDEFAULT_STATEMENT_SEPARATOR
,
FALLBACK_STATEMENT_SEPARATOR
,
EOF_STATEMENT_SEPARATOR
,
DataSourceUtils.getConnection(javax.sql.DataSource)
,
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)