Skip to content

Commit cc28f65

Browse files
APIJSON @combine模版, 通过参数生成sql where条件
使用说明参见: Tencent#509
1 parent 86a68d2 commit cc28f65

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,21 +2801,29 @@ protected String parseCombineExpression(RequestMethod method, String quote, Stri
28012801
throw new IllegalArgumentException(errPrefix + " 中字符 '" + s + "' 不合法!"
28022802
+ "其中 key 数量 " + allCount + " 已超过最大值,必须在条件键值对数量 0-" + maxCombineCount + " 内!");
28032803
}
2804+
2805+
String column = key;
2806+
int keyIndex = column.indexOf(":");
2807+
column = keyIndex > 0 ? column.substring(0, keyIndex) : column;
2808+
Object value = conditionMap.get(column);
2809+
String wi = "";
2810+
if (value == null && conditionMap.containsKey(column) == false) { // 兼容@null
2811+
isNot = false; // 以占位表达式为准
2812+
size++; // 兼容 key 数量判断
2813+
wi = keyIndex > 0 ? key.substring(keyIndex + 1) : "";
2814+
if (StringUtil.isEmpty(wi)) {
2815+
throw new IllegalArgumentException(errPrefix + " 中字符 '" + key + "' 对应的条件键值对 " + column + ":value 不存在!");
2816+
}
2817+
} else {
2818+
wi = isHaving ? getHavingItem(quote, table, alias, column, (String) value, containRaw) : getWhereItem(column, value, method, verifyName);
2819+
}
2820+
28042821
if (1.0f*allCount/size > maxCombineRatio && maxCombineRatio > 0) {
28052822
throw new IllegalArgumentException(errPrefix + " 中字符 '" + s + "' 不合法!"
28062823
+ "其中 key 数量 " + allCount + " / 条件键值对数量 " + size + " = " + (1.0f*allCount/size)
28072824
+ " 已超过 最大倍数,必须在条件键值对数量 0-" + maxCombineRatio + " 倍内!");
28082825
}
2809-
2810-
String column = key;
2811-
2812-
Object value = conditionMap.get(column);
2813-
if (value == null) {
2814-
throw new IllegalArgumentException(errPrefix + " 中字符 '" + key
2815-
+ "' 对应的条件键值对 " + column + ":value 不存在!");
2816-
}
2817-
2818-
String wi = isHaving ? getHavingItem(quote, table, alias, column, (String) value, containRaw) : getWhereItem(column, value, method, verifyName);
2826+
28192827
if (StringUtil.isEmpty(wi, true)) { // 转成 1=1 ?
28202828
throw new IllegalArgumentException(errPrefix + " 中字符 '" + key
28212829
+ "' 对应的 " + column + ":value 不是有效条件键值对!");
@@ -3230,6 +3238,9 @@ protected String getWhereItem(String key, Object value, RequestMethod method, bo
32303238
throw new IllegalArgumentException(TAG + ".getWhereItem: 字符 " + key + " 不合法!");
32313239
}
32323240

3241+
if (value == null) {
3242+
return null;
3243+
}
32333244

32343245
int keyType;
32353246
if (key.endsWith("$")) {
@@ -5652,7 +5663,7 @@ private static boolean keyInCombineExpr(String combineExpr, String key) {
56525663

56535664
char left = index <= 0 ? ' ' : combineExpr.charAt(index - 1);
56545665
char right = index >= combineExpr.length() - key.length() ? ' ' : combineExpr.charAt(index + key.length());
5655-
if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!') && (right == ' ' || right == ')')) {
5666+
if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!') && (right == ' ' || right == ')' || right == ':')) {
56565667
return true;
56575668
}
56585669
int newIndex = index + key.length() + 1;
@@ -5680,4 +5691,4 @@ public List<Object> getWithAsExprePreparedValueList() {
56805691
public void setWithAsExprePreparedValueList(List<Object> list) {
56815692
this.withAsExprePreparedValueList = list;
56825693
}
5683-
}
5694+
}

0 commit comments

Comments
 (0)