Java String Split by Delimiter splitSqlScript(String script, char delim, List statements)

Here you can find the source of splitSqlScript(String script, char delim, List statements)

Description

Split an SQL script into separate statements delimited with the provided delimiter character.

License

Apache License

Parameter

Parameter Description
script the SQL script
delim character delimiting each statement - typically a ';' character
statements the List that will contain the individual statements

Declaration

public static void splitSqlScript(String script, char delim,
        List<String> statements) 

Method Source Code

//package com.java2s;
/*/*from   www.j  a  v a  2  s  .  co  m*/
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Split an SQL script into separate statements delimited with the provided
     * delimiter character. Each individual statement will be added to the
     * provided <code>List</code>.
     * 
     * @param script the SQL script
     * @param delim character delimiting each statement - typically a ';'
     * character
     * @param statements the List that will contain the individual statements
     */
    public static void splitSqlScript(String script, char delim,
            List<String> statements) {
        StringBuilder sb = new StringBuilder();
        boolean inLiteral = false;
        char[] content = script.toCharArray();
        for (int i = 0; i < script.length(); i++) {
            if (content[i] == '\'') {
                inLiteral = !inLiteral;
            }
            if (content[i] == delim && !inLiteral) {
                if (sb.length() > 0) {
                    statements.add(sb.toString());
                    sb = new StringBuilder();
                }
            } else {
                sb.append(content[i]);
            }
        }
        if (sb.length() > 0) {
            statements.add(sb.toString());
        }
    }
}

Related

  1. splitNestedString(String params, String delimStr, int numLeft, int numRight)
  2. splitNoCoalesce(String s, char delimiter)
  3. splitOnEntireString(String target, String delimiter)
  4. splitSimple(String delimiter, String str)
  5. splitSimpleLimit(String str, String delimiter, int limitSize)
  6. splitStr(String str, char delimiter, boolean trim)
  7. splitString(CharSequence s, char delim)
  8. splitString(final String values, final char delimiter)
  9. splitString(String data, String delimiter)