Skip to content

Commit 6f80013

Browse files
committed
解决 JOIN 副表有 引用赋值 外的条件时因为缓存 SQL WHERE 中条件顺序不一致导致多余查询
1 parent ef41ebb commit 6f80013

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,19 @@ else if (join != null){
11741174
throw new IllegalArgumentException("/" + path + ":'/targetTable/targetKey' 中路径对应的对象 '" + targetTableKey + "':{} 不存在或值为 null !必须是 {} 这种 JSONObject 格式!");
11751175
}
11761176

1177-
tableObj.put(key, tableObj.remove(key)); //保证和SQLExcecutor缓存的Config里where顺序一致,生成的SQL也就一致
1177+
// 保证和 SQLExcecutor 缓存的 Config 里 where 顺序一致,生成的 SQL 也就一致 <<<<<<<<<
1178+
// AbstractSQLConfig.newSQLConfig 中强制把 id, id{}, userId, userId{} 放到了最前面 tableObj.put(key, tableObj.remove(key));
1179+
1180+
if (tableObj.size() > 1) { // 把 key 强制放最前,AbstractSQLExcecutor 中 config.putWhere 也是放尽可能最前
1181+
JSONObject newTableObj = new JSONObject(tableObj.size(), true);
1182+
newTableObj.put(key, tableObj.remove(key));
1183+
newTableObj.putAll(tableObj);
1184+
1185+
tableObj = newTableObj;
1186+
request.put(tableKey, tableObj);
1187+
}
1188+
// 保证和 SQLExcecutor 缓存的 Config 里 where 顺序一致,生成的 SQL 也就一致 >>>>>>>>>
1189+
11781190

11791191
Join j = new Join();
11801192
j.setPath(path);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,9 @@ public AbstractSQLConfig putWhere(String key, Object value, boolean prior) {
15701570
combine = getCombine();
15711571
List<String> andList = combine.get("&");
15721572
if (value == null) {
1573-
andList.remove(key);
1573+
if (andList != null) {
1574+
andList.remove(key);
1575+
}
15741576
}
15751577
else if (andList == null || andList.contains(key) == false) {
15761578
int i = 0;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
408408

409409

410410
//替换为 "id{}": [userId1, userId2, userId3...]
411-
jc.putWhere(j.getOriginKey(), null, false);
412-
jc.putWhere(j.getKey() + "{}", targetValueList, false);
411+
jc.putWhere(j.getOriginKey(), null, false); // remove orginKey
412+
jc.putWhere(j.getKey() + "{}", targetValueList, true); // add orginKey{}
413413

414414
jc.setMain(true).setPreparedValueList(new ArrayList<>());
415415

@@ -456,7 +456,7 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
456456
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n");
457457

458458
//缓存到 childMap
459-
cc.putWhere(j.getKey(), result.get(j.getKey()), false);
459+
cc.putWhere(j.getKey(), result.get(j.getKey()), true);
460460
cacheSql = cc.getSQL(false);
461461
childMap.put(cacheSql, result);
462462

@@ -531,7 +531,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
531531

532532
if (childConfig != null && childTable.equalsIgnoreCase(childConfig.getSQLTable())) {
533533

534-
childConfig.putWhere(j.getKey(), table.get(j.getTargetKey()), false);
534+
childConfig.putWhere(j.getKey(), table.get(j.getTargetKey()), true);
535535
childSql = childConfig.getSQL(false);
536536

537537
if (StringUtil.isEmpty(childSql, true)) {

0 commit comments

Comments
 (0)