Skip to content

Commit 3ea3e61

Browse files
committed
优化 where 和 JOIN 解析代码
1 parent cf7bdd7 commit 3ea3e61

File tree

1 file changed

+20
-137
lines changed

1 file changed

+20
-137
lines changed

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

Lines changed: 20 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,132 +2585,8 @@ else if (StringUtil.isNotEmpty(andWhere, true)) { // andWhere 必须放后面
25852585
setPreparedValueList(prepreadValues);
25862586
}
25872587

2588-
if (joinList != null) {
2589-
2590-
String newWs = "";
2591-
String ws = whereString;
2592-
2593-
List<Object> newPvl = new ArrayList<>();
2594-
List<Object> pvl = new ArrayList<>(getPreparedValueList());
2595-
2596-
SQLConfig jc;
2597-
String js;
2598-
2599-
boolean changed = false;
2600-
//各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样?
2601-
for (Join j : joinList) {
2602-
String jt = j.getJoinType();
2603-
2604-
switch (jt) {
2605-
case "*": // CROSS JOIN
2606-
case "@": // APP JOIN
2607-
case "<": // LEFT JOIN
2608-
case ">": // RIGHT JOIN
2609-
break;
2610-
2611-
case "&": // INNER JOIN: A & B
2612-
case "": // FULL JOIN: A | B
2613-
case "|": // FULL JOIN: A | B
2614-
case "!": // OUTER JOIN: ! (A | B)
2615-
case "^": // SIDE JOIN: ! (A & B)
2616-
case "(": // ANTI JOIN: A & ! B
2617-
case ")": // FOREIGN JOIN: B & ! A
2618-
jc = j.getJoinConfig();
2619-
boolean isMain = jc.isMain();
2620-
jc.setMain(false).setPrepared(isPrepared()).setPreparedValueList(new ArrayList<Object>());
2621-
js = jc.getWhereString(false);
2622-
jc.setMain(isMain);
2623-
2624-
boolean isOuterJoin = "!".equals(jt);
2625-
boolean isSideJoin = "^".equals(jt);
2626-
boolean isAntiJoin = "(".equals(jt);
2627-
boolean isForeignJoin = ")".equals(jt);
2628-
boolean isWsEmpty = StringUtil.isEmpty(ws, true);
2629-
2630-
if (isWsEmpty) {
2631-
if (isOuterJoin) { // ! OUTER JOIN: ! (A | B)
2632-
throw new NotExistException("no result for ! OUTER JOIN( ! (A | B) ) when A or B is empty!");
2633-
}
2634-
if (isForeignJoin) { // ) FOREIGN JOIN: B & ! A
2635-
throw new NotExistException("no result for ) FOREIGN JOIN( B & ! A ) when A is empty!");
2636-
}
2637-
}
2638-
2639-
if (StringUtil.isEmpty(js, true)) {
2640-
if (isOuterJoin) { // ! OUTER JOIN: ! (A | B)
2641-
throw new NotExistException("no result for ! OUTER JOIN( ! (A | B) ) when A or B is empty!");
2642-
}
2643-
if (isAntiJoin) { // ( ANTI JOIN: A & ! B
2644-
throw new NotExistException("no result for ( ANTI JOIN( A & ! B ) when B is empty!");
2645-
}
2646-
2647-
if (isWsEmpty) {
2648-
if (isSideJoin) {
2649-
throw new NotExistException("no result for ^ SIDE JOIN( ! (A & B) ) when both A and B are empty!");
2650-
}
2651-
}
2652-
else {
2653-
if (isSideJoin || isForeignJoin) {
2654-
newWs += " ( " + getCondition(true, ws) + " ) ";
2655-
2656-
newPvl.addAll(pvl);
2657-
newPvl.addAll(jc.getPreparedValueList());
2658-
changed = true;
2659-
}
2660-
}
2661-
2662-
continue;
2663-
}
2664-
2665-
if (StringUtil.isEmpty(newWs, true) == false) {
2666-
newWs += AND;
2667-
}
2668-
2669-
if (isAntiJoin) { // ( ANTI JOIN: A & ! B
2670-
newWs += " ( " + ( isWsEmpty ? "" : ws + AND ) + NOT + " ( " + js + " ) " + " ) ";
2671-
}
2672-
else if (isForeignJoin) { // ) FOREIGN JOIN: (! A) & B // preparedValueList.add 不好反过来 B & ! A
2673-
newWs += " ( " + NOT + " ( " + ws + " ) ) " + AND + " ( " + js + " ) ";
2674-
}
2675-
else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
2676-
//MySQL 因为 NULL 值处理问题,(A & ! B) | (B & ! A) 与 ! (A & B) 返回结果不一样,后者往往更多
2677-
newWs += " ( " + getCondition(
2678-
true,
2679-
( isWsEmpty ? "" : ws + AND ) + " ( " + js + " ) "
2680-
) + " ) ";
2681-
}
2682-
else { // & INNER JOIN: A & B; | FULL JOIN: A | B; OUTER JOIN: ! (A | B)
2683-
int logic = Logic.getType(jt);
2684-
newWs += " ( "
2685-
+ getCondition(
2686-
Logic.isNot(logic),
2687-
ws
2688-
+ ( isWsEmpty ? "" : (Logic.isAnd(logic) ? AND : OR) )
2689-
+ " ( " + js + " ) "
2690-
)
2691-
+ " ) ";
2692-
}
2693-
2694-
newPvl.addAll(pvl);
2695-
newPvl.addAll(jc.getPreparedValueList());
2696-
2697-
changed = true;
2698-
break;
2699-
default:
2700-
throw new UnsupportedOperationException(
2701-
"join:value 中 value 里的 " + jt + "/" + j.getPath()
2702-
+ "错误!不支持 " + jt + " 等 [ @ APP, < LEFT, > RIGHT, * CROSS"
2703-
+ ", & INNER, | FULL, ! OUTER, ^ SIDE, ( ANTI, ) FOREIGN ] 之外的 JOIN 类型 !"
2704-
);
2705-
}
2706-
}
2707-
2708-
if (changed) {
2709-
whereString = newWs;
2710-
setPreparedValueList(newPvl);
2711-
}
2712-
}
2713-
2588+
whereString = concatJoinWhereString(whereString);
2589+
27142590
String result = StringUtil.isEmpty(whereString, true) ? "" : (hasPrefix ? " WHERE " : "") + whereString;
27152591

27162592
if (result.isEmpty() && RequestMethod.isQueryMethod(method) == false) {
@@ -2721,7 +2597,6 @@ else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
27212597
}
27222598

27232599

2724-
27252600
public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String, Object> where, Map<String, List<String>> combine, List<Join> joinList, boolean verifyName) throws Exception {
27262601
Set<Entry<String, List<String>>> combineSet = combine == null ? null : combine.entrySet();
27272602
if (combineSet == null || combineSet.isEmpty()) {
@@ -2776,15 +2651,28 @@ else if ("!".equals(ce.getKey())) {
27762651
whereString += (isCombineFirst ? "" : AND) + (Logic.isNot(logic) ? NOT : "") + " ( " + cs + " ) ";
27772652
isCombineFirst = false;
27782653
}
2654+
2655+
whereString = concatJoinWhereString(whereString);
27792656

2657+
String s = StringUtil.isEmpty(whereString, true) ? "" : (hasPrefix ? " WHERE " : "") + whereString;
27802658

2659+
if (s.isEmpty() && RequestMethod.isQueryMethod(method) == false) {
2660+
throw new UnsupportedOperationException("写操作请求必须带条件!!!");
2661+
}
2662+
2663+
return s;
2664+
}
2665+
2666+
2667+
protected String concatJoinWhereString(String whereString) throws Exception {
2668+
List<Join> joinList = getJoinList();
27812669
if (joinList != null) {
27822670

27832671
String newWs = "";
27842672
String ws = whereString;
27852673

27862674
List<Object> newPvl = new ArrayList<>();
2787-
List<Object> pvl = new ArrayList<>(preparedValueList);
2675+
List<Object> pvl = new ArrayList<>(getPreparedValueList());
27882676

27892677
SQLConfig jc;
27902678
String js;
@@ -2873,7 +2761,7 @@ else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
28732761
) + " ) ";
28742762
}
28752763
else { // & INNER JOIN: A & B; | FULL JOIN: A | B; OUTER JOIN: ! (A | B)
2876-
logic = Logic.getType(jt);
2764+
int logic = Logic.getType(jt);
28772765
newWs += " ( "
28782766
+ getCondition(
28792767
Logic.isNot(logic),
@@ -2900,19 +2788,14 @@ else if (isSideJoin) { // ^ SIDE JOIN: ! (A & B)
29002788

29012789
if (changed) {
29022790
whereString = newWs;
2903-
preparedValueList = newPvl;
2791+
setPreparedValueList(newPvl);
29042792
}
29052793
}
29062794

2907-
String s = StringUtil.isEmpty(whereString, true) ? "" : (hasPrefix ? " WHERE " : "") + whereString;
2908-
2909-
if (s.isEmpty() && RequestMethod.isQueryMethod(method) == false) {
2910-
throw new UnsupportedOperationException("写操作请求必须带条件!!!");
2911-
}
2912-
2913-
return s;
2795+
return whereString;
29142796
}
29152797

2798+
29162799
/**
29172800
* @param key
29182801
* @param value

0 commit comments

Comments
 (0)