java.lang.Object
org.springframework.data.cassandra.core.cql.session.init.ScriptUtils

public abstract class ScriptUtils extends Object
Generic utility methods for working with CQL scripts.

Mainly for internal use within the framework.

Since:
3.0
Author:
Mark Paluch
  • Field Details

    • DEFAULT_STATEMENT_SEPARATOR

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

      public static final String FALLBACK_STATEMENT_SEPARATOR
      Fallback statement separator within CQL scripts: "\n".

      Used if neither a custom separator nor the DEFAULT_STATEMENT_SEPARATOR is present in a given script.

      See Also:
    • EOF_STATEMENT_SEPARATOR

      public static final String EOF_STATEMENT_SEPARATOR
      End of file (EOF) CQL statement separator: "^^^ END OF SCRIPT ^^^".

      This value may be supplied as the separator to executeCqlScript(CqlSession, EncodedResource, boolean, boolean, String, String, String, String) to denote that an CQL script contains a single statement (potentially spanning multiple lines) with no explicit statement separator. Note that such a script should not actually contain this value; it is merely a virtual statement separator.

      See Also:
    • DEFAULT_COMMENT_PREFIX

      public static final String DEFAULT_COMMENT_PREFIX
      Default prefix for single-line comments within CQL scripts: "--".
      See Also:
    • DEFAULT_COMMENT_PREFIXES

      public static final String[] DEFAULT_COMMENT_PREFIXES
      Default prefixes for single-line comments within CQL scripts: ["--"].
      Since:
      5.2
    • DEFAULT_BLOCK_COMMENT_START_DELIMITER

      public static final String DEFAULT_BLOCK_COMMENT_START_DELIMITER
      Default start delimiter for block comments within CQL scripts: "/*".
      See Also:
    • DEFAULT_BLOCK_COMMENT_END_DELIMITER

      public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER
      Default end delimiter for block comments within CQL scripts: "*/".
      See Also:
  • Constructor Details

    • ScriptUtils

      public ScriptUtils()
  • Method Details

    • splitCqlScript

      public static void splitCqlScript(String script, char separator, List<String> statements) throws ScriptException
      Split an CQL 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 CQL 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 CQL script.
      See Also:
    • splitCqlScript

      public static void splitCqlScript(String script, String separator, List<String> statements) throws ScriptException
      Split an CQL 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 CQL 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 CQL script.
      See Also:
    • splitCqlScript

      public static void splitCqlScript(@Nullable EncodedResource resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) throws ScriptException
      Split an CQL 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 CQL script.
      separator - text separating each statement (typically a ';' or newline character).
      commentPrefix - the prefix that identifies CQL 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 CQL script
    • splitCqlScript

      public static void splitCqlScript(@Nullable EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) throws ScriptException
      Split an CQL 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 CQL script.
      separator - text separating each statement (typically a ';' or newline character).
      commentPrefixes - the prefixes that identify CQL 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 CQL script
    • 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 CQL script (typically "--").
      separator - the statement separator in the CQL 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 CQL script (typically "--").
      separator - the statement separator in the CQL script (typically ";").
      blockCommentEndDelimiter - the end block comment delimiter.
      Returns:
      a String containing the script lines
      Throws:
      IOException - in case of I/O errors
    • containsCqlScriptDelimiters

      public static boolean containsCqlScriptDelimiters(String script, String separator)
      Does the provided CQL script contain the specified delimiter?
      Parameters:
      script - the CQL script.
      separator - the string delimiting each statement - typically a ';' character.
    • executeCqlScript

      public static void executeCqlScript(com.datastax.oss.driver.api.core.CqlSession session, Resource resource) throws ScriptException
      Execute the given CQL script using default settings for statement separators, comment delimiters, and exception handling flags.

      Statement separators and comments will be removed before executing individual statements within the supplied script.

      Parameters:
      session - the CQL CqlSession to use to execute the script; already configured and ready to use.
      resource - the resource to load the CQL script from; encoded with the current platform's default encoding.
      Throws:
      ScriptException - if an error occurred while executing the CQL script
      See Also:
    • executeCqlScript

      public static void executeCqlScript(com.datastax.oss.driver.api.core.CqlSession session, EncodedResource resource) throws ScriptException
      Execute the given CQL script using default settings for statement separators, comment delimiters, and exception handling flags.

      Statement separators and comments will be removed before executing individual statements within the supplied script.

      Parameters:
      session - the CQL CqlSession to use to execute the script; already configured and ready to use.
      resource - the resource (potentially associated with a specific encoding) to load the CQL script from.
      Throws:
      ScriptException - if an error occurred while executing the CQL script
      See Also:
    • executeCqlScript

      public static void executeCqlScript(com.datastax.oss.driver.api.core.CqlSession session, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException
      Execute the given CQL script.

      Statement separators and comments will be removed before executing individual statements within the supplied script.

      Parameters:
      session - the CQL CqlSession to use to execute the script; already configured and ready to use.
      resource - the resource (potentially associated with a specific encoding) to load the CQL 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 single-line comments in the CQL script (typically "--")-
      separator - the script statement separator; defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to "^^^ END OF SCRIPT ^^^" to signal that the script contains a single statement without a separator.
      blockCommentStartDelimiter - the start block comment delimiter
      blockCommentEndDelimiter - the end block comment delimiter
      Throws:
      ScriptException - if an error occurred while executing the CQL script
      See Also:
    • executeCqlScript

      public static void executeCqlScript(com.datastax.oss.driver.api.core.CqlSession session, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException
      Execute the given CQL script.

      Statement separators and comments will be removed before executing individual statements within the supplied script.

      Parameters:
      session - the CQL CqlSession to use to execute the script; already configured and ready to use.
      resource - the resource (potentially associated with a specific encoding) to load the CQL 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.
      commentPrefixes - the prefixes that identify single-line comments in the CQL script (typically "--").
      separator - the script statement separator; defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to "^^^ END OF SCRIPT ^^^" to signal that the script contains a single statement without a separator.
      blockCommentStartDelimiter - the start block comment delimiter
      blockCommentEndDelimiter - the end block comment delimiter
      Throws:
      ScriptException - if an error occurred while executing the CQL script
      See Also: