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