Example usage for org.springframework.jdbc.datasource.init ScriptParseException ScriptParseException

List of usage examples for org.springframework.jdbc.datasource.init ScriptParseException ScriptParseException

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.init ScriptParseException ScriptParseException.

Prototype

public ScriptParseException(String message, @Nullable EncodedResource resource) 

Source Link

Document

Construct a new ScriptParseException .

Usage

From source file:org.springframework.jdbc.datasource.init.ScriptUtils.java

/**
 * Split an SQL script into separate statements delimited by the provided
 * separator string. Each individual statement will be added to the provided
 * {@code List}.//from w  w  w  .j a  v a  2 s .  c  o  m
 * <p>Within the script, the provided {@code 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
 * {@code blockCommentStartDelimiter} and {@code 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.
 * @param resource the resource from which the script was read
 * @param script the SQL script; never {@code null} or empty
 * @param separator text separating each statement &mdash; typically a ';' or
 * newline character; never {@code null}
 * @param commentPrefix the prefix that identifies SQL line comments &mdash;
 * typically "--"; never {@code null} or empty
 * @param blockCommentStartDelimiter the <em>start</em> block comment delimiter;
 * never {@code null} or empty
 * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter;
 * never {@code null} or empty
 * @param statements the list that will contain the individual statements
 * @throws ScriptException if an error occurred while splitting the SQL script
 */
public static void splitSqlScript(@Nullable EncodedResource resource, String script, String separator,
        String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter,
        List<String> statements) throws ScriptException {

    Assert.hasText(script, "'script' must not be null or empty");
    Assert.notNull(separator, "'separator' must not be null");
    Assert.hasText(commentPrefix, "'commentPrefix' must not be null or empty");
    Assert.hasText(blockCommentStartDelimiter, "'blockCommentStartDelimiter' must not be null or empty");
    Assert.hasText(blockCommentEndDelimiter, "'blockCommentEndDelimiter' must not be null or empty");

    StringBuilder sb = new StringBuilder();
    boolean inSingleQuote = false;
    boolean inDoubleQuote = false;
    boolean inEscape = false;
    for (int i = 0; i < script.length(); i++) {
        char c = script.charAt(i);
        if (inEscape) {
            inEscape = false;
            sb.append(c);
            continue;
        }
        // MySQL style escapes
        if (c == '\\') {
            inEscape = true;
            sb.append(c);
            continue;
        }
        if (!inDoubleQuote && (c == '\'')) {
            inSingleQuote = !inSingleQuote;
        } else if (!inSingleQuote && (c == '"')) {
            inDoubleQuote = !inDoubleQuote;
        }
        if (!inSingleQuote && !inDoubleQuote) {
            if (script.startsWith(separator, i)) {
                // We've reached the end of the current statement
                if (sb.length() > 0) {
                    statements.add(sb.toString());
                    sb = new StringBuilder();
                }
                i += separator.length() - 1;
                continue;
            } else if (script.startsWith(commentPrefix, i)) {
                // Skip over any content from the start of the comment to the EOL
                int indexOfNextNewline = script.indexOf("\n", i);
                if (indexOfNextNewline > i) {
                    i = indexOfNextNewline;
                    continue;
                } else {
                    // If there's no EOL, we must be at the end of the script, so stop here.
                    break;
                }
            } else if (script.startsWith(blockCommentStartDelimiter, i)) {
                // Skip over any block comments
                int indexOfCommentEnd = script.indexOf(blockCommentEndDelimiter, i);
                if (indexOfCommentEnd > i) {
                    i = indexOfCommentEnd + blockCommentEndDelimiter.length() - 1;
                    continue;
                } else {
                    throw new ScriptParseException(
                            "Missing block comment end delimiter: " + blockCommentEndDelimiter, resource);
                }
            } else if (c == ' ' || c == '\n' || c == '\t') {
                // Avoid multiple adjacent whitespace characters
                if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
                    c = ' ';
                } else {
                    continue;
                }
            }
        }
        sb.append(c);
    }
    if (StringUtils.hasText(sb)) {
        statements.add(sb.toString());
    }
}