Skip to content

Commit 76cb89f

Browse files
committed
fix(crud): 修复TermExpressionParser解析逻辑,支持更多条件运算符和空格处理
1 parent 314abfc commit 76cb89f

File tree

2 files changed

+249
-46
lines changed

2 files changed

+249
-46
lines changed

hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/TermExpressionParser.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hswebframework.ezorm.core.param.TermType;
1010

1111
import java.net.URLDecoder;
12+
import java.nio.charset.StandardCharsets;
1213
import java.util.*;
1314
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
@@ -82,7 +83,7 @@ public static List<Term> parse(Map<String, Object> map) {
8283
@SneakyThrows
8384
public static List<Term> parse(String expression) {
8485
try {
85-
expression = URLDecoder.decode(expression, "utf-8");
86+
expression = URLDecoder.decode(expression, StandardCharsets.UTF_8);
8687
} catch (Throwable ignore) {
8788

8889
}
@@ -91,15 +92,13 @@ public static List<Term> parse(String expression) {
9192
NestConditional<?> nest = null;
9293

9394
// 字符容器
94-
char[] buf = new char[128];
95-
// 记录词项的长度, Arrays.copyOf使用
96-
byte len = 0;
95+
StringBuilder buf = new StringBuilder(128);
9796
// 空格数量?
9897
byte spaceLen = 0;
9998
// 当前列
100-
char[] currentColumn = null;
99+
String currentColumn = null;
101100
// 当前列对应的值
102-
char[] currentValue = null;
101+
String currentValue = null;
103102
// 当前条件类型 eq btw in ...
104103
String currentTermType = null;
105104
// 当前链接类型 and / or
@@ -124,24 +123,24 @@ public static List<Term> parse(String expression) {
124123
nest = (nest == null ?
125124
(currentType.equals("or") ? conditional.orNest() : conditional.nest()) :
126125
(currentType.equals("or") ? nest.orNest() : nest.nest()));
127-
len = 0;
126+
buf.setLength(0);
128127
continue;
129128
} else if (c == ')') {
130129
if (nest == null) {
131130
continue;
132131
}
133132
if (null != currentColumn) {
134-
currentValue = Arrays.copyOf(buf, len);
135-
nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
133+
currentValue = buf.toString();
134+
nest.accept(currentColumn, convertTermType(currentTermType), currentValue);
136135
currentColumn = null;
137136
currentTermType = null;
138137
}
139138
Object end = nest.end();
140-
nest = end instanceof NestConditional ? ((NestConditional) end) : null;
141-
len = 0;
139+
nest = end instanceof NestConditional ? ((NestConditional<?>) end) : null;
140+
buf.setLength(0);
142141
spaceLen++;
143142
continue;
144-
} else if (c == '=' || c == '>' || c == '<') {
143+
} else if (c == '=' || c == '>' || c == '<' || c == '!') {
145144
if (currentTermType != null) {
146145
currentTermType += String.valueOf(c);
147146
//spaceLen--;
@@ -150,52 +149,52 @@ public static List<Term> parse(String expression) {
150149
}
151150

152151
if (currentColumn == null) {
153-
currentColumn = Arrays.copyOf(buf, len);
152+
currentColumn = buf.toString();
154153
}
155154
spaceLen++;
156-
len = 0;
155+
buf.setLength(0);
157156
continue;
158157
} else if (c == ' ') {
159-
if (len == 0) {
158+
if (buf.isEmpty()) {
160159
continue;
161160
}
162161
if (quotationMarks != 0) {
163162
// 如果当前字符是空格,并且前面迭代时碰到过单/双引号, 不处理并且添加到buf中
164-
buf[len++] = c;
163+
buf.append(c);
165164
continue;
166165
}
167166
spaceLen++;
168167
if (currentColumn == null && (spaceLen == 1 || spaceLen % 5 == 0)) {
169-
currentColumn = Arrays.copyOf(buf, len);
170-
len = 0;
168+
currentColumn = buf.toString();
169+
buf.setLength(0);
171170
continue;
172171
}
173172
if (null != currentColumn) {
174173
if (null == currentTermType) {
175-
currentTermType = new String(Arrays.copyOf(buf, len));
176-
len = 0;
174+
currentTermType = buf.toString();
175+
buf.setLength(0);
177176
continue;
178177
}
179-
currentValue = Arrays.copyOf(buf, len);
178+
currentValue = buf.toString();
180179
if (nest != null) {
181-
nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
180+
nest.accept(currentColumn, convertTermType(currentTermType), currentValue);
182181
} else {
183-
conditional.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
182+
conditional.accept(currentColumn, convertTermType(currentTermType), currentValue);
184183
}
185184
currentColumn = null;
186185
currentTermType = null;
187-
len = 0;
186+
buf.setLength(0);
188187
continue;
189-
} else if (len == 2 || len == 3) {
190-
String type = new String(Arrays.copyOf(buf, len));
188+
} else if (buf.length() == 2 || buf.length() == 3) {
189+
String type = buf.toString();
191190
if (type.equalsIgnoreCase("or")) {
192191
currentType = "or";
193192
if (nest != null) {
194193
nest.or();
195194
} else {
196195
conditional.or();
197196
}
198-
len = 0;
197+
buf.setLength(0);
199198
continue;
200199
} else if (type.equalsIgnoreCase("and")) {
201200
currentType = "and";
@@ -204,29 +203,29 @@ public static List<Term> parse(String expression) {
204203
} else {
205204
conditional.and();
206205
}
207-
len = 0;
206+
buf.setLength(0);
208207
continue;
209208
} else {
210-
currentColumn = Arrays.copyOf(buf, len);
211-
len = 0;
209+
currentColumn = buf.toString();
210+
buf.setLength(0);
212211
spaceLen++;
213212
}
214213
} else {
215-
currentColumn = Arrays.copyOf(buf, len);
216-
len = 0;
214+
currentColumn = buf.toString();
215+
buf.setLength(0);
217216
spaceLen++;
218217
}
219218
continue;
220219
}
221220

222-
buf[len++] = c;
221+
buf.append(c);
223222
}
224223
if (null != currentColumn) {
225-
currentValue = Arrays.copyOf(buf, len);
224+
currentValue = buf.toString();
226225
if (nest != null) {
227-
nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
226+
nest.accept(currentColumn, convertTermType(currentTermType), currentValue);
228227
} else {
229-
conditional.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
228+
conditional.accept(currentColumn, convertTermType(currentTermType), currentValue);
230229
}
231230
}
232231
return conditional.getParam().getTerms();
@@ -270,6 +269,8 @@ private static String convertTermType(String termType) {
270269
return TermType.gte;
271270
case "<=":
272271
return TermType.lte;
272+
case "!=":
273+
return TermType.not;
273274
default:
274275
return termType;
275276
}

0 commit comments

Comments
 (0)