Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1457,10 +1457,11 @@ else if (join != null){
// }
path = path.substring(index + 1);

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

// 取出Table对应的JSONObject,及内部引用赋值 key:value
JSONObject tableObj;
JSONObject tableObj = request;
JSONObject parentPathObj = null; // 保留
try {
tableObj = request.getJSONObject(tableKey);
for (String tableKeyPath : tablePath) {
parentPathObj = tableObj;
tableObj = tableObj.getJSONObject(tableKeyPath);
}
if (tableObj == null) {
throw new NullPointerException("tableObj == null");
}
Expand Down Expand Up @@ -1580,6 +1585,9 @@ else if (join != null){
j.setAlias(alias);
j.setOuter((JSONObject) outer);
j.setRequest(requestObj);
if (parentPathObj != null) {
j.setCount(parentPathObj.getInteger("count") != null ? parentPathObj.getInteger("count") : 1);
}

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

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

// tableObj.clear();
// tableObj.putAll(refObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5008,7 +5008,7 @@ public static <T extends Object> SQLConfig parseJoin(RequestMethod method, SQLCo
alias = j.getAlias();
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback);
SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(1);
SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(j.getCount());

if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
if (joinConfig.getDatabase() == null) {
Expand Down
26 changes: 15 additions & 11 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
Log.i(TAG, ">>> execute result = getCache('" + sql + "', " + position + ") = " + result);
if (result != null) {
cachedSQLCount ++;

if (getCache(sql,config).size() > 1) {
result.put(KEY_RAW_LIST, getCache(sql,config));
}
Log.d(TAG, "\n\n execute result != null >> return result;" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
return result;
}
Expand Down Expand Up @@ -589,19 +591,17 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {

if (isHead == false) {
// @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

executeAppJoin(config, resultList, childMap);
Map<String,List<JSONObject>> appJoinChildMap = new HashMap<>();
executeAppJoin(config, resultList, appJoinChildMap);

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

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

//<sql, Table>
for (Entry<String, JSONObject> entry : set) {
List<JSONObject> l = new ArrayList<>();
l.add(entry.getValue());
putCache(entry.getKey(), l, null);
for (Entry<String, List<JSONObject>> entry : set) {
putCache(entry.getKey(), entry.getValue(), null);
}

putCache(sql, resultList, config);
Expand Down Expand Up @@ -633,7 +633,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
* @param childMap
* @throws Exception
*/
protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map<String, JSONObject> childMap) throws Exception {
protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map<String, List<JSONObject>> childMap) throws Exception {
List<Join> joinList = config.getJoinList();
if (joinList != null) {

Expand Down Expand Up @@ -737,8 +737,12 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
}
}
cacheSql = cc.getSQL(false);
childMap.put(cacheSql, result);

List<JSONObject> results = childMap.get(cacheSql);
if (results == null) {
results = new ArrayList<>();
childMap.put(cacheSql,results);
}
results.add(result);
Log.d(TAG, ">>> executeAppJoin childMap.put('" + cacheSql + "', result); childMap.size() = " + childMap.size());
}
}
Expand Down
8 changes: 8 additions & 0 deletions APIJSONORM/src/main/java/apijson/orm/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Join {
private String joinType; // "@" - APP, "<" - LEFT, ">" - RIGHT, "*" - CROSS, "&" - INNER, "|" - FULL, "!" - OUTER, "^" - SIDE, "(" - ANTI, ")" - FOREIGN
private String table; // User
private String alias; // owner
private int count = 1; // 当app join子表,需要返回子表的行数,默认1行;
private List<On> onList; // ON User.id = Moment.userId AND ...

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

public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}

public String getJoinType() {
return joinType;
}
Expand Down