| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SqlSplitter |
|
| 13.0;13 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | |
| 3 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | |
| 4 | * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | |
| 5 | * License. You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
| 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| 11 | * specific language governing permissions and limitations under the License. | |
| 12 | */ | |
| 13 | ||
| 14 | package org.kuali.db.jdbc; | |
| 15 | ||
| 16 | import org.apache.commons.lang.StringUtils; | |
| 17 | ||
| 18 | /** | |
| 19 | * Utility class to split a long sql batch script into single SQL commands. | |
| 20 | */ | |
| 21 | 0 | public class SqlSplitter { |
| 22 | /** | |
| 23 | * Value indicating the sql has no end-delimiter like i.e. semicolon | |
| 24 | */ | |
| 25 | public static final int NO_END = -1; | |
| 26 | ||
| 27 | /** | |
| 28 | * Check if the given sql line contains an end of command ';' Please note that we do <em>not</em> fully parse the | |
| 29 | * SQL, so if we get a malformed statement, we cannot detect it. | |
| 30 | * | |
| 31 | * @param line | |
| 32 | * to parse | |
| 33 | * @param delimiter | |
| 34 | * which should be used to split SQL commands | |
| 35 | * @return position after the end character if the given line contains the end of a SQL script, | |
| 36 | * {@value SqlSplitter#NO_END} if it doesn't contain an end char. | |
| 37 | */ | |
| 38 | public static int containsSqlEnd(String line, String delimiter) { | |
| 39 | // / * * / comments | |
| 40 | 0 | boolean isComment = false; |
| 41 | ||
| 42 | 0 | boolean isAlphaDelimiter = StringUtils.isAlpha(delimiter); |
| 43 | ||
| 44 | 0 | if (line == null || line.length() == 0) { |
| 45 | 0 | return NO_END; |
| 46 | } | |
| 47 | ||
| 48 | 0 | int pos = 0; |
| 49 | ||
| 50 | do { | |
| 51 | 0 | if (isComment) { |
| 52 | 0 | if (line.startsWith("*/", pos)) { |
| 53 | 0 | isComment = false; |
| 54 | } else { | |
| 55 | 0 | pos++; |
| 56 | 0 | continue; |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | 0 | if (line.startsWith("/*", pos)) { |
| 61 | 0 | isComment = true; |
| 62 | 0 | pos += 2; |
| 63 | 0 | continue; |
| 64 | } | |
| 65 | ||
| 66 | 0 | if (line.startsWith("--", pos)) { |
| 67 | 0 | return NO_END; |
| 68 | } | |
| 69 | ||
| 70 | 0 | if (line.startsWith("'", pos) || line.startsWith("\"", pos)) { |
| 71 | 0 | String quoteChar = "" + line.charAt(pos); |
| 72 | 0 | String quoteEscape = "\\" + quoteChar; |
| 73 | 0 | pos++; |
| 74 | ||
| 75 | 0 | if (line.length() <= pos) { |
| 76 | 0 | return NO_END; |
| 77 | } | |
| 78 | ||
| 79 | do { | |
| 80 | 0 | if (line.startsWith(quoteEscape, pos)) { |
| 81 | 0 | pos += 2; |
| 82 | } | |
| 83 | 0 | } while (!line.startsWith(quoteChar, pos++)); |
| 84 | ||
| 85 | 0 | continue; |
| 86 | } | |
| 87 | ||
| 88 | 0 | if (line.startsWith(delimiter, pos)) { |
| 89 | 0 | if (isAlphaDelimiter) { |
| 90 | // check if delimiter is at start or end of line, surrounded | |
| 91 | // by non-alpha character | |
| 92 | 0 | if ((pos == 0 || !isAlpha(line.charAt(pos - 1))) |
| 93 | && (line.length() == pos + delimiter.length() || !isAlpha(line.charAt(pos | |
| 94 | + delimiter.length())))) { | |
| 95 | 0 | return pos + delimiter.length(); |
| 96 | } | |
| 97 | } else { | |
| 98 | 0 | return pos + delimiter.length(); |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | 0 | pos++; |
| 103 | ||
| 104 | 0 | } while (line.length() >= pos); |
| 105 | ||
| 106 | 0 | return NO_END; |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * @param c | |
| 111 | * the char to check | |
| 112 | * @return <code>true</code> if the given character is either a lower or an upperchase alphanumerical character | |
| 113 | */ | |
| 114 | private static boolean isAlpha(char c) { | |
| 115 | 0 | return Character.isUpperCase(c) || Character.isLowerCase(c); |
| 116 | } | |
| 117 | ||
| 118 | } |