Skip to content

Commit 9ff6891

Browse files
committed
Server:预编译模式下支持@having:"function0(arg0,arg1,...)operator0 value0;function1(arg0,arg1,...)operator1 value1..."
1 parent 107b524 commit 9ff6891

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public AbstractSQLConfig setGroup(String group) {
255255
@JSONField(serialize = false)
256256
public String getGroupString() {
257257
//TODO 加上子表的group
258-
258+
259259
group = StringUtil.getTrimedString(group);
260260
if (group.isEmpty()) {
261261
return "";
@@ -291,16 +291,68 @@ public AbstractSQLConfig setHaving(String having) {
291291
this.having = having;
292292
return this;
293293
}
294+
/**
295+
* @return HAVING conditoin0 AND condition1 OR condition2 ...
296+
*/
294297
@JSONField(serialize = false)
295298
public String getHavingString() {
296299
having = StringUtil.getTrimedString(having);
297300
if(having.isEmpty()) {
298301
return "";
299302
}
300-
if (isPrepared()) {
301-
throw new UnsupportedOperationException("字符串 " + having + " 不合法!预编译模式下不允许传 @having:\"condition\" !");
303+
304+
String[] keys = StringUtil.split(having, ";");
305+
if (keys == null || keys.length <= 0) {
306+
return "";
307+
}
308+
309+
String expression;
310+
String method;
311+
//暂时不允许 String prefix;
312+
String suffix;
313+
314+
//fun0(arg0,arg1,...);fun1(arg0,arg1,...)
315+
for (int i = 0; i < keys.length; i++) {
316+
317+
//fun(arg0,arg1,...)
318+
expression = keys[i];
319+
320+
//TODO 支持 maxId>=100 这种没括号的
321+
int start = expression.indexOf("(");
322+
int end = expression.indexOf(")");
323+
if (start >= end) {
324+
throw new IllegalArgumentException("字符 " + expression + " 不合法!@having:value 中 value 里的 SQL函数必须为 function(arg0,arg1,...) 这种格式!");
325+
}
326+
327+
method = expression.substring(0, start);
328+
329+
if (StringUtil.isName(method) == false) {
330+
throw new IllegalArgumentException("字符 " + method + " 不合法!@having:\"function0(...)condition0;function1(...)condition1...\""
331+
+ " 中SQL函数名 function 必须符合正则表达式 ^[0-9a-zA-Z_]+$ !");
332+
}
333+
334+
suffix = expression.substring(end + 1, expression.length());
335+
336+
if (isPrepared() && PATTERN_RANGE.matcher((String) suffix).matches() == false) {
337+
throw new UnsupportedOperationException("字符串 " + suffix + " 不合法!预编译模式下 @having:\"function0(...)condition0;function1(...)condition1...\""
338+
+ " 中 condition 必须符合正则表达式 ^[0-9%!=<>,]+$ !不允许空格!");
339+
}
340+
341+
String[] ckeys = StringUtil.split(expression.substring(start + 1, end));
342+
343+
for (int j = 0; j < ckeys.length; j++) {
344+
345+
if (isPrepared() && StringUtil.isName(ckeys[j]) == false) {
346+
throw new IllegalArgumentException("@having:'function0(arg0,arg1,...);function1(arg0,arg1,...)' 中所有 arg 都必须是1个单词!并且不要有空格!");
347+
}
348+
349+
ckeys[j] = getKey(ckeys[j]);
350+
}
351+
352+
keys[i] = method + "(" + StringUtil.getString(ckeys) + ")" + suffix;
302353
}
303-
return " HAVING " + having;
354+
355+
return " HAVING " + StringUtil.getString(keys, AND); //TODO 支持 OR, NOT 参考 @combine:"&key0,|key1,!key2"
304356
}
305357

306358
@Override
@@ -329,7 +381,7 @@ public String getOrderString() {
329381
if (order.contains("-")) {
330382
order = order.replaceAll("-", " DESC ");
331383
}
332-
384+
333385
//TODO column, order, group 都改用 List<String> 存储!!!,并且每个字段都要加 Table. 前缀!
334386
String[] keys = StringUtil.split(order);
335387
if (keys == null || keys.length <= 0) {
@@ -358,7 +410,7 @@ public String getOrderString() {
358410
}
359411
keys[i] = getKey(origin) + sort;
360412
}
361-
413+
362414
return " ORDER BY " + StringUtil.getString(keys);
363415
}
364416

0 commit comments

Comments
 (0)