Skip to content

Commit 3c21439

Browse files
committed
fix: 现在的 Join.java 中 SQLConfig outerConfig 重命名成 onConfig 及对应的 get set 方法,然后再加回来 SQLConfig outerConfig,然后下方的键值对放到 outerConfig 上,后面再具体 AbstractSQLConfig gainWhereString。
1、inner join 本身是最外面增加条件,没有增加 2、onConfig "@group": "key2+" // JOIN 外层 GROYP BY key2 "@having": "count(key3)>0" // JOIN 外层 HAVING count(key3)>0 "@order": "key2+,key3-" // JOIN 外层 ORDER BY key2 ASC, key3 DESC 本身这些条件就是加到最外层。
1 parent 7314336 commit 3c21439

File tree

3 files changed

+131
-50
lines changed

3 files changed

+131
-50
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,39 @@ else if (join != null){
15631563
throw new UnsupportedDataTypeException(TAG + ".onJoinParse join 只能是 String 或 Map<String, Object> 类型!");
15641564
}
15651565

1566-
Set<Entry<String, Object>> set = joinMap == null ? null : joinMap.entrySet();
1566+
List<Entry<String, Object>> slashKeys = new ArrayList<>();
1567+
List<Entry<String, Object>> nonSlashKeys = new ArrayList<>();
1568+
Set<Entry<String, Object>> entries = joinMap == null ? null : joinMap.entrySet();
1569+
1570+
if (entries == null || entries.isEmpty()) {
1571+
Log.e(TAG, "onJoinParse set == null || set.isEmpty() >> return null;");
1572+
return null;
1573+
}
1574+
for (Entry<String, Object> e : entries) {
1575+
String path = e.getKey();
1576+
if (path != null && path.indexOf("/") > 0) {
1577+
slashKeys.add(e); // 以 / 开头的 key,例如 </Table/key@
1578+
} else {
1579+
nonSlashKeys.add(e); // 普通 key,例如 Table: {}
1580+
}
1581+
}
1582+
1583+
Map<String, Object> whereJoinMap = new LinkedHashMap<>();
1584+
1585+
for (Entry<String, Object> e : nonSlashKeys) {
1586+
String tableKey = e.getKey(); // 如 "Location_info"
1587+
Object tableObj = e.getValue(); // value 是 Map
1588+
1589+
if (request.containsKey(tableKey)) {
1590+
whereJoinMap.put(tableKey, tableObj);
1591+
} else {
1592+
Log.w(TAG, "跳过 join 中 key = " + tableKey + ",因为它不在 request 中");
1593+
}
1594+
}
1595+
1596+
1597+
Set<Entry<String, Object>> set = joinMap == null ? null : new LinkedHashSet<>(slashKeys);
1598+
;
15671599
if (set == null || set.isEmpty()) {
15681600
Log.e(TAG, "onJoinParse set == null || set.isEmpty() >> return null;");
15691601
return null;
@@ -1759,9 +1791,15 @@ else if (join != null){
17591791
j.setAlias(alias);
17601792

17611793
M outerObj = (M) JSON.createJSONObject((Map<String, Object>) outer);
1762-
j.setOuter(outerObj);
1794+
j.setOn(outerObj);
17631795
j.setRequest(requestObj);
17641796

1797+
if (whereJoinMap.containsKey(table)) {
1798+
Object rawOuter = whereJoinMap.get(table);
1799+
M outerObj1 = (M) JSON.createJSONObject((Map<String, Object>) rawOuter);
1800+
j.setOuter(outerObj1);
1801+
}
1802+
17651803
if (arrKey != null) {
17661804
Integer count = getInteger(parentPathObj, apijson.JSONRequest.KEY_COUNT);
17671805
j.setCount(count == null ? getDefaultQueryCount() : count);

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

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,21 @@
55

66
package apijson.orm;
77

8-
import java.util.*;
9-
import java.util.Map.Entry;
10-
import java.util.regex.Pattern;
11-
128
import apijson.*;
139
import apijson.orm.Join.On;
1410
import apijson.orm.exception.NotExistException;
1511
import apijson.orm.exception.UnsupportedDataTypeException;
16-
import apijson.orm.model.Access;
17-
import apijson.orm.model.AllColumn;
18-
import apijson.orm.model.AllColumnComment;
19-
import apijson.orm.model.AllTable;
20-
import apijson.orm.model.AllTableComment;
21-
import apijson.orm.model.Column;
22-
import apijson.orm.model.Document;
23-
import apijson.orm.model.ExtendedProperty;
24-
import apijson.orm.model.Function;
25-
import apijson.orm.model.PgAttribute;
26-
import apijson.orm.model.PgClass;
27-
import apijson.orm.model.Request;
28-
import apijson.orm.model.SysColumn;
29-
import apijson.orm.model.SysTable;
30-
import apijson.orm.model.Table;
31-
import apijson.orm.model.TestRecord;
12+
import apijson.orm.model.*;
13+
14+
import java.util.*;
15+
import java.util.Map.Entry;
16+
import java.util.regex.Pattern;
3217

3318
import static apijson.JSON.getBoolean;
3419
import static apijson.JSON.getString;
3520
import static apijson.JSONMap.*;
36-
import static apijson.RequestMethod.DELETE;
37-
import static apijson.RequestMethod.GET;
38-
import static apijson.RequestMethod.POST;
39-
import static apijson.RequestMethod.PUT;
40-
import static apijson.SQL.AND;
41-
import static apijson.SQL.NOT;
42-
import static apijson.SQL.ON;
43-
import static apijson.SQL.OR;
21+
import static apijson.RequestMethod.*;
22+
import static apijson.SQL.*;
4423

4524
/**config sql for JSON Request
4625
* @author Lemon
@@ -1574,7 +1553,7 @@ public String gainGroupString(boolean hasPrefix) {
15741553
continue;
15751554
}
15761555

1577-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1556+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
15781557
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getGroup() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
15791558

15801559
if (cfg != null) {
@@ -1589,6 +1568,23 @@ public String gainGroupString(boolean hasPrefix) {
15891568
first = false;
15901569
}
15911570
}
1571+
1572+
////先处理左/右关联,内关联忽略
1573+
//SQLConfig<T, M, L> outerConfig = join.getOuterConfig();
1574+
//SQLConfig<T, M, L> outerConfig2 = (outerConfig != null && outerConfig.getGroup() != null) || join.isLeftOrRightJoin() ? outerConfig : null;
1575+
//
1576+
//if (outerConfig2 != null) {
1577+
// outerConfig2.setMain(false).setKeyPrefix(true);
1578+
// //if (StringUtil.isEmpty(cfg.getAlias(), true)) {
1579+
// // cfg.setAlias(cfg.getTable());
1580+
// //}
1581+
// String c = ((AbstractSQLConfig<?, ?, ?>) outerConfig2).gainGroupString(false);
1582+
//
1583+
// if (StringUtil.isNotEmpty(c, true)) {
1584+
// joinGroup += (first ? "" : ", ") + c;
1585+
// first = false;
1586+
// }
1587+
//}
15921588
}
15931589
}
15941590

@@ -1650,7 +1646,7 @@ public String gainHavingString(boolean hasPrefix) throws Exception {
16501646
continue;
16511647
}
16521648

1653-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1649+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
16541650
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getHaving() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
16551651

16561652
if (cfg != null) {
@@ -1763,7 +1759,7 @@ public String gainSampleString(boolean hasPrefix) {
17631759
continue;
17641760
}
17651761

1766-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1762+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
17671763
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getSample() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
17681764

17691765
if (cfg != null) {
@@ -1833,7 +1829,7 @@ public String gainLatestString(boolean hasPrefix) {
18331829
continue;
18341830
}
18351831

1836-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1832+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
18371833
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getLatest() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
18381834

18391835
if (cfg != null) {
@@ -1898,7 +1894,7 @@ public String gainPartitionString(boolean hasPrefix) {
18981894
continue;
18991895
}
19001896

1901-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1897+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
19021898
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getPartition() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
19031899

19041900
if (cfg != null) {
@@ -1963,7 +1959,7 @@ public String gainFillString(boolean hasPrefix) {
19631959
continue;
19641960
}
19651961

1966-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
1962+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
19671963
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getFill() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
19681964

19691965
if (cfg != null) {
@@ -2029,14 +2025,15 @@ public AbstractSQLConfig<T, M, L> setOrder(String order) {
20292025
public String gainOrderString(boolean hasPrefix) {
20302026
//加上子表的 order
20312027
String joinOrder = "";
2028+
String joinOuterOrder = "";
20322029
if (joinList != null) {
20332030
boolean first = true;
20342031
for (Join<T, M, L> join : joinList) {
20352032
if (join.isAppJoin()) {
20362033
continue;
20372034
}
20382035

2039-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
2036+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
20402037
SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getOrder() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig();
20412038

20422039
if (cfg != null) {
@@ -2345,7 +2342,7 @@ public String gainColumnString(boolean inSQLJoin) throws Exception {
23452342
continue;
23462343
}
23472344

2348-
SQLConfig<T, M, L> ocfg = join.getOuterConfig();
2345+
SQLConfig<T, M, L> ocfg = join.getOnConfig();
23492346
boolean isEmpty = ocfg == null || ocfg.getColumn() == null;
23502347
boolean isLeftOrRightJoin = join.isLeftOrRightJoin();
23512348

@@ -3729,8 +3726,9 @@ protected String concatJoinWhereString(String whereString) throws Exception {
37293726
List<Object> pvl = new ArrayList<>(getPreparedValueList());
37303727

37313728
SQLConfig<T, M, L> jc;
3729+
SQLConfig<T, M, L> outerConfig;
37323730
String js;
3733-
3731+
boolean isWsEmpty = StringUtil.isEmpty(ws, true);
37343732
boolean changed = false;
37353733
// 各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样?
37363734
for (Join<T, M, L> j : joinList) {
@@ -3741,6 +3739,24 @@ protected String concatJoinWhereString(String whereString) throws Exception {
37413739
case "@": // APP JOIN
37423740
case "<": // LEFT JOIN
37433741
case ">": // RIGHT JOIN
3742+
outerConfig = j.getOuterConfig();
3743+
if (outerConfig == null){
3744+
break;
3745+
}
3746+
boolean isMain1 = outerConfig.isMain();
3747+
outerConfig.setMain(false).setPrepared(isPrepared()).setPreparedValueList(new ArrayList<Object>());
3748+
String outerWhere = outerConfig.gainWhereString(false);
3749+
3750+
int logic1 = Logic.getType(jt);
3751+
newWs += " ( "
3752+
+ gainCondition(
3753+
Logic.isNot(logic1),
3754+
ws
3755+
+ ( isWsEmpty ? "" : (Logic.isAnd(logic1) ? AND : OR) )
3756+
+ " ( " + outerWhere + " ) "
3757+
)
3758+
+ " ) ";
3759+
changed = true;
37443760
break;
37453761

37463762
case "&": // INNER JOIN: A & B
@@ -3761,7 +3777,7 @@ protected String concatJoinWhereString(String whereString) throws Exception {
37613777
boolean isSideJoin = "^".equals(jt);
37623778
boolean isAntiJoin = "(".equals(jt);
37633779
boolean isForeignJoin = ")".equals(jt);
3764-
boolean isWsEmpty = StringUtil.isEmpty(ws, true);
3780+
//boolean isWsEmpty = StringUtil.isEmpty(ws, true);
37653781

37663782
if (isWsEmpty) {
37673783
if (isOuterJoin) { // ! OUTER JOIN: ! (A | B)
@@ -5202,7 +5218,7 @@ public String gainJoinString() throws Exception {
52025218
);
52035219
}
52045220

5205-
SQLConfig<T, M, L> oc = j.getOuterConfig();
5221+
SQLConfig<T, M, L> oc = j.getOnConfig();
52065222
String ow = null;
52075223
if (oc != null) {
52085224
oc.setPrepared(isPrepared());
@@ -6305,13 +6321,22 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
63056321

63066322
joinConfig.setMain(false).setKeyPrefix(true);
63076323

6324+
if (join.getOn() != null) {
6325+
SQLConfig<T, M, L> onConfig = newSQLConfig(method, table, alias, join.getOn(), null, false, callback);
6326+
onConfig.setMain(false)
6327+
.setKeyPrefix(true)
6328+
.setDatabase(joinConfig.getDatabase())
6329+
.setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
6330+
6331+
join.setOnConfig(onConfig);
6332+
}
6333+
63086334
if (join.getOuter() != null) {
63096335
SQLConfig<T, M, L> outerConfig = newSQLConfig(method, table, alias, join.getOuter(), null, false, callback);
63106336
outerConfig.setMain(false)
63116337
.setKeyPrefix(true)
63126338
.setDatabase(joinConfig.getDatabase())
63136339
.setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
6314-
63156340
join.setOuterConfig(outerConfig);
63166341
}
63176342
}

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ public class Join<T, M extends Map<String, Object>, L extends List<Object>> {
2525
private List<On> onList; // ON User.id = Moment.userId AND ...
2626

2727
private M request; // { "id@":"/Moment/userId" }
28-
private M outer; // "join": { "</User": { "@order":"id-", "@group":"id", "name~":"a", "tag$":"%a%", "@combine": "name~,tag$" } } 中的 </User 对应值
28+
private M on; // "join": { "</User": { "@order":"id-", "@group":"id", "name~":"a", "tag$":"%a%", "@combine": "name~,tag$" } } 中的 </User 对应值
2929

30+
private M outer; // "join": { "</User": { "key2": value2, "@column": "key2,key3","@group": "key2+" }}
3031
private SQLConfig<T, M, L> joinConfig;
3132
private SQLConfig<T, M, L> cacheConfig;
32-
private SQLConfig<T, M, L> outerConfig;
33+
private SQLConfig<T, M, L> onConfig;
3334

35+
private SQLConfig<T, M, L> outerConfig;
3436

3537
public String getPath() {
3638
return path;
@@ -78,13 +80,29 @@ public M getRequest() {
7880
public void setRequest(M request) {
7981
this.request = request;
8082
}
81-
public M getOuter() {
82-
return outer;
83+
public M getOn() {
84+
return on;
85+
}
86+
public void setOn(M on) {
87+
this.on = on;
8388
}
89+
8490
public void setOuter(M outer) {
8591
this.outer = outer;
8692
}
8793

94+
public M getOuter() {
95+
return outer;
96+
}
97+
98+
public SQLConfig<T, M, L> getOuterConfig() {
99+
return outerConfig;
100+
}
101+
102+
public void setOuterConfig(SQLConfig<T, M, L> outerConfig) {
103+
this.outerConfig = outerConfig;
104+
}
105+
88106
public SQLConfig<T, M, L> getJoinConfig() {
89107
return joinConfig;
90108
}
@@ -97,11 +115,11 @@ public SQLConfig<T, M, L> getCacheConfig() {
97115
public void setCacheConfig(SQLConfig<T, M, L> cacheConfig) {
98116
this.cacheConfig = cacheConfig;
99117
}
100-
public SQLConfig<T, M, L> getOuterConfig() {
101-
return outerConfig;
118+
public SQLConfig<T, M, L> getOnConfig() {
119+
return onConfig;
102120
}
103-
public void setOuterConfig(SQLConfig<T, M, L> outerConfig) {
104-
this.outerConfig = outerConfig;
121+
public void setOnConfig(SQLConfig<T, M, L> onConfig) {
122+
this.onConfig = onConfig;
105123
}
106124

107125
public boolean isOne2One() {

0 commit comments

Comments
 (0)