| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| 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 | |
@@ -1583,7 +1562,7 @@ public String gainGroupString(boolean hasPrefix) { | |||
| 1583 | 1562 | continue; | |
| 1584 | 1563 | } | |
| 1585 | 1564 | ||
| 1586 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1565 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1587 | 1566 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getGroup() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1588 | 1567 | ||
| 1589 | 1568 | if (cfg != null) { | |
@@ -1598,6 +1577,23 @@ public String gainGroupString(boolean hasPrefix) { | |||
| 1598 | 1577 | first = false; | |
| 1599 | 1578 | } | |
| 1600 | 1579 | } | |
| 1580 | + | ||
| 1581 | + ////先处理左/右关联,内关联忽略 | ||
| 1582 | + //SQLConfig<T, M, L> outerConfig = join.getOuterConfig(); | ||
| 1583 | + //SQLConfig<T, M, L> outerConfig2 = (outerConfig != null && outerConfig.getGroup() != null) || join.isLeftOrRightJoin() ? outerConfig : null; | ||
| 1584 | + // | ||
| 1585 | + //if (outerConfig2 != null) { | ||
| 1586 | + // outerConfig2.setMain(false).setKeyPrefix(true); | ||
| 1587 | + // //if (StringUtil.isEmpty(cfg.getAlias(), true)) { | ||
| 1588 | + // // cfg.setAlias(cfg.getTable()); | ||
| 1589 | + // //} | ||
| 1590 | + // String c = ((AbstractSQLConfig<?, ?, ?>) outerConfig2).gainGroupString(false); | ||
| 1591 | + // | ||
| 1592 | + // if (StringUtil.isNotEmpty(c, true)) { | ||
| 1593 | + // joinGroup += (first ? "" : ", ") + c; | ||
| 1594 | + // first = false; | ||
| 1595 | + // } | ||
| 1596 | + //} | ||
| 1601 | 1597 | } | |
| 1602 | 1598 | } | |
| 1603 | 1599 | ||
@@ -1659,7 +1655,7 @@ public String gainHavingString(boolean hasPrefix) throws Exception { | |||
| 1659 | 1655 | continue; | |
| 1660 | 1656 | } | |
| 1661 | 1657 | ||
| 1662 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1658 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1663 | 1659 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getHaving() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1664 | 1660 | ||
| 1665 | 1661 | if (cfg != null) { | |
@@ -1772,7 +1768,7 @@ public String gainSampleString(boolean hasPrefix) { | |||
| 1772 | 1768 | continue; | |
| 1773 | 1769 | } | |
| 1774 | 1770 | ||
| 1775 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1771 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1776 | 1772 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getSample() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1777 | 1773 | ||
| 1778 | 1774 | if (cfg != null) { | |
@@ -1842,7 +1838,7 @@ public String gainLatestString(boolean hasPrefix) { | |||
| 1842 | 1838 | continue; | |
| 1843 | 1839 | } | |
| 1844 | 1840 | ||
| 1845 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1841 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1846 | 1842 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getLatest() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1847 | 1843 | ||
| 1848 | 1844 | if (cfg != null) { | |
@@ -1907,7 +1903,7 @@ public String gainPartitionString(boolean hasPrefix) { | |||
| 1907 | 1903 | continue; | |
| 1908 | 1904 | } | |
| 1909 | 1905 | ||
| 1910 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1906 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1911 | 1907 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getPartition() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1912 | 1908 | ||
| 1913 | 1909 | if (cfg != null) { | |
@@ -1972,7 +1968,7 @@ public String gainFillString(boolean hasPrefix) { | |||
| 1972 | 1968 | continue; | |
| 1973 | 1969 | } | |
| 1974 | 1970 | ||
| 1975 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 1971 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 1976 | 1972 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getFill() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 1977 | 1973 | ||
| 1978 | 1974 | if (cfg != null) { | |
@@ -2038,14 +2034,15 @@ public AbstractSQLConfig<T, M, L> setOrder(String order) { | |||
| 2038 | 2034 | public String gainOrderString(boolean hasPrefix) { | |
| 2039 | 2035 | //加上子表的 order | |
| 2040 | 2036 | String joinOrder = ""; | |
| 2037 | + String joinOuterOrder = ""; | ||
| 2041 | 2038 | if (joinList != null) { | |
| 2042 | 2039 | boolean first = true; | |
| 2043 | 2040 | for (Join<T, M, L> join : joinList) { | |
| 2044 | 2041 | if (join.isAppJoin()) { | |
| 2045 | 2042 | continue; | |
| 2046 | 2043 | } | |
| 2047 | 2044 | ||
| 2048 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 2045 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 2049 | 2046 | SQLConfig<T, M, L> cfg = (ocfg != null && ocfg.getOrder() != null) || join.isLeftOrRightJoin() ? ocfg : join.getJoinConfig(); | |
| 2050 | 2047 | ||
| 2051 | 2048 | if (cfg != null) { | |
@@ -2354,7 +2351,7 @@ public String gainColumnString(boolean inSQLJoin) throws Exception { | |||
| 2354 | 2351 | continue; | |
| 2355 | 2352 | } | |
| 2356 | 2353 | ||
| 2357 | - SQLConfig<T, M, L> ocfg = join.getOuterConfig(); | ||
| 2354 | + SQLConfig<T, M, L> ocfg = join.getOnConfig(); | ||
| 2358 | 2355 | boolean isEmpty = ocfg == null || ocfg.getColumn() == null; | |
| 2359 | 2356 | boolean isLeftOrRightJoin = join.isLeftOrRightJoin(); | |
| 2360 | 2357 | ||
@@ -3738,8 +3735,9 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3738 | 3735 | List<Object> pvl = new ArrayList<>(getPreparedValueList()); | |
| 3739 | 3736 | ||
| 3740 | 3737 | SQLConfig<T, M, L> jc; | |
| 3738 | + SQLConfig<T, M, L> outerConfig; | ||
| 3741 | 3739 | String js; | |
| 3742 | - | ||
| 3740 | + boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3743 | 3741 | boolean changed = false; | |
| 3744 | 3742 | // 各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样? | |
| 3745 | 3743 | for (Join<T, M, L> j : joinList) { | |
@@ -3750,6 +3748,24 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3750 | 3748 | case "@": // APP JOIN | |
| 3751 | 3749 | case "<": // LEFT JOIN | |
| 3752 | 3750 | case ">": // RIGHT JOIN | |
| 3751 | + outerConfig = j.getOuterConfig(); | ||
| 3752 | + if (outerConfig == null){ | ||
| 3753 | + break; | ||
| 3754 | + } | ||
| 3755 | + boolean isMain1 = outerConfig.isMain(); | ||
| 3756 | + outerConfig.setMain(false).setPrepared(isPrepared()).setPreparedValueList(new ArrayList<Object>()); | ||
| 3757 | + String outerWhere = outerConfig.gainWhereString(false); | ||
| 3758 | + | ||
| 3759 | + int logic1 = Logic.getType(jt); | ||
| 3760 | + newWs += " ( " | ||
| 3761 | + + gainCondition( | ||
| 3762 | + Logic.isNot(logic1), | ||
| 3763 | + ws | ||
| 3764 | + + ( isWsEmpty ? "" : (Logic.isAnd(logic1) ? AND : OR) ) | ||
| 3765 | + + " ( " + outerWhere + " ) " | ||
| 3766 | + ) | ||
| 3767 | + + " ) "; | ||
| 3768 | + changed = true; | ||
| 3753 | 3769 | break; | |
| 3754 | 3770 | ||
| 3755 | 3771 | case "&": // INNER JOIN: A & B | |
@@ -3770,7 +3786,7 @@ protected String concatJoinWhereString(String whereString) throws Exception { | |||
| 3770 | 3786 | boolean isSideJoin = "^".equals(jt); | |
| 3771 | 3787 | boolean isAntiJoin = "(".equals(jt); | |
| 3772 | 3788 | boolean isForeignJoin = ")".equals(jt); | |
| 3773 | - boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3789 | + //boolean isWsEmpty = StringUtil.isEmpty(ws, true); | ||
| 3774 | 3790 | ||
| 3775 | 3791 | if (isWsEmpty) { | |
| 3776 | 3792 | if (isOuterJoin) { // ! OUTER JOIN: ! (A | B) | |
@@ -5211,7 +5227,7 @@ public String gainJoinString() throws Exception { | |||
| 5211 | 5227 | ); | |
| 5212 | 5228 | } | |
| 5213 | 5229 | ||
| 5214 | - SQLConfig<T, M, L> oc = j.getOuterConfig(); | ||
| 5230 | + SQLConfig<T, M, L> oc = j.getOnConfig(); | ||
| 5215 | 5231 | String ow = null; | |
| 5216 | 5232 | if (oc != null) { | |
| 5217 | 5233 | oc.setPrepared(isPrepared()); | |
@@ -6314,13 +6330,22 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) { | |||
| 6314 | 6330 | ||
| 6315 | 6331 | joinConfig.setMain(false).setKeyPrefix(true); | |
| 6316 | 6332 | ||
| 6333 | + if (join.getOn() != null) { | ||
| 6334 | + SQLConfig<T, M, L> onConfig = newSQLConfig(method, table, alias, join.getOn(), null, false, callback); | ||
| 6335 | + onConfig.setMain(false) | ||
| 6336 | + .setKeyPrefix(true) | ||
| 6337 | + .setDatabase(joinConfig.getDatabase()) | ||
| 6338 | + .setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致 | ||
| 6339 | + | ||
| 6340 | + join.setOnConfig(onConfig); | ||
| 6341 | + } | ||
| 6342 | + | ||
| 6317 | 6343 | if (join.getOuter() != null) { | |
| 6318 | 6344 | SQLConfig<T, M, L> outerConfig = newSQLConfig(method, table, alias, join.getOuter(), null, false, callback); | |
| 6319 | 6345 | outerConfig.setMain(false) | |
| 6320 | 6346 | .setKeyPrefix(true) | |
| 6321 | 6347 | .setDatabase(joinConfig.getDatabase()) | |
| 6322 | 6348 | .setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致 | |
| 6323 | - | ||
| 6324 | 6349 | join.setOuterConfig(outerConfig); | |
| 6325 | 6350 | } | |
| 6326 | 6351 | } | |
| 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