Skip to content

Commit b248c69

Browse files
committed
优化 @combine 对应的代码,默认 combine 为逻辑运算模板,原来的 combine 重命名为 combineMap,combineExpression 重命名为 combine
1 parent 12738bf commit b248c69

File tree

3 files changed

+70
-47
lines changed

3 files changed

+70
-47
lines changed

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

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ public String getUserIdKey() {
775775
private Map<String, String> cast;
776776
private Map<String, Object> content; //Request内容,key:value形式,column = content.keySet(),values = content.values()
777777
private Map<String, Object> where; //筛选条件,key:value形式
778-
private Map<String, List<String>> combine; //条件组合,{ "&":[key], "|":[key], "!":[key] }
779-
private String combineExpression;
778+
private String combine; //条件组合, a | (b & c & !(d | !e))
779+
private Map<String, List<String>> combineMap; //条件组合,{ "&":[key], "|":[key], "!":[key] }
780780

781781
//array item <<<<<<<<<<
782782
private int count; //Table数量
@@ -2238,31 +2238,31 @@ protected float getMaxCombineRatio() {
22382238

22392239

22402240
@Override
2241-
public String getCombineExpression() {
2242-
return combineExpression;
2241+
public String getCombine() {
2242+
return combine;
22432243
}
22442244
@Override
2245-
public AbstractSQLConfig setCombineExpression(String combineExpression) {
2246-
this.combineExpression = combineExpression;
2245+
public AbstractSQLConfig setCombine(String combine) {
2246+
this.combine = combine;
22472247
return this;
22482248
}
22492249

22502250
@NotNull
22512251
@Override
2252-
public Map<String, List<String>> getCombine() {
2253-
List<String> andList = combine == null ? null : combine.get("&");
2252+
public Map<String, List<String>> getCombineMap() {
2253+
List<String> andList = combineMap == null ? null : combineMap.get("&");
22542254
if (andList == null) {
22552255
andList = where == null ? new ArrayList<String>() : new ArrayList<String>(where.keySet());
2256-
if (combine == null) {
2257-
combine = new HashMap<>();
2256+
if (combineMap == null) {
2257+
combineMap = new HashMap<>();
22582258
}
2259-
combine.put("&", andList);
2259+
combineMap.put("&", andList);
22602260
}
2261-
return combine;
2261+
return combineMap;
22622262
}
22632263
@Override
2264-
public AbstractSQLConfig setCombine(Map<String, List<String>> combine) {
2265-
this.combine = combine;
2264+
public AbstractSQLConfig setCombineMap(Map<String, List<String>> combineMap) {
2265+
this.combineMap = combineMap;
22662266
return this;
22672267
}
22682268

@@ -2329,8 +2329,8 @@ public AbstractSQLConfig putWhere(String key, Object value, boolean prior) {
23292329
where.put(key, value);
23302330
}
23312331

2332-
combine = getCombine();
2333-
List<String> andList = combine.get("&");
2332+
Map<String, List<String>> combineMap = getCombineMap();
2333+
List<String> andList = combineMap.get("&");
23342334
if (value == null) {
23352335
if (andList != null) {
23362336
andList.remove(key);
@@ -2392,7 +2392,7 @@ else if (key.equals(userIdInKey)) {
23922392
andList.add(key); //AbstractSQLExecutor.onPutColumn里getSQL,要保证缓存的SQL和查询的SQL里 where 的 key:value 顺序一致
23932393
}
23942394
}
2395-
combine.put("&", andList);
2395+
combineMap.put("&", andList);
23962396
}
23972397

23982398
return this;
@@ -2405,11 +2405,11 @@ else if (key.equals(userIdInKey)) {
24052405
@JSONField(serialize = false)
24062406
@Override
24072407
public String getWhereString(boolean hasPrefix) throws Exception {
2408-
String ce = getCombineExpression();
2409-
if (StringUtil.isEmpty(ce, true)) {
2410-
return getWhereString(hasPrefix, getMethod(), getWhere(), getCombine(), getJoinList(), ! isTest());
2408+
String combineExpr = getCombine();
2409+
if (StringUtil.isEmpty(combineExpr, true)) {
2410+
return getWhereString(hasPrefix, getMethod(), getWhere(), getCombineMap(), getJoinList(), ! isTest());
24112411
}
2412-
return getWhereString(hasPrefix, getMethod(), getWhere(), ce, getJoinList(), ! isTest());
2412+
return getWhereString(hasPrefix, getMethod(), getWhere(), combineExpr, getJoinList(), ! isTest());
24132413
}
24142414
/**获取WHERE
24152415
* @param method
@@ -2432,7 +2432,19 @@ public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String
24322432
return result;
24332433
}
24342434

2435-
2435+
/**解析 @combine 条件 key 组合的与或非+括号的逻辑运算表达式为具体的完整条件组合
2436+
* @param method
2437+
* @param quote
2438+
* @param table
2439+
* @param alias
2440+
* @param conditioinMap where 或 having 对应条件的 Map
2441+
* @param combine
2442+
* @param verifyName
2443+
* @param containRaw
2444+
* @param isHaving
2445+
* @return
2446+
* @throws Exception
2447+
*/
24362448
protected String parseCombineExpression(RequestMethod method, String quote, String table, String alias
24372449
, Map<String, Object> conditioinMap, String combine, boolean verifyName, boolean containRaw, boolean isHaving) throws Exception {
24382450

@@ -2693,6 +2705,17 @@ else if (StringUtil.isNotEmpty(andCond, true)) { // andCond 必须放后面,
26932705
return result;
26942706
}
26952707

2708+
/**已废弃,最快 6.0 删除,请尽快把前端/客户端 @combine:"a,b" 这种旧方式改为 @combine:"a | b" 这种新方式
2709+
* @param hasPrefix
2710+
* @param method
2711+
* @param where
2712+
* @param combine
2713+
* @param joinList
2714+
* @param verifyName
2715+
* @return
2716+
* @throws Exception
2717+
*/
2718+
@Deprecated
26962719
public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String, Object> where, Map<String, List<String>> combine, List<Join> joinList, boolean verifyName) throws Exception {
26972720
Set<Entry<String, List<String>>> combineSet = combine == null ? null : combine.entrySet();
26982721
if (combineSet == null || combineSet.isEmpty()) {
@@ -4437,9 +4460,9 @@ else if (id instanceof Subquery) {}
44374460
List<String> whereList = null;
44384461

44394462
String[] ws = StringUtil.split(combine);
4440-
String combineExpression = ws == null || ws.length != 1 ? null : ws[0];
4463+
String combineExpr = ws == null || ws.length != 1 ? null : ws[0];
44414464

4442-
Map<String, List<String>> combineMap = StringUtil.isNotEmpty(combineExpression, true) ? null : new LinkedHashMap<>();
4465+
Map<String, List<String>> combineMap = StringUtil.isNotEmpty(combineExpr, true) ? null : new LinkedHashMap<>();
44434466
List<String> andList = combineMap == null ? null : new ArrayList<>();
44444467
List<String> orList = combineMap == null ? null : new ArrayList<>();
44454468
List<String> notList = combineMap == null ? null : new ArrayList<>();
@@ -4459,14 +4482,14 @@ else if (id instanceof Subquery) {}
44594482
}
44604483

44614484
if (combineMap == null) {
4462-
if (StringUtil.isNotEmpty(combineExpression, true)) {
4485+
if (StringUtil.isNotEmpty(combineExpr, true)) {
44634486
List<String> banKeyList = Arrays.asList(idKey, idInKey, userIdKey, userIdInKey);
44644487

44654488
for (String key : banKeyList) {
4466-
int index = combineExpression.indexOf(key);
4489+
int index = combineExpr.indexOf(key);
44674490
if (index >= 0) {
4468-
char left = index <= 0 ? ' ' : combineExpression.charAt(index - 1);
4469-
char right = index >= combineExpression.length() - key.length() ? ' ' : combineExpression.charAt(index + key.length());
4491+
char left = index <= 0 ? ' ' : combineExpr.charAt(index - 1);
4492+
char right = index >= combineExpr.length() - key.length() ? ' ' : combineExpr.charAt(index + key.length());
44704493
if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!') && (right == ' ' || right == ')')) {
44714494
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的 value 里 " + key + " 不合法!"
44724495
+ "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!");
@@ -4567,8 +4590,8 @@ else if (whereList != null && whereList.contains(key)) {
45674590
combineMap.put("|", orList);
45684591
combineMap.put("!", notList);
45694592
}
4570-
config.setCombine(combineMap);
4571-
config.setCombineExpression(combineExpression);
4593+
config.setCombineMap(combineMap);
4594+
config.setCombine(combineExpr);
45724595

45734596
config.setContent(tableContent);
45744597
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
335335
capacity = config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount();
336336
if (capacity > 100) {
337337
// 有条件,条件越多过滤数据越多
338-
Map<String, List<String>> combine = config.getCombine();
338+
Map<String, List<String>> combine = config.getCombineMap();
339339

340340
List<String> andList = combine == null ? null : combine.get("&");
341341
int andCondCount = andList == null ? (config.getWhere() == null ? 0 : config.getWhere().size()) : andList.size();

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,6 @@ public interface SQLConfig {
156156
List<String> getRaw();
157157
SQLConfig setRaw(List<String> raw);
158158

159-
String getGroup();
160-
SQLConfig setGroup(String group);
161-
162-
Map<String, Object> getHaving();
163-
SQLConfig setHaving(Map<String, Object> having);
164-
165-
String getHavingCombine();
166-
SQLConfig setHavingCombine(String havingCombine);
167-
168-
String getOrder();
169-
SQLConfig setOrder(String order);
170-
171159
Subquery getFrom();
172160
SQLConfig setFrom(Subquery from);
173161

@@ -180,11 +168,11 @@ public interface SQLConfig {
180168
Map<String, Object> getContent();
181169
SQLConfig setContent(Map<String, Object> content);
182170

183-
Map<String, List<String>> getCombine();
184-
SQLConfig setCombine(Map<String, List<String>> combine);
171+
Map<String, List<String>> getCombineMap();
172+
SQLConfig setCombineMap(Map<String, List<String>> combineMap);
185173

186-
String getCombineExpression();
187-
SQLConfig setCombineExpression(String combineExpression);
174+
String getCombine();
175+
SQLConfig setCombine(String combine);
188176

189177
Map<String, String> getCast();
190178
SQLConfig setCast(Map<String, String> cast);
@@ -195,6 +183,18 @@ public interface SQLConfig {
195183
Map<String, Object> getWhere();
196184
SQLConfig setWhere(Map<String, Object> where);
197185

186+
String getGroup();
187+
SQLConfig setGroup(String group);
188+
189+
Map<String, Object> getHaving();
190+
SQLConfig setHaving(Map<String, Object> having);
191+
192+
String getHavingCombine();
193+
SQLConfig setHavingCombine(String havingCombine);
194+
195+
String getOrder();
196+
SQLConfig setOrder(String order);
197+
198198
/**
199199
* exactMatch = false
200200
* @param key

0 commit comments

Comments
 (0)