Skip to content

Commit dabbeeb

Browse files
committed
Server: LEFT JOIN 和 RIGHT JOIN 的 表对象内部的 @group, @having, @order 不加到子查询外层,其它 SQL JOIN 的不允许被 join 键值对里的覆盖
1 parent 6f0053e commit dabbeeb

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ public String getGroupString(boolean hasPrefix) {
308308
//加上子表的 group
309309
String joinGroup = "";
310310
if (joinList != null) {
311-
SQLConfig ecfg;
312311
SQLConfig cfg;
313312
String c;
314313
boolean first = true;
@@ -317,14 +316,7 @@ public String getGroupString(boolean hasPrefix) {
317316
continue;
318317
}
319318

320-
ecfg = j.getOutterConfig();
321-
if (ecfg != null && ecfg.getGroup() != null) { //优先级更高
322-
cfg = ecfg;
323-
}
324-
else {
325-
cfg = j.getJoinConfig();
326-
}
327-
319+
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
328320
cfg.setAlias(cfg.getTable());
329321

330322
c = ((AbstractSQLConfig) cfg).getGroupString(false);
@@ -376,7 +368,6 @@ public String getHavingString(boolean hasPrefix) {
376368
//加上子表的 having
377369
String joinHaving = "";
378370
if (joinList != null) {
379-
SQLConfig ecfg;
380371
SQLConfig cfg;
381372
String c;
382373
boolean first = true;
@@ -385,14 +376,7 @@ public String getHavingString(boolean hasPrefix) {
385376
continue;
386377
}
387378

388-
ecfg = j.getOutterConfig();
389-
if (ecfg != null && ecfg.getHaving() != null) { //优先级更高
390-
cfg = ecfg;
391-
}
392-
else {
393-
cfg = j.getJoinConfig();
394-
}
395-
379+
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
396380
cfg.setAlias(cfg.getTable());
397381

398382
c = ((AbstractSQLConfig) cfg).getHavingString(false);
@@ -492,7 +476,6 @@ public String getOrderString(boolean hasPrefix) {
492476
//加上子表的 order
493477
String joinOrder = "";
494478
if (joinList != null) {
495-
SQLConfig ecfg;
496479
SQLConfig cfg;
497480
String c;
498481
boolean first = true;
@@ -501,14 +484,7 @@ public String getOrderString(boolean hasPrefix) {
501484
continue;
502485
}
503486

504-
ecfg = j.getOutterConfig();
505-
if (ecfg != null && ecfg.getOrder() != null) { //优先级更高
506-
cfg = ecfg;
507-
}
508-
else {
509-
cfg = j.getJoinConfig();
510-
}
511-
487+
cfg = j.isLeftOrRightJoin() ? j.getOutterConfig() : j.getJoinConfig();
512488
cfg.setAlias(cfg.getTable());
513489

514490
c = ((AbstractSQLConfig) cfg).getOrderString(false);
@@ -2384,7 +2360,7 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
23842360

23852361
joinConfig.setMain(false).setKeyPrefix(true);
23862362

2387-
if ("<".equals(j.getJoinType()) || ">".equals(j.getJoinType())) {
2363+
if (j.isLeftOrRightJoin()) {
23882364
SQLConfig outterConfig = newSQLConfig(method, name, j.getOutter(), null, callback);
23892365
outterConfig.setMain(false).setKeyPrefix(true);
23902366
j.setOutterConfig(outterConfig);

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/Join.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
* @author Lemon
2323
*/
2424
public class Join {
25-
25+
2626
private String path;
27-
27+
2828
private String originKey;
2929
private String originValue;
3030

@@ -37,7 +37,7 @@ public class Join {
3737
private String targetKey; // userId
3838

3939
private JSONObject outter;
40-
40+
4141
private SQLConfig joinConfig;
4242
private SQLConfig cacheConfig;
4343
private SQLConfig outterConfig;
@@ -61,7 +61,7 @@ public String getOriginValue() {
6161
public void setOriginValue(String originValue) {
6262
this.originValue = originValue;
6363
}
64-
64+
6565
public String getJoinType() {
6666
return joinType;
6767
}
@@ -125,23 +125,23 @@ public SQLConfig getCacheConfig() {
125125
public void setCacheConfig(SQLConfig cacheConfig) {
126126
this.cacheConfig = cacheConfig;
127127
}
128-
128+
129129
public SQLConfig getOutterConfig() {
130130
return outterConfig;
131131
}
132132
public void setOutterConfig(SQLConfig outterConfig) {
133133
this.outterConfig = outterConfig;
134134
}
135135

136-
136+
137137
public void setKeyAndType(@NotNull String originKey) throws Exception { //id, id@, id{}@, contactIdList<>@ ...
138138
if (originKey.endsWith("@")) {
139139
originKey = originKey.substring(0, originKey.length() - 1);
140140
}
141141
else { //TODO 暂时只允许 User.id = Moment.userId 字段关联,不允许 User.id = 82001 这种
142142
throw new IllegalArgumentException(joinType + "/.../" + name + "/" + originKey + " 不合法!join:'.../refKey'" + " 中 refKey 必须以 @ 结尾!");
143143
}
144-
144+
145145
if (originKey.endsWith("{}")) {
146146
setRelateType("{}");
147147
setKey(originKey.substring(0, originKey.length() - 2));
@@ -155,25 +155,32 @@ else if (originKey.endsWith("<>")) {
155155
setKey(originKey);
156156
}
157157
}
158-
159-
160-
158+
159+
160+
161161
public boolean isSQLJoin() {
162162
return ! isAppJoin();
163163
}
164-
164+
165165
public static boolean isSQLJoin(Join j) {
166166
return j != null && j.isSQLJoin();
167167
}
168-
168+
169169
public boolean isAppJoin() {
170170
return "@".equals(getJoinType());
171171
}
172-
172+
173173
public static boolean isAppJoin(Join j) {
174174
return j != null && j.isAppJoin();
175175
}
176-
176+
177+
public boolean isLeftOrRightJoin() {
178+
return "<".equals(getJoinType()) || ">".equals(getJoinType());
179+
}
180+
181+
public static boolean isLeftOrRightJoin(Join j) {
182+
return j != null && j.isLeftOrRightJoin();
183+
}
177184

178185

179186

0 commit comments

Comments
 (0)