Skip to content

Commit 104db00

Browse files
authored
Merge pull request Tencent#413 from github291406933/master
feat: 支持跨层级app join
2 parents 0a76454 + f1430b2 commit 104db00

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,11 @@ else if (join != null){
14571457
// }
14581458
path = path.substring(index + 1);
14591459

1460-
index = path.indexOf("/");
1460+
index = path.lastIndexOf("/");
14611461
String tableKey = index < 0 ? path : path.substring(0, index); // User:owner
14621462
apijson.orm.Entry<String, String> entry = Pair.parseEntry(tableKey, true);
1463-
String table = entry.getKey(); // User
1463+
String[] tablePath = entry.getKey().split("/"); // User
1464+
String table = tableKey = tablePath[tablePath.length - 1]; // path最后一级为真实table;如:@/A/b/id@,b为目录最后一级
14641465
if (StringUtil.isName(table) == false) {
14651466
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":value 中 value 的 Table 值 " + table + " 不合法!"
14661467
+ "必须为 &/Table0,</Table1/key1,@/Table1:alias2/key2,... 或 { '&/Table0':{}, '</Table1/key1':{},... } 这种格式!"
@@ -1475,9 +1476,13 @@ else if (join != null){
14751476
}
14761477

14771478
// 取出Table对应的JSONObject,及内部引用赋值 key:value
1478-
JSONObject tableObj;
1479+
JSONObject tableObj = request;
1480+
JSONObject parentPathObj = null; // 保留
14791481
try {
1480-
tableObj = request.getJSONObject(tableKey);
1482+
for (String tableKeyPath : tablePath) {
1483+
parentPathObj = tableObj;
1484+
tableObj = tableObj.getJSONObject(tableKeyPath);
1485+
}
14811486
if (tableObj == null) {
14821487
throw new NullPointerException("tableObj == null");
14831488
}
@@ -1580,6 +1585,9 @@ else if (join != null){
15801585
j.setAlias(alias);
15811586
j.setOuter((JSONObject) outer);
15821587
j.setRequest(requestObj);
1588+
if (parentPathObj != null) {
1589+
j.setCount(parentPathObj.getInteger("count") != null ? parentPathObj.getInteger("count") : 1);
1590+
}
15831591

15841592
List<Join.On> onList = new ArrayList<>();
15851593
for (Entry<String, Object> refEntry : refSet) {
@@ -1656,7 +1664,7 @@ else if (join != null){
16561664

16571665
if (refObj.size() != tableObj.size()) { // 把 key 强制放最前,AbstractSQLExcecutor 中 config.putWhere 也是放尽可能最前
16581666
refObj.putAll(tableObj);
1659-
request.put(tableKey, refObj);
1667+
parentPathObj.put(tableKey, refObj);
16601668

16611669
// tableObj.clear();
16621670
// tableObj.putAll(refObj);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5008,7 +5008,7 @@ public static <T extends Object> SQLConfig parseJoin(RequestMethod method, SQLCo
50085008
alias = j.getAlias();
50095009
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
50105010
SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback);
5011-
SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(1);
5011+
SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(j.getCount());
50125012

50135013
if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
50145014
if (joinConfig.getDatabase() == null) {

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
260260
Log.i(TAG, ">>> execute result = getCache('" + sql + "', " + position + ") = " + result);
261261
if (result != null) {
262262
cachedSQLCount ++;
263-
263+
if (getCache(sql,config).size() > 1) {
264+
result.put(KEY_RAW_LIST, getCache(sql,config));
265+
}
264266
Log.d(TAG, "\n\n execute result != null >> return result;" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
265267
return result;
266268
}
@@ -589,19 +591,17 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
589591

590592
if (isHead == false) {
591593
// @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
592-
593-
executeAppJoin(config, resultList, childMap);
594+
Map<String,List<JSONObject>> appJoinChildMap = new HashMap<>();
595+
executeAppJoin(config, resultList, appJoinChildMap);
594596

595597
// @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
596598

597599
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
598-
Set<Entry<String, JSONObject>> set = childMap.entrySet();
600+
Set<Entry<String, List<JSONObject>>> set = appJoinChildMap.entrySet();
599601

600602
//<sql, Table>
601-
for (Entry<String, JSONObject> entry : set) {
602-
List<JSONObject> l = new ArrayList<>();
603-
l.add(entry.getValue());
604-
putCache(entry.getKey(), l, null);
603+
for (Entry<String, List<JSONObject>> entry : set) {
604+
putCache(entry.getKey(), entry.getValue(), null);
605605
}
606606

607607
putCache(sql, resultList, config);
@@ -633,7 +633,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
633633
* @param childMap
634634
* @throws Exception
635635
*/
636-
protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map<String, JSONObject> childMap) throws Exception {
636+
protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map<String, List<JSONObject>> childMap) throws Exception {
637637
List<Join> joinList = config.getJoinList();
638638
if (joinList != null) {
639639

@@ -737,8 +737,12 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
737737
}
738738
}
739739
cacheSql = cc.getSQL(false);
740-
childMap.put(cacheSql, result);
741-
740+
List<JSONObject> results = childMap.get(cacheSql);
741+
if (results == null) {
742+
results = new ArrayList<>();
743+
childMap.put(cacheSql,results);
744+
}
745+
results.add(result);
742746
Log.d(TAG, ">>> executeAppJoin childMap.put('" + cacheSql + "', result); childMap.size() = " + childMap.size());
743747
}
744748
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Join {
2222
private String joinType; // "@" - APP, "<" - LEFT, ">" - RIGHT, "*" - CROSS, "&" - INNER, "|" - FULL, "!" - OUTER, "^" - SIDE, "(" - ANTI, ")" - FOREIGN
2323
private String table; // User
2424
private String alias; // owner
25+
private int count = 1; // 当app join子表,需要返回子表的行数,默认1行;
2526
private List<On> onList; // ON User.id = Moment.userId AND ...
2627

2728
private JSONObject request; // { "id@":"/Moment/userId" }
@@ -39,6 +40,13 @@ public void setPath(String path) {
3940
this.path = path;
4041
}
4142

43+
public int getCount() {
44+
return count;
45+
}
46+
public void setCount(int count) {
47+
this.count = count;
48+
}
49+
4250
public String getJoinType() {
4351
return joinType;
4452
}

0 commit comments

Comments
 (0)