1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.db;
17
18 import org.apache.commons.lang.StringUtils;
19
20
21
22
23 public class SqlSplitter {
24
25
26
27 public static final int NO_END = -1;
28
29
30
31
32
33
34
35
36
37
38
39
40 public static int containsSqlEnd(String line, String delimiter) {
41
42 boolean isComment = false;
43
44 boolean isAlphaDelimiter = StringUtils.isAlpha(delimiter);
45
46 if (line == null || line.length() == 0) {
47 return NO_END;
48 }
49
50 int pos = 0;
51
52 do {
53 if (isComment) {
54 if (line.startsWith("*/", pos)) {
55 isComment = false;
56 } else {
57 pos++;
58 continue;
59 }
60 }
61
62 if (line.startsWith("/*", pos)) {
63 isComment = true;
64 pos += 2;
65 continue;
66 }
67
68 if (line.startsWith("--", pos)) {
69 return NO_END;
70 }
71
72 if (line.startsWith("'", pos) || line.startsWith("\"", pos)) {
73 String quoteChar = "" + line.charAt(pos);
74 String quoteEscape = "\\" + quoteChar;
75 pos++;
76
77 if (line.length() <= pos) {
78 return NO_END;
79 }
80
81 do {
82 if (line.startsWith(quoteEscape, pos)) {
83 pos += 2;
84 }
85 } while (!line.startsWith(quoteChar, pos++));
86
87 continue;
88 }
89
90 if (line.startsWith(delimiter, pos)) {
91 if (isAlphaDelimiter) {
92
93
94 if ((pos == 0 || !isAlpha(line.charAt(pos - 1))) && (line.length() == pos + delimiter.length() || !isAlpha(line.charAt(pos + delimiter.length())))) {
95 return pos + delimiter.length();
96 }
97 } else {
98 return pos + delimiter.length();
99 }
100 }
101
102 pos++;
103
104 } while (line.length() >= pos);
105
106 return NO_END;
107 }
108
109
110
111
112
113
114 private static boolean isAlpha(char c) {
115 return Character.isUpperCase(c) || Character.isLowerCase(c);
116 }
117
118 }