Class ScriptUtils

java.lang.Object
org.testcontainers.ext.ScriptUtils

public abstract class ScriptUtils extends 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.
  • Field Details

    • DEFAULT_STATEMENT_SEPARATOR

      public static final String DEFAULT_STATEMENT_SEPARATOR
      Default statement separator within SQL scripts.
      See Also:
    • FALLBACK_STATEMENT_SEPARATOR

      public static final String 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

      public static final String DEFAULT_COMMENT_PREFIX
      Default prefix for line comments within SQL scripts.
      See Also:
    • DEFAULT_BLOCK_COMMENT_START_DELIMITER

      public static final String DEFAULT_BLOCK_COMMENT_START_DELIMITER
      Default start delimiter for block comments within SQL scripts.
      See Also:
    • DEFAULT_BLOCK_COMMENT_END_DELIMITER

      public static final String 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 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(String script, String delim)
      Does the provided SQL script contain the specified delimiter?
      Parameters:
      script - the SQL script
      delim - 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 script
      delim - String delimiting each statement - typically a ';' character
      commentPrefix - the prefix that identifies comments in the SQL script, typically "--"
      blockCommentStartDelimiter - block comment start delimiter
      blockCommentEndDelimiter - block comment end delimiter
    • runInitScript

      public static void runInitScript(DatabaseDelegate databaseDelegate, 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, 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 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:
      ScriptException