Package org.testcontainers.ext
Class ScriptUtils
java.lang.Object
org.testcontainers.ext.ScriptUtils
public abstract class ScriptUtils
extends java.lang.Object
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.
- Since:
- 4.0.3
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classScriptUtils.ScriptLoadExceptionstatic classScriptUtils.ScriptParseExceptionstatic classScriptUtils.ScriptStatementFailedExceptionstatic classScriptUtils.UncategorizedScriptException -
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_BLOCK_COMMENT_END_DELIMITERDefault end delimiter for block comments within SQL scripts.static java.lang.StringDEFAULT_BLOCK_COMMENT_START_DELIMITERDefault start delimiter for block comments within SQL scripts.static java.lang.StringDEFAULT_COMMENT_PREFIXDefault prefix for line comments within SQL scripts.static java.lang.StringDEFAULT_STATEMENT_SEPARATORDefault statement separator within SQL scripts.static java.lang.StringFALLBACK_STATEMENT_SEPARATORFallback statement separator within SQL scripts. -
Method Summary
Modifier and Type Method Description static booleancontainsSqlScriptDelimiters(java.lang.String script, java.lang.String delim)Does the provided SQL script contain the specified delimiter?static voidexecuteDatabaseScript(DatabaseDelegate databaseDelegate, java.lang.String scriptPath, java.lang.String script)static voidexecuteDatabaseScript(DatabaseDelegate databaseDelegate, java.lang.String scriptPath, java.lang.String script, boolean continueOnError, boolean ignoreFailedDrops, java.lang.String commentPrefix, java.lang.String separator, java.lang.String blockCommentStartDelimiter, java.lang.String blockCommentEndDelimiter)Execute the given database script.static voidrunInitScript(DatabaseDelegate databaseDelegate, java.lang.String initScriptPath)Load script from classpath and apply it to the given databasestatic voidsplitSqlScript(java.lang.String 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.
-
Field Details
-
DEFAULT_STATEMENT_SEPARATOR
public static final java.lang.String DEFAULT_STATEMENT_SEPARATORDefault statement separator within SQL scripts.- See Also:
- Constant Field Values
-
FALLBACK_STATEMENT_SEPARATOR
public static final java.lang.String FALLBACK_STATEMENT_SEPARATORFallback statement separator within SQL scripts.Used if neither a custom defined separator nor the
DEFAULT_STATEMENT_SEPARATORis present in a given script.- See Also:
- Constant Field Values
-
DEFAULT_COMMENT_PREFIX
public static final java.lang.String DEFAULT_COMMENT_PREFIXDefault prefix for line comments within SQL scripts.- See Also:
- Constant Field Values
-
DEFAULT_BLOCK_COMMENT_START_DELIMITER
public static final java.lang.String DEFAULT_BLOCK_COMMENT_START_DELIMITERDefault start delimiter for block comments within SQL scripts.- See Also:
- Constant Field Values
-
DEFAULT_BLOCK_COMMENT_END_DELIMITER
public static final java.lang.String DEFAULT_BLOCK_COMMENT_END_DELIMITERDefault end delimiter for block comments within SQL scripts.- See Also:
- Constant Field Values
-
-
Method Details
-
splitSqlScript
public static void splitSqlScript(java.lang.String 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. Each individual statement will be added to the providedList.Within the script, the provided
commentPrefixwill 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 providedblockCommentStartDelimiterandblockCommentEndDelimiterdelimiters 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; nevernullor emptyseparator- text separating each statement — typically a ';' or newline character; nevernullcommentPrefix- the prefix that identifies SQL line comments — typically "--"; nevernullor emptyblockCommentStartDelimiter- the start block comment delimiter; nevernullor emptyblockCommentEndDelimiter- the end block comment delimiter; nevernullor emptystatements- the list that will contain the individual statements
-
containsSqlScriptDelimiters
public static boolean containsSqlScriptDelimiters(java.lang.String script, java.lang.String delim)Does the provided SQL script contain the specified delimiter?- Parameters:
script- the SQL scriptdelim- String delimiting each statement - typically a ';' character
-
runInitScript
public static void runInitScript(DatabaseDelegate databaseDelegate, java.lang.String initScriptPath)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, java.lang.String scriptPath, java.lang.String script) throws javax.script.ScriptException- Throws:
javax.script.ScriptException
-
executeDatabaseScript
public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, java.lang.String scriptPath, java.lang.String script, boolean continueOnError, boolean ignoreFailedDrops, java.lang.String commentPrefix, java.lang.String separator, java.lang.String blockCommentStartDelimiter, java.lang.String blockCommentEndDelimiter) throws javax.script.ScriptExceptionExecute 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 aDROPstatementcommentPrefix- 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; nevernullor emptyblockCommentEndDelimiter- the end block comment delimiter; nevernullor empty @throws ScriptException if an error occurred while executing the SQL script- Throws:
javax.script.ScriptException
-