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 class  ScriptUtils.ScriptLoadException  
    static class  ScriptUtils.ScriptParseException  
    static class  ScriptUtils.ScriptStatementFailedException  
    static class  ScriptUtils.UncategorizedScriptException  
  • Field Summary

    Fields 
    Modifier and Type Field 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 line comments within SQL scripts.
    static java.lang.String DEFAULT_STATEMENT_SEPARATOR
    Default statement separator within SQL scripts.
    static java.lang.String FALLBACK_STATEMENT_SEPARATOR
    Fallback statement separator within SQL scripts.
  • Method Summary

    Modifier and Type Method Description
    static boolean containsSqlScriptDelimiters​(java.lang.String script, java.lang.String delim)
    Does the provided SQL script contain the specified delimiter?
    static void executeDatabaseScript​(DatabaseDelegate databaseDelegate, java.lang.String scriptPath, java.lang.String script)  
    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)
    Execute the given database script.
    static void runInitScript​(DatabaseDelegate databaseDelegate, java.lang.String initScriptPath)
    Load script from classpath and apply it to the given database
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 provided 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.

      Parameters:
      resource - the resource from which the script was read
      script - the SQL script; never null or empty
      separator - text separating each statement — typically a ';' or newline character; never null
      commentPrefix - the prefix that identifies SQL line comments — typically "--"; never null or empty
      blockCommentStartDelimiter - the start block comment delimiter; never null or empty
      blockCommentEndDelimiter - the end block comment delimiter; never null or empty
      statements - 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 script
      delim - 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 execution
      initScriptPath - 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.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 execution
      scriptPath - the resource (potentially associated with a specific encoding) to load the SQL script from
      script - the raw script content
      continueOnError - whether or not to continue without throwing an exception in the event of an error
      ignoreFailedDrops - whether or not to continue in the event of specifically an error on a DROP statement
      commentPrefix - 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 resort
      blockCommentStartDelimiter - the start block comment delimiter; never null or empty
      blockCommentEndDelimiter - the end block comment delimiter; never null or empty @throws ScriptException if an error occurred while executing the SQL script
      Throws:
      javax.script.ScriptException