Here you can find the source of splitSqlScript(String script, char delim, List
Parameter | Description |
---|---|
script | the SQL script |
delim | character delimiting each statement - typically a ';' character |
statements | the List that will contain the individual statements |
public static void splitSqlScript(String script, char delim, List<String> statements)
//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()); } } }