Skip to content

Commit

Permalink
解决 > RIGHT JOIN, ^ SIDE JOIN, ! ANTI JOIN, ) FOREIGN JOIN 等不返回副表数据;解决…
Browse files Browse the repository at this point in the history
… | FULL JOIN 返回的副表数据部分是错的
  • Loading branch information
TommyLemon committed Apr 17, 2021
1 parent 6f80013 commit b900c58
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
17 changes: 11 additions & 6 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1769,8 +1769,8 @@ else if ("!".equals(ce.getKey())) {
if (isAntiJoin) { // ( ANTI JOIN: A & ! B
newWs += " ( " + ( isWsEmpty ? "" : ws + AND ) + NOT + " ( " + js + " ) " + " ) ";
}
else if (isForeignJoin) { // ) FOREIGN JOIN: B & ! A
newWs += " ( " + " ( " + js + " ) " + ( isWsEmpty ? "" : AND + NOT + ws ) + " ) ";
else if (isForeignJoin) { // ) FOREIGN JOIN: (! A) & B // preparedValueList.add 不好反过来 B & ! A
newWs += " ( " + NOT + " ( " + ws + " ) ) " + AND + " ( " + js + " ) ";
}
else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
//MySQL 因为 NULL 值处理问题,(A & ! B) | (B & ! A) 与 ! (A & B) 返回结果不一样,后者往往更多
Expand Down Expand Up @@ -3203,7 +3203,7 @@ public static SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<J
alias = j.getAlias();
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback);
SQLConfig cacheConfig = 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(1);

if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
if (joinConfig.getDatabase() == null) {
Expand All @@ -3215,7 +3215,10 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
if (joinConfig.getSchema() == null) {
joinConfig.setSchema(config.getSchema()); //主表 JOIN 副表,默认 schema 一致
}
cacheConfig.setDatabase(joinConfig.getDatabase()).setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致

if (cacheConfig != null) {
cacheConfig.setDatabase(joinConfig.getDatabase()).setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
}


if (isQuery) {
Expand All @@ -3238,8 +3241,10 @@ LEFT JOIN ( SELECT count(*) AS count FROM sys.Comment ) AS Comment ON Comment.m
joinConfig.setMethod(GET); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId
joinConfig.setColumn(Arrays.asList(j.getKey())); //优化性能,不取非必要的字段

cacheConfig.setMethod(GET); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId
cacheConfig.setColumn(Arrays.asList(j.getKey())); //优化性能,不取非必要的字段
if (cacheConfig != null) {
cacheConfig.setMethod(GET); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId
cacheConfig.setColumn(Arrays.asList(j.getKey())); //优化性能,不取非必要的字段
}
}

j.setJoinConfig(joinConfig);
Expand Down
18 changes: 14 additions & 4 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,15 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
continue;
}

jc = j.getJoinConfig();
cc = j.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好
if (cc == null) {
if (Log.DEBUG) {
throw new NullPointerException("服务器内部错误, executeAppJoin cc == null ! 导致不能缓存 @ APP JOIN 的副表数据!");
}
continue;
}

jc = j.getJoinConfig();

//取出 "id@": "@/User/userId" 中所有 userId 的值
List<Object> targetValueList = new ArrayList<>();
Expand Down Expand Up @@ -543,15 +550,18 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
}
}
}

}

Object value = getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
if (value != null) {
if (finalTable == null) {
finalTable = new JSONObject(true);
childMap.put(childSql, finalTable);
}
finalTable.put(lable, value);
}

finalTable.put(lable, getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap));


return table;
}

Expand Down
52 changes: 42 additions & 10 deletions APIJSONORM/src/main/java/apijson/orm/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,48 @@ else if (originKey.endsWith("<>")) {
}


public boolean isAppJoin() {
return "@".equals(getJoinType());
}
public boolean isLeftJoin() {
return "<".equals(getJoinType());
}
public boolean isRightJoin() {
return ">".equals(getJoinType());
}
public boolean isCrossJoin() {
return "*".equals(getJoinType());
}
public boolean isInnerJoin() {
return "&".equals(getJoinType());
}
public boolean isFullJoin() {
String jt = getJoinType();
return "".equals(jt) || "|".equals(jt);
}
public boolean isOuterJoin() {
return "!".equals(getJoinType());
}
public boolean isSideJoin() {
return "^".equals(getJoinType());
}
public boolean isAntiJoin() {
return "(".equals(getJoinType());
}
public boolean isForeignJoin() {
return ")".equals(getJoinType());
}

public boolean isLeftOrRightJoin() {
String jt = getJoinType();
return "<".equals(jt) || ">".equals(jt);
}

public boolean canCacheViceTable() {
String jt = getJoinType();
return "@".equals(jt) || "<".equals(jt) || ">".equals(jt) || "&".equals(jt) || "*".equals(jt) || ")".equals(jt);
}

public boolean isSQLJoin() {
return ! isAppJoin();
}
Expand All @@ -171,22 +212,13 @@ public static boolean isSQLJoin(Join j) {
return j != null && j.isSQLJoin();
}

public boolean isAppJoin() {
return "@".equals(getJoinType());
}

public static boolean isAppJoin(Join j) {
return j != null && j.isAppJoin();
}

public boolean isLeftOrRightJoin() {
return "<".equals(getJoinType()) || ">".equals(getJoinType());
}


public static boolean isLeftOrRightJoin(Join j) {
return j != null && j.isLeftOrRightJoin();
}



}

0 comments on commit b900c58

Please sign in to comment.