Skip to content

Commit

Permalink
优化 @combine 对应的代码,默认 combine 为逻辑运算模板,原来的 combine 重命名为 combineMap,comb…
Browse files Browse the repository at this point in the history
…ineExpression 重命名为 combine
  • Loading branch information
TommyLemon committed Mar 26, 2022
1 parent 12738bf commit b248c69
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 47 deletions.
83 changes: 53 additions & 30 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Expand Up @@ -775,8 +775,8 @@ public String getUserIdKey() {
private Map<String, String> cast;
private Map<String, Object> content; //Request内容,key:value形式,column = content.keySet(),values = content.values()
private Map<String, Object> where; //筛选条件,key:value形式
private Map<String, List<String>> combine; //条件组合,{ "&":[key], "|":[key], "!":[key] }
private String combineExpression;
private String combine; //条件组合, a | (b & c & !(d | !e))
private Map<String, List<String>> combineMap; //条件组合,{ "&":[key], "|":[key], "!":[key] }

//array item <<<<<<<<<<
private int count; //Table数量
Expand Down Expand Up @@ -2238,31 +2238,31 @@ protected float getMaxCombineRatio() {


@Override
public String getCombineExpression() {
return combineExpression;
public String getCombine() {
return combine;
}
@Override
public AbstractSQLConfig setCombineExpression(String combineExpression) {
this.combineExpression = combineExpression;
public AbstractSQLConfig setCombine(String combine) {
this.combine = combine;
return this;
}

@NotNull
@Override
public Map<String, List<String>> getCombine() {
List<String> andList = combine == null ? null : combine.get("&");
public Map<String, List<String>> getCombineMap() {
List<String> andList = combineMap == null ? null : combineMap.get("&");
if (andList == null) {
andList = where == null ? new ArrayList<String>() : new ArrayList<String>(where.keySet());
if (combine == null) {
combine = new HashMap<>();
if (combineMap == null) {
combineMap = new HashMap<>();
}
combine.put("&", andList);
combineMap.put("&", andList);
}
return combine;
return combineMap;
}
@Override
public AbstractSQLConfig setCombine(Map<String, List<String>> combine) {
this.combine = combine;
public AbstractSQLConfig setCombineMap(Map<String, List<String>> combineMap) {
this.combineMap = combineMap;
return this;
}

Expand Down Expand Up @@ -2329,8 +2329,8 @@ public AbstractSQLConfig putWhere(String key, Object value, boolean prior) {
where.put(key, value);
}

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

return this;
Expand All @@ -2405,11 +2405,11 @@ else if (key.equals(userIdInKey)) {
@JSONField(serialize = false)
@Override
public String getWhereString(boolean hasPrefix) throws Exception {
String ce = getCombineExpression();
if (StringUtil.isEmpty(ce, true)) {
return getWhereString(hasPrefix, getMethod(), getWhere(), getCombine(), getJoinList(), ! isTest());
String combineExpr = getCombine();
if (StringUtil.isEmpty(combineExpr, true)) {
return getWhereString(hasPrefix, getMethod(), getWhere(), getCombineMap(), getJoinList(), ! isTest());
}
return getWhereString(hasPrefix, getMethod(), getWhere(), ce, getJoinList(), ! isTest());
return getWhereString(hasPrefix, getMethod(), getWhere(), combineExpr, getJoinList(), ! isTest());
}
/**获取WHERE
* @param method
Expand All @@ -2432,7 +2432,19 @@ public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String
return result;
}


/**解析 @combine 条件 key 组合的与或非+括号的逻辑运算表达式为具体的完整条件组合
* @param method
* @param quote
* @param table
* @param alias
* @param conditioinMap where 或 having 对应条件的 Map
* @param combine
* @param verifyName
* @param containRaw
* @param isHaving
* @return
* @throws Exception
*/
protected String parseCombineExpression(RequestMethod method, String quote, String table, String alias
, Map<String, Object> conditioinMap, String combine, boolean verifyName, boolean containRaw, boolean isHaving) throws Exception {

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

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

String[] ws = StringUtil.split(combine);
String combineExpression = ws == null || ws.length != 1 ? null : ws[0];
String combineExpr = ws == null || ws.length != 1 ? null : ws[0];

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

if (combineMap == null) {
if (StringUtil.isNotEmpty(combineExpression, true)) {
if (StringUtil.isNotEmpty(combineExpr, true)) {
List<String> banKeyList = Arrays.asList(idKey, idInKey, userIdKey, userIdInKey);

for (String key : banKeyList) {
int index = combineExpression.indexOf(key);
int index = combineExpr.indexOf(key);
if (index >= 0) {
char left = index <= 0 ? ' ' : combineExpression.charAt(index - 1);
char right = index >= combineExpression.length() - key.length() ? ' ' : combineExpression.charAt(index + key.length());
char left = index <= 0 ? ' ' : combineExpr.charAt(index - 1);
char right = index >= combineExpr.length() - key.length() ? ' ' : combineExpr.charAt(index + key.length());
if ((left == ' ' || left == '(' || left == '&' || left == '|' || left == '!') && (right == ' ' || right == ')')) {
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的 value 里 " + key + " 不合法!"
+ "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!");
Expand Down Expand Up @@ -4567,8 +4590,8 @@ else if (whereList != null && whereList.contains(key)) {
combineMap.put("|", orList);
combineMap.put("!", notList);
}
config.setCombine(combineMap);
config.setCombineExpression(combineExpression);
config.setCombineMap(combineMap);
config.setCombine(combineExpr);

config.setContent(tableContent);
}
Expand Down
Expand Up @@ -335,7 +335,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
capacity = config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount();
if (capacity > 100) {
// 有条件,条件越多过滤数据越多
Map<String, List<String>> combine = config.getCombine();
Map<String, List<String>> combine = config.getCombineMap();

List<String> andList = combine == null ? null : combine.get("&");
int andCondCount = andList == null ? (config.getWhere() == null ? 0 : config.getWhere().size()) : andList.size();
Expand Down
32 changes: 16 additions & 16 deletions APIJSONORM/src/main/java/apijson/orm/SQLConfig.java
Expand Up @@ -156,18 +156,6 @@ public interface SQLConfig {
List<String> getRaw();
SQLConfig setRaw(List<String> raw);

String getGroup();
SQLConfig setGroup(String group);

Map<String, Object> getHaving();
SQLConfig setHaving(Map<String, Object> having);

String getHavingCombine();
SQLConfig setHavingCombine(String havingCombine);

String getOrder();
SQLConfig setOrder(String order);

Subquery getFrom();
SQLConfig setFrom(Subquery from);

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

Map<String, List<String>> getCombine();
SQLConfig setCombine(Map<String, List<String>> combine);
Map<String, List<String>> getCombineMap();
SQLConfig setCombineMap(Map<String, List<String>> combineMap);

String getCombineExpression();
SQLConfig setCombineExpression(String combineExpression);
String getCombine();
SQLConfig setCombine(String combine);

Map<String, String> getCast();
SQLConfig setCast(Map<String, String> cast);
Expand All @@ -195,6 +183,18 @@ public interface SQLConfig {
Map<String, Object> getWhere();
SQLConfig setWhere(Map<String, Object> where);

String getGroup();
SQLConfig setGroup(String group);

Map<String, Object> getHaving();
SQLConfig setHaving(Map<String, Object> having);

String getHavingCombine();
SQLConfig setHavingCombine(String havingCombine);

String getOrder();
SQLConfig setOrder(String order);

/**
* exactMatch = false
* @param key
Expand Down

0 comments on commit b248c69

Please sign in to comment.