| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7314336 commit 3c21439
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1563,7 +1563,39 @@ else if (join != null){ | |||
| 1563 | 1563 | throw new UnsupportedDataTypeException(TAG + ".onJoinParse join 只能是 String 或 Map<String, Object> 类型!"); | |
| 1564 | 1564 | } | |
| 1565 | 1565 | ||
| 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 | + ; | ||
| 1567 | 1599 | if (set == null || set.isEmpty()) { | |
| 1568 | 1600 | Log.e(TAG, "onJoinParse set == null || set.isEmpty() >> return null;"); | |
| 1569 | 1601 | return null; | |
@@ -1759,9 +1791,15 @@ else if (join != null){ | |||
| 1759 | 1791 | j.setAlias(alias); | |
| 1760 | 1792 | ||
| 1761 | 1793 | M outerObj = (M) JSON.createJSONObject((Map<String, Object>) outer); | |
| 1762 | - j.setOuter(outerObj); | ||
| 1794 | + j.setOn(outerObj); | ||
| 1763 | 1795 | j.setRequest(requestObj); | |
| 1764 | 1796 | ||
| 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 | + | ||
| 1765 | 1803 | if (arrKey != null) { | |
| 1766 | 1804 | Integer count = getInteger(parentPathObj, apijson.JSONRequest.KEY_COUNT); | |
| 1767 | 1805 | j.setCount(count == null ? getDefaultQueryCount() : count); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,42 +5,21 @@ | |||
| 5 | 5 | ||
| 6 | 6 | package apijson.orm; | |
| 7 | 7 | ||
| 8 | - import java.util.*; | ||
| 9 | - import java.util.Map.Entry; | ||
| 10 | - import java.util.regex.Pattern; | ||
| 11 | - | ||
| 12 | 8 | import apijson.*; | |
| 13 | 9 | import apijson.orm.Join.On; | |
| 14 | 10 | import apijson.orm.exception.NotExistException; | |
| 15 | 11 | 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; | ||
| 32 | 17 | ||
| 33 | 18 | import static apijson.JSON.getBoolean; | |
| 34 | 19 | import static apijson.JSON.getString; | |
| 35 | 20 | 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.*; | ||
| 44 | 23 | ||
| 45 | 24 | /**config sql for JSON Request | |
| 46 | 25 | * @author Lemon | |
@@ -1574,7 +1553,7 @@ public String gainGroupString(boolean hasPrefix) { | |||
| 1574 | 1553 | continue; | |
| 1575 | 1554 | } | |
| 1576 | 1555 | ||
| 1577 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1556 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1578 | 1557 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getGroup() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1579 | 1558 | ||
| 1580 | 1559 | if (cfg != null) { | |
@@ -1589,6 +1568,23 @@ public String gainGroupString(boolean hasPrefix) { | |||
| 1589 | 1568 | first = false; | |
| 1590 | 1569 | } | |
| 1591 | 1570 | } | |
| 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 | + //} | ||
| 1592 | 1588 | } | |
| 1593 | 1589 | } | |
| 1594 | 1590 | ||
@@ -1650,7 +1646,7 @@ public String gainHavingString(boolean hasPrefix) throws Exception { | |||
| 1650 | 1646 | continue; | |
| 1651 | 1647 | } | |
| 1652 | 1648 | ||
| 1653 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1649 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1654 | 1650 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getHaving() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1655 | 1651 | ||
| 1656 | 1652 | if (cfg != null) { | |
@@ -1763,7 +1759,7 @@ public String gainSampleString(boolean hasPrefix) { | |||
| 1763 | 1759 | continue; | |
| 1764 | 1760 | } | |
| 1765 | 1761 | ||
| 1766 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1762 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1767 | 1763 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getSample() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1768 | 1764 | ||
| 1769 | 1765 | if (cfg != null) { | |
@@ -1833,7 +1829,7 @@ public String gainLatestString(boolean hasPrefix) { | |||
| 1833 | 1829 | continue; | |
| 1834 | 1830 | } | |
| 1835 | 1831 | ||
| 1836 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1832 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1837 | 1833 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getLatest() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1838 | 1834 | ||
| 1839 | 1835 | if (cfg != null) { | |
@@ -1898,7 +1894,7 @@ public String gainPartitionString(boolean hasPrefix) { | |||
| 1898 | 1894 | continue; | |
| 1899 | 1895 | } | |
| 1900 | 1896 | ||
| 1901 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1897 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1902 | 1898 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getPartition() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1903 | 1899 | ||
| 1904 | 1900 | if (cfg != null) { | |
@@ -1963,7 +1959,7 @@ public String gainFillString(boolean hasPrefix) { | |||
| 1963 | 1959 | continue; | |
| 1964 | 1960 | } | |
| 1965 | 1961 | ||
| 1966 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1962 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1967 | 1963 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getFill() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1968 | 1964 | ||
| 1969 | 1965 | if (cfg != null) { | |
@@ -2029,14 +2025,15 @@ public AbstractSQLConfig<T, M, L> setOrder(String order) { | |||
| 2029 | 2025 | public String gainOrderString(boolean hasPrefix) { | |
| 2030 | 2026 | //加上子表的 order | |
| 2031 | 2027 | String joinOrder = ""; | |
| 2028 | + String joinOuterOrder = ""; | ||
| 2032 | 2029 | if (joinList != null) { | |
| 2033 | 2030 | boolean first = true; | |
| 2034 | 2031 | for (Join<T, M, L> join : joinList) { | |
| 2035 | 2032 | if (join.isAppJoin()) { | |
| 2036 | 2033 | continue; | |
| 2037 | 2034 | } | |
| 2038 | 2035 | ||
| 2039 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 2036 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 2040 | 2037 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getOrder() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 2041 | 2038 | ||
| 2042 | 2039 | if (cfg != null) { | |
@@ -2345,7 +2342,7 @@ public String gainColumnString(boolean inSQLJoin) throws Exception { | |||
| 2345 | 2342 | continue; | |
| 2346 | 2343 | } | |
| 2347 | 2344 | ||
| 2348 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 2345 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 2349 | 2346 | boolean isEmpty = ocfg == null || ocfg.getColumn() == null; | |
| 2350 | 2347 | boolean isLeftOrRightJoin = join.isLeftOrRightJoin(); | |
| 2351 | 2348 | ||
@@ -3729,8 +3726,9 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3729 | 3726 | List<Object> pvl = new ArrayList<>(getPreparedValueList()); | |
| 3730 | 3727 | ||
| 3731 | 3728 | SQLConfig<T, M, L> jc; | |
| 3729 | + SQLConfig<T, M, L> outerConfig; | ||
| 3732 | 3730 | String js; | |
| 3733 | - | ||
| 3731 | + boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3734 | 3732 | boolean changed = false; | |
| 3735 | 3733 | // 各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样? | |
| 3736 | 3734 | for (Join<T, M, L> j : joinList) { | |
@@ -3741,6 +3739,24 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3741 | 3739 | case "@": // APP JOIN | |
| 3742 | 3740 | case "<": // LEFT JOIN | |
| 3743 | 3741 | 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; | ||
| 3744 | 3760 | break; | |
| 3745 | 3761 | ||
| 3746 | 3762 | case "&": // INNER JOIN: A & B | |
@@ -3761,7 +3777,7 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3761 | 3777 | boolean isSideJoin = "^".equals(jt); | |
| 3762 | 3778 | boolean isAntiJoin = "(".equals(jt); | |
| 3763 | 3779 | boolean isForeignJoin = ")".equals(jt); | |
| 3764 | - boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3780 | + //boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3765 | 3781 | ||
| 3766 | 3782 | if (isWsEmpty) { | |
| 3767 | 3783 | if (isOuterJoin) { // ! OUTER JOIN: ! (A | B) | |
@@ -5202,7 +5218,7 @@ public String gainJoinString() throws Exception { | |||
| 5202 | 5218 | ); | |
| 5203 | 5219 | } | |
| 5204 | 5220 | ||
| 5205 | - SQLConfig<T, M, L> oc = j.getOuterConfig(); | ||
| 5221 | + SQLConfig<T, M, L> oc = j.getOnConfig(); | ||
| 5206 | 5222 | String ow = null; | |
| 5207 | 5223 | if (oc != null) { | |
| 5208 | 5224 | oc.setPrepared(isPrepared()); | |
@@ -6305,13 +6321,22 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) { | |||
| 6305 | 6321 | ||
| 6306 | 6322 | joinConfig.setMain(false).setKeyPrefix(true); | |
| 6307 | 6323 | ||
| 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 | + | ||
| 6308 | 6334 | if (join.getOuter() != null) { | |
| 6309 | 6335 | SQLConfig<T, M, L> outerConfig = newSQLConfig(method, table, alias, join.getOuter(), null, false, callback); | |
| 6310 | 6336 | outerConfig.setMain(false) | |
| 6311 | 6337 | .setKeyPrefix(true) | |
| 6312 | 6338 | .setDatabase(joinConfig.getDatabase()) | |
| 6313 | 6339 | .setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致 | |
| 6314 | - | ||
| 6315 | 6340 | join.setOuterConfig(outerConfig); | |
| 6316 | 6341 | } | |
| 6317 | 6342 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,12 +25,14 @@ public class Join<T, M extends Map<String, Object>, L extends List<Object>> { | |||
| 25 | 25 | private List<On> onList; // ON User.id = Moment.userId AND ... | |
| 26 | 26 | ||
| 27 | 27 | 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 对应值 | ||
| 29 | 29 | ||
| 30 | + private M outer; // "join": { "</User": { "key2": value2, "@column": "key2,key3","@group": "key2+" }} | ||
| 30 | 31 | private SQLConfig<T, M, L> joinConfig; | |
| 31 | 32 | private SQLConfig<T, M, L> cacheConfig; | |
| 32 | - private SQLConfig<T, M, L> outerConfig; | ||
| 33 | + private SQLConfig<T, M, L> onConfig; | ||
| 33 | 34 | ||
| 35 | + private SQLConfig<T, M, L> outerConfig; | ||
| 34 | 36 | ||
| 35 | 37 | public String getPath() { | |
| 36 | 38 | return path; | |
@@ -78,13 +80,29 @@ public M getRequest() { | |||
| 78 | 80 | public void setRequest(M request) { | |
| 79 | 81 | this.request = request; | |
| 80 | 82 | } | |
| 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; | ||
| 83 | 88 | } | |
| 89 | + | ||
| 84 | 90 | public void setOuter(M outer) { | |
| 85 | 91 | this.outer = outer; | |
| 86 | 92 | } | |
| 87 | 93 | ||
| 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 | + | ||
| 88 | 106 | public SQLConfig<T, M, L> getJoinConfig() { | |
| 89 | 107 | return joinConfig; | |
| 90 | 108 | } | |
@@ -97,11 +115,11 @@ public SQLConfig<T, M, L> getCacheConfig() { | |||
| 97 | 115 | public void setCacheConfig(SQLConfig<T, M, L> cacheConfig) { | |
| 98 | 116 | this.cacheConfig = cacheConfig; | |
| 99 | 117 | } | |
| 100 | - public SQLConfig<T, M, L> getOuterConfig() { | ||
| 101 | - return outerConfig; | ||
| 118 | + public SQLConfig<T, M, L> getOnConfig() { | ||
| 119 | + return onConfig; | ||
| 102 | 120 | } | |
| 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; | ||
| 105 | 123 | } | |
| 106 | 124 | ||
| 107 | 125 | public boolean isOne2One() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments