Package org.testcontainers.ext
Class ScriptUtils
java.lang.Object
org.testcontainers.ext.ScriptUtils
This is a modified version of the Spring-JDBC ScriptUtils class, adapted to reduce
dependencies and slightly alter the API.
Generic utility methods for working with SQL scripts. Mainly for internal use
within the framework.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
static class
static class
-
Field Summary
FieldsModifier 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 line comments within SQL scripts.static final String
Default statement separator within SQL scripts.static final String
Fallback statement separator within SQL scripts. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsSqlScriptDelimiters
(String script, String delim) Does the provided SQL script contain the specified delimiter?static boolean
containsSqlScriptDelimiters
(String scriptPath, String script, String commentPrefix, String delim, String blockCommentStartDelimiter, String blockCommentEndDelimiter) Does the provided SQL script contain the specified delimiter?static void
executeDatabaseScript
(DatabaseDelegate databaseDelegate, String scriptPath, String script) static void
executeDatabaseScript
(DatabaseDelegate databaseDelegate, String scriptPath, String script, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) Execute the given database script.static void
runInitScript
(DatabaseDelegate databaseDelegate, String initScriptPath) Load script from classpath and apply it to the given databasestatic void
splitSqlScript
(String 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.
-
Field Details
-
DEFAULT_STATEMENT_SEPARATOR
Default statement separator within SQL scripts.- See Also:
-
FALLBACK_STATEMENT_SEPARATOR
Fallback statement separator within SQL scripts.Used if neither a custom defined separator nor the
DEFAULT_STATEMENT_SEPARATOR
is present in a given script.- See Also:
-
DEFAULT_COMMENT_PREFIX
Default prefix for line comments within SQL scripts.- See Also:
-
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:
-
-
Method Details
-
splitSqlScript
public static void splitSqlScript(String 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. 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 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
-
containsSqlScriptDelimiters
Does the provided SQL script contain the specified delimiter?- Parameters:
script
- the SQL scriptdelim
- String delimiting each statement - typically a ';' character
-
containsSqlScriptDelimiters
public static boolean containsSqlScriptDelimiters(String scriptPath, String script, String commentPrefix, String delim, String blockCommentStartDelimiter, String blockCommentEndDelimiter) Does the provided SQL script contain the specified delimiter?- Parameters:
script
- the SQL scriptdelim
- String delimiting each statement - typically a ';' charactercommentPrefix
- the prefix that identifies comments in the SQL script, typically "--"blockCommentStartDelimiter
- block comment start delimiterblockCommentEndDelimiter
- block comment end delimiter
-
runInitScript
Load script from classpath and apply it to the given database- Parameters:
databaseDelegate
- database delegate for script executioninitScriptPath
- the resource to load the init script from
-
executeDatabaseScript
public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script) throws ScriptException - Throws:
ScriptException
-
executeDatabaseScript
public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException Execute the given database script.Statement separators and comments will be removed before executing individual statements within the supplied script.
Do not use this method to execute DDL if you expect rollback.
- Parameters:
databaseDelegate
- database delegate for script executionscriptPath
- the resource (potentially associated with a specific encoding) to load the SQL script fromscript
- the raw script contentcontinueOnError
- 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 comments in the SQL script — typically "--"separator
- the script statement separator; defaults to ";" if not specified and falls back to "\n" as a last resortblockCommentStartDelimiter
- the start block comment delimiter; nevernull
or emptyblockCommentEndDelimiter
- the end block comment delimiter; nevernull
or empty @throws ScriptException if an error occurred while executing the SQL script- Throws:
ScriptException
-