-
Notifications
You must be signed in to change notification settings - Fork 2
/
SqlCommentParser.java
199 lines (183 loc) · 6.42 KB
/
SqlCommentParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package myself;
/**
* SqlCommentParser is used to parse the comment position in sql
*
* SqlCommentParser thinks that a sql string is made up of three types of strings.
* One is the quote type, the other is the comment type, and the other is the ID type
*
* E.g:
* select * from a where column_b='this is not comment'-- this is comment
* in this sql,
* the quote type of strings is 【'this is not comment'】
* the comment type of strings is 【-- this is comment】
* the ID type of strings is 【select * from a where column_b=】
*
* the quote type is consist a pair of '、"、`, and the quote internal string can be any string
* the comment type is consist of --、/*
* the ID type is a strings that do not meet the above two types
*
* note:
* (1)SqlCommentParser cannot identify whether the string conforms to the SQL specification,
* (2)If you want quote、comment to support more, you should add more case in the method of nextComment
*
*/
public class SqlCommentParser {
private final String sql;
private int pos;
private int start = 0;
public SqlCommentParser(String sql) {
this.sql = sql;
this.pos = 0;
}
public String removeCommentSql() {
StringBuilder newSQL = new StringBuilder();
int startIndex = 0;
while (true) {
Comment comment = this.nextComment();
// the sql is parse over
if (comment == null) {
// process the sql without comment case
if (startIndex != sql.length()) {
newSQL.append(sql, startIndex, sql.length());
}
break;
} else {
newSQL.append(sql, startIndex, comment.startIndex);
startIndex = comment.endIndex;
}
}
return newSQL.toString();
}
/**
* Get next comment of sql
*
* it only support two comment modes, one is -- ,the other is /*
* @return null, only if sql parser over;
*/
public Comment nextComment() {
while (pos < sql.length()) {
char c = sql.charAt(pos);
Integer startIndex = null;
switch (c) {
// ignore the type of quote
case '\'':
start = pos;
++pos;
while (pos < sql.length()) {
c = sql.charAt(pos);
++pos;
if (c == '\'') {
break;
}
}
break;
case '`':
start = pos;
++pos;
while (pos < sql.length()) {
c = sql.charAt(pos);
++pos;
if (c == '`') {
break;
}
}
break;
case '\"':
start = pos;
++pos;
while (pos < sql.length()) {
c = sql.charAt(pos);
++pos;
if (c == '\"') {
break;
}
}
break;
// parse the type of comment
case '/':
// possible start of '/*'
if (pos + 1 < sql.length()) {
char c1 = sql.charAt(pos + 1);
if (c1 == '*') {
startIndex = pos;
int end = sql.indexOf("*/", pos + 2);
if (end < 0) {
end = sql.length();
} else {
end += "*/".length();
}
pos = end;
Integer endIndex = pos;
return new Comment(startIndex, endIndex);
}
}
case '-':
// possible start of '--' comment
if (c == '-' && pos + 1 < sql.length() && sql.charAt(pos + 1) == '-') {
startIndex = pos;
pos = indexOfLineEnd(sql, pos + 2);
Integer endIndex = pos;
return new Comment(startIndex, endIndex);
}
default:
if (isOpenQuote(c)) {
break;
} else {
// parse the type of ID
++pos;
loop:
while (pos < sql.length()) {
c = sql.charAt(pos);
switch (c) {
case '\'':
case '`':
case '\"':
case '/':
break loop;
case '-':
// possible start of '--' comment
if (c == '-' && pos + 1 < sql.length() && sql.charAt(pos + 1) == '-') {
break loop;
}
default:
++pos;
}
}
}
}
}
return null;
}
private boolean isOpenQuote(char character) {
if (character == '\"') {
return true;
} else if (character == '`') {
return true;
} else if (character == '\'') {
return true;
}
return false;
}
private int indexOfLineEnd(String sql, int i) {
int length = sql.length();
while (i < length) {
char c = sql.charAt(i);
switch (c) {
case '\r':
case '\n':
return i;
default:
++i;
}
}
return i;
}
public static class Comment {
private Integer startIndex;
private Integer endIndex;
Comment(Integer startIndex, Integer endIndex) {
this.startIndex = startIndex;
this.endIndex = endIndex;
}
}
}