Skip to content

Commit b900c58

Browse files
committed
解决 > RIGHT JOIN, ^ SIDE JOIN, ! ANTI JOIN, ) FOREIGN JOIN 等不返回副表数据;解决 | FULL JOIN 返回的副表数据部分是错的
1 parent 6f80013 commit b900c58

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,8 +1769,8 @@ else if ("!".equals(ce.getKey())) {
17691769
if (isAntiJoin) { // ( ANTI JOIN: A & ! B
17701770
newWs += " ( " + ( isWsEmpty ? "" : ws + AND ) + NOT + " ( " + js + " ) " + " ) ";
17711771
}
1772-
else if (isForeignJoin) { // ) FOREIGN JOIN: B & ! A
1773-
newWs += " ( " + " ( " + js + " ) " + ( isWsEmpty ? "" : AND + NOT + ws ) + " ) ";
1772+
else if (isForeignJoin) { // ) FOREIGN JOIN: (! A) & B // preparedValueList.add 不好反过来 B & ! A
1773+
newWs += " ( " + NOT + " ( " + ws + " ) ) " + AND + " ( " + js + " ) ";
17741774
}
17751775
else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
17761776
//MySQL 因为 NULL 值处理问题,(A & ! B) | (B & ! A) 与 ! (A & B) 返回结果不一样,后者往往更多
@@ -3203,7 +3203,7 @@ public static SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<J
32033203
alias = j.getAlias();
32043204
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
32053205
SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback);
3206-
SQLConfig cacheConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(1);
3206+
SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias, j.getRequest(), null, false, callback).setCount(1);
32073207

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

32203223

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

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

32453250
j.setJoinConfig(joinConfig);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,15 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
389389
continue;
390390
}
391391

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

395402
//取出 "id@": "@/User/userId" 中所有 userId 的值
396403
List<Object> targetValueList = new ArrayList<>();
@@ -543,15 +550,18 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
543550
}
544551
}
545552
}
553+
554+
}
546555

556+
Object value = getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
557+
if (value != null) {
547558
if (finalTable == null) {
548559
finalTable = new JSONObject(true);
549560
childMap.put(childSql, finalTable);
550561
}
562+
finalTable.put(lable, value);
551563
}
552-
553-
finalTable.put(lable, getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap));
554-
564+
555565
return table;
556566
}
557567

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,48 @@ else if (originKey.endsWith("<>")) {
162162
}
163163

164164

165+
public boolean isAppJoin() {
166+
return "@".equals(getJoinType());
167+
}
168+
public boolean isLeftJoin() {
169+
return "<".equals(getJoinType());
170+
}
171+
public boolean isRightJoin() {
172+
return ">".equals(getJoinType());
173+
}
174+
public boolean isCrossJoin() {
175+
return "*".equals(getJoinType());
176+
}
177+
public boolean isInnerJoin() {
178+
return "&".equals(getJoinType());
179+
}
180+
public boolean isFullJoin() {
181+
String jt = getJoinType();
182+
return "".equals(jt) || "|".equals(jt);
183+
}
184+
public boolean isOuterJoin() {
185+
return "!".equals(getJoinType());
186+
}
187+
public boolean isSideJoin() {
188+
return "^".equals(getJoinType());
189+
}
190+
public boolean isAntiJoin() {
191+
return "(".equals(getJoinType());
192+
}
193+
public boolean isForeignJoin() {
194+
return ")".equals(getJoinType());
195+
}
165196

197+
public boolean isLeftOrRightJoin() {
198+
String jt = getJoinType();
199+
return "<".equals(jt) || ">".equals(jt);
200+
}
201+
202+
public boolean canCacheViceTable() {
203+
String jt = getJoinType();
204+
return "@".equals(jt) || "<".equals(jt) || ">".equals(jt) || "&".equals(jt) || "*".equals(jt) || ")".equals(jt);
205+
}
206+
166207
public boolean isSQLJoin() {
167208
return ! isAppJoin();
168209
}
@@ -171,22 +212,13 @@ public static boolean isSQLJoin(Join j) {
171212
return j != null && j.isSQLJoin();
172213
}
173214

174-
public boolean isAppJoin() {
175-
return "@".equals(getJoinType());
176-
}
177-
178215
public static boolean isAppJoin(Join j) {
179216
return j != null && j.isAppJoin();
180217
}
181-
182-
public boolean isLeftOrRightJoin() {
183-
return "<".equals(getJoinType()) || ">".equals(getJoinType());
184-
}
185-
218+
186219
public static boolean isLeftOrRightJoin(Join j) {
187220
return j != null && j.isLeftOrRightJoin();
188221
}
189222

190223

191-
192224
}

0 commit comments

Comments
 (0)