org.springframework.jdbc.datasource.init

## Class 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.
  Since:
    4.0.3
  Author:
    Thomas Risberg, Sam Brannen, Juergen Hoeller, Keith Donald, Dave Syer, Chris Beams, Oliver Gierke, Chris Baldwin

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `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 line comments within SQL scripts.
    `static String` | `DEFAULT_STATEMENT_SEPARATOR` Default statement separator within SQL scripts.
    `static String` | `FALLBACK_STATEMENT_SEPARATOR` Fallback statement separator within SQL scripts.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `containsSqlScriptDelimiters(String script, String delim)` Does the provided SQL script contain the specified delimiter?
    `static void` | `executeSqlScript(Connection connection, EncodedResource resource)` Execute the given SQL script using default settings for separator 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 void` | `executeSqlScript(Connection connection, Resource resource)` Execute the given SQL script using default settings for separator separators, comment delimiters, and exception handling flags.
    `static String` | `readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator)` Read a script from the provided `LineNumberReader`, using the supplied comment prefix and statement separator, and build a `String` containing the lines.
    `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.
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_STATEMENT_SEPARATOR

          public static final String DEFAULT_STATEMENT_SEPARATOR

      Default statement separator within SQL scripts.
      See Also:
        Constant Field Values

    - #### 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:
        Constant Field Values

    - #### DEFAULT_COMMENT_PREFIX

          public static final String DEFAULT_COMMENT_PREFIX

      Default prefix for line comments within SQL scripts.
      See Also:
        Constant Field Values

    - #### 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:
        Constant Field Values

    - #### 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:
        Constant Field Values

  - ### Method Detail

    - #### 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 provided `List`.

      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.

      Parameters:
        `script` - the SQL script
        `separator` - character separating each statement — typically a ';'
        `statements` - the list that will contain the individual statements
      Throws:
        `ScriptException` - if an error occurred while splitting the SQL script
      See Also:
        `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 provided `List`.

      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.

      Parameters:
        `script` - the SQL script
        `separator` - text separating each statement — typically a ';' or newline character
        `statements` - the list that will contain the individual statements
      Throws:
        `ScriptException` - if an error occurred while splitting the SQL script
      See Also:
        `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 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
      Throws:
        `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 provided `LineNumberReader`, using the supplied comment prefix and statement separator, and build a `String` 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.

      Parameters:
        `lineNumberReader` - the `LineNumberReader` containing the script to be processed
        `commentPrefix` - the prefix that identifies comments in the SQL script — typically "--"
        `separator` - the statement separator in the SQL script — typically ";"
      Returns:
        a `String` containing the script lines
      Throws:
        `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?
      Parameters:
        `script` - the SQL script
        `delim` - 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 separator separators, comment delimiters, and exception handling flags.

      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:
        `connection` - the JDBC connection to use to execute the script; already configured and ready to use
        `resource` - the resource to load the SQL script from; encoded with the current platform's default encoding
      Throws:
        `ScriptException` - if an error occurred while executing the SQL script
      See Also:
        `executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)`, `DEFAULT_COMMENT_PREFIX`, `DEFAULT_STATEMENT_SEPARATOR`, `DEFAULT_BLOCK_COMMENT_START_DELIMITER`, `DEFAULT_BLOCK_COMMENT_END_DELIMITER`

    - #### executeSqlScript

          public static void executeSqlScript(Connection connection,
                                              EncodedResource resource)
                                       throws ScriptException

      Execute the given SQL script using default settings for separator separators, comment delimiters, and exception handling flags.

      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:
        `connection` - the JDBC connection to use to execute the script; already configured and ready to use
        `resource` - the resource (potentially associated with a specific encoding) to load the SQL script from
      Throws:
        `ScriptException` - if an error occurred while executing the SQL script
      See Also:
        `executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)`, `DEFAULT_COMMENT_PREFIX`, `DEFAULT_STATEMENT_SEPARATOR`, `DEFAULT_BLOCK_COMMENT_START_DELIMITER`, `DEFAULT_BLOCK_COMMENT_END_DELIMITER`

    - #### 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.

      Do not use this method to execute DDL if you expect rollback.

      Parameters:
        `connection` - the JDBC connection to use to execute the script; already configured and ready to use
        `resource` - the resource (potentially associated with a specific encoding) to load the SQL script from
        `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