类 ScriptUtils
- java.lang.Object
- org.springframework.jdbc.datasource.init.ScriptUtils
public abstract class ScriptUtils extends Object
Generic utility methods for working with SQL scripts.Mainly for internal use within the framework.
- 从以下版本开始:
- 4.0.3
- 作者:
- Thomas Risberg, Sam Brannen, Juergen Hoeller, Keith Donald, Dave Syer, Chris Beams, Oliver Gierke, Chris Baldwin, Nicolas Debeissat
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_BLOCK_COMMENT_END_DELIMITER
Default end delimiter for block comments within SQL scripts:"*/"
.static String
DEFAULT_BLOCK_COMMENT_START_DELIMITER
Default start delimiter for block comments within SQL scripts:"/*"
.static String
DEFAULT_COMMENT_PREFIX
Default prefix for single-line comments within SQL scripts:"--"
.static String
DEFAULT_STATEMENT_SEPARATOR
Default statement separator within SQL scripts:";"
.static String
EOF_STATEMENT_SEPARATOR
End of file (EOF) SQL statement separator:"^^^ END OF SCRIPT ^^^"
.static String
FALLBACK_STATEMENT_SEPARATOR
Fallback statement separator within SQL scripts:"\n"
.
构造器概要
构造器 构造器 说明 ScriptUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static boolean
containsSqlScriptDelimiters(String script, String delim)
Does the provided SQL script contain the specified delimiter?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 commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter)
Execute the given SQL script.static String
readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator)
Read a script from the providedLineNumberReader
, using the supplied comment prefix and statement separator, and build aString
containing the lines.static void
splitSqlScript(String script, char separator, List<String> statements)
Split an SQL script into separate statements delimited by the provided separator character.static void
splitSqlScript(String script, String separator, List<String> statements)
Split an SQL script into separate statements delimited by the provided separator string.static void
splitSqlScript(EncodedResource resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements)
Split an SQL script into separate statements delimited by the provided separator string.
字段详细资料
DEFAULT_STATEMENT_SEPARATOR
public static final String DEFAULT_STATEMENT_SEPARATOR
Default statement separator within SQL scripts:";"
.- 另请参阅:
- 常量字段值
FALLBACK_STATEMENT_SEPARATOR
public static final String 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.- 另请参阅:
- 常量字段值
EOF_STATEMENT_SEPARATOR
public static final String 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.- 另请参阅:
- 常量字段值
DEFAULT_COMMENT_PREFIX
public static final String DEFAULT_COMMENT_PREFIX
Default prefix for single-line comments within SQL scripts:"--"
.- 另请参阅:
- 常量字段值
DEFAULT_BLOCK_COMMENT_START_DELIMITER
public static final String DEFAULT_BLOCK_COMMENT_START_DELIMITER
Default start delimiter for block comments within SQL scripts:"/*"
.- 另请参阅:
- 常量字段值
DEFAULT_BLOCK_COMMENT_END_DELIMITER
public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER
Default end delimiter for block comments within SQL scripts:"*/"
.- 另请参阅:
- 常量字段值
构造器详细资料
ScriptUtils
public ScriptUtils()
方法详细资料
splitSqlScript
public static void splitSqlScript(String script, char separator, List<String> statements) throws ScriptException
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.
- 参数:
script
- the SQL scriptseparator
- character separating each statement — typically a ';'statements
- the list that will contain the individual statements- 抛出:
ScriptException
- if an error occurred while splitting the SQL script- 另请参阅:
splitSqlScript(String, String, List)
,splitSqlScript(EncodedResource, String, String, String, String, String, List)
splitSqlScript
public static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException
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.
- 参数:
script
- the SQL scriptseparator
- text separating each statement — typically a ';' or newline characterstatements
- the list that will contain the individual statements- 抛出:
ScriptException
- if an error occurred while splitting the SQL script- 另请参阅:
splitSqlScript(String, char, List)
,splitSqlScript(EncodedResource, String, String, String, String, String, List)
splitSqlScript
public static void splitSqlScript(EncodedResource resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) throws ScriptException
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.- 参数:
resource
- the resource from which the script was readscript
- the SQL script; nevernull
or emptyseparator
- text separating each statement — typically a ';' or newline character; nevernull
commentPrefix
- the prefix that identifies SQL line comments — typically "--"; nevernull
or emptyblockCommentStartDelimiter
- the start block comment delimiter; nevernull
or emptyblockCommentEndDelimiter
- the end block comment delimiter; nevernull
or emptystatements
- the list that will contain the individual statements- 抛出:
ScriptException
- if an error occurred while splitting the SQL script
readScript
public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator) throws IOException
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.
- 参数:
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 ";"- 返回:
- a
String
containing the script lines - 抛出:
IOException
- in case of I/O errors
containsSqlScriptDelimiters
public static boolean containsSqlScriptDelimiters(String script, String delim)
Does the provided SQL script contain the specified delimiter?- 参数:
script
- the SQL scriptdelim
- String delimiting each statement - typically a ';' character
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
.- 参数:
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- 抛出:
ScriptException
- if an error occurred while executing the SQL script- 另请参阅:
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
.- 参数:
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- 抛出:
ScriptException
- if an error occurred while executing the SQL script- 另请参阅:
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, 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
.- 参数:
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 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 delimiter; nevernull
or emptyblockCommentEndDelimiter
- the end block comment delimiter; nevernull
or empty- 抛出:
ScriptException
- if an error occurred while executing the SQL script- 另请参阅:
DEFAULT_STATEMENT_SEPARATOR
,FALLBACK_STATEMENT_SEPARATOR
,EOF_STATEMENT_SEPARATOR
,DataSourceUtils.getConnection(javax.sql.DataSource)
,DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)