Class 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, Nicolas Debeissat, Phillip Webb
    • 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​(@Nullable
                                          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
        separator - text separating each statement (typically a ';' or newline character)
        commentPrefix - the prefix that identifies SQL line comments (typically "--")
        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
      • splitSqlScript

        public static void splitSqlScript​(@Nullable
                                          EncodedResource resource,
                                          String script,
                                          String separator,
                                          String[] commentPrefixes,
                                          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 commentPrefixes will be honored: any text beginning with one of the comment prefixes 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
        separator - text separating each statement (typically a ';' or newline character)
        commentPrefixes - the prefixes that identify SQL line comments (typically "--")
        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
        Since:
        5.2
      • readScript

        public static String readScript​(LineNumberReader lineNumberReader,
                                        @Nullable
                                        String lineCommentPrefix,
                                        @Nullable
                                        String separator,
                                        @Nullable
                                        String blockCommentEndDelimiter)
                                 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
        lineCommentPrefix - the prefix that identifies comments in the SQL script (typically "--")
        separator - the statement separator in the SQL script (typically ";")
        blockCommentEndDelimiter - the end block comment delimiter
        Returns:
        a String containing the script lines
        Throws:
        IOException - in case of I/O errors
      • readScript

        public static String readScript​(LineNumberReader lineNumberReader,
                                        @Nullable
                                        String[] lineCommentPrefixes,
                                        @Nullable
                                        String separator,
                                        @Nullable
                                        String blockCommentEndDelimiter)
                                 throws IOException
        Read a script from the provided LineNumberReader, using the supplied comment prefixes and statement separator, and build a String containing the lines.

        Lines beginning with one of the comment prefixes 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
        lineCommentPrefixes - the prefixes that identify comments in the SQL script (typically "--")
        separator - the statement separator in the SQL script (typically ";")
        blockCommentEndDelimiter - the end block comment delimiter
        Returns:
        a String containing the script lines
        Throws:
        IOException - in case of I/O errors
        Since:
        5.2
      • containsSqlScriptDelimiters

        public static boolean containsSqlScriptDelimiters​(String script,
                                                          String delim)
        Does the provided SQL script contain the specified delimiter?
        Parameters:
        script - the SQL script
        delim - the string delimiting each statement - typically a ';' character