| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 47670c2 commit 509d245
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,8 +32,6 @@ | |||
| 32 | 32 | */ | |
| 33 | 33 | public class DemoSQLConfig extends AbstractSQLConfig { | |
| 34 | 34 | ||
| 35 | - public static final String DATABASE_MYSQL = "MySQL"; | ||
| 36 | - public static final String DATABASE_POSTGRESQL = "PostgreSQL"; | ||
| 37 | 35 | ||
| 38 | 36 | //表名映射,隐藏真实表名,对安全要求很高的表可以这么做 | |
| 39 | 37 | static { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,7 @@ | |||
| 50 | 50 | import com.alibaba.fastjson.JSONArray; | |
| 51 | 51 | import com.alibaba.fastjson.JSONObject; | |
| 52 | 52 | import com.alibaba.fastjson.annotation.JSONField; | |
| 53 | + import com.sun.xml.internal.ws.util.StringUtils; | ||
| 53 | 54 | ||
| 54 | 55 | import zuo.biao.apijson.Log; | |
| 55 | 56 | import zuo.biao.apijson.RequestMethod; | |
@@ -191,7 +192,7 @@ public SQLConfig setDatabase(String database) { | |||
| 191 | 192 | this.database = database; | |
| 192 | 193 | return this; | |
| 193 | 194 | } | |
| 194 | - | ||
| 195 | + | ||
| 195 | 196 | @Override | |
| 196 | 197 | public String getSchema() { | |
| 197 | 198 | String sqlTable = getSQLTable(); | |
@@ -611,7 +612,7 @@ public String getColumnString() throws Exception { | |||
| 611 | 612 | } | |
| 612 | 613 | ||
| 613 | 614 | } | |
| 614 | - | ||
| 615 | + | ||
| 615 | 616 | c = StringUtil.getString(keys); | |
| 616 | 617 | ||
| 617 | 618 | return (c.contains(":") == false ? c : c.replaceAll(":", " AS ")) + (StringUtil.isEmpty(joinColumn, true) ? "" : ", " + joinColumn);//不能在这里改,后续还要用到: | |
@@ -959,6 +960,12 @@ else if ("!".equals(ce.getKey())) { | |||
| 959 | 960 | ||
| 960 | 961 | if (joinList != null) { | |
| 961 | 962 | ||
| 963 | + String newWs = ""; | ||
| 964 | + String ws = "" + whereString; | ||
| 965 | + | ||
| 966 | + List<Object> newPvl = new ArrayList<>(); | ||
| 967 | + List<Object> pvl = new ArrayList<>(preparedValueList); | ||
| 968 | + | ||
| 962 | 969 | SQLConfig jc; | |
| 963 | 970 | String js; | |
| 964 | 971 | //各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样? | |
@@ -968,8 +975,8 @@ else if ("!".equals(ce.getKey())) { | |||
| 968 | 975 | case "|": //不支持 <>, [] ,避免太多符号 | |
| 969 | 976 | case "&": | |
| 970 | 977 | case "!": | |
| 971 | - logic = Logic.getType(j.getJoinType()); | ||
| 972 | - | ||
| 978 | + case "^": | ||
| 979 | + case "*": | ||
| 973 | 980 | jc = j.getJoinConfig(); | |
| 974 | 981 | boolean isMain = jc.isMain(); | |
| 975 | 982 | jc.setMain(false).setPrepared(isPrepared()).setPreparedValueList(new ArrayList<Object>()); | |
@@ -980,21 +987,44 @@ else if ("!".equals(ce.getKey())) { | |||
| 980 | 987 | continue; | |
| 981 | 988 | } | |
| 982 | 989 | ||
| 983 | - whereString = " ( " | ||
| 984 | - + getCondition( | ||
| 985 | - Logic.isNot(logic), | ||
| 986 | - whereString | ||
| 987 | - + ( StringUtil.isEmpty(whereString, true) ? "" : (Logic.isAnd(logic) ? AND : OR) ) | ||
| 988 | - + " ( " + js + " ) " | ||
| 989 | - ) | ||
| 990 | - + " ) "; | ||
| 990 | + if (StringUtil.isEmpty(newWs, true) == false) { | ||
| 991 | + newWs += AND; | ||
| 992 | + } | ||
| 993 | + | ||
| 994 | + if ("^".equals(j.getJoinType())) { // (A & ! B) | (B & ! A) | ||
| 995 | + newWs += " ( ( " + ws + ( StringUtil.isEmpty(ws, true) ? "" : AND + NOT ) + " ( " + js + " ) ) " | ||
| 996 | + + OR | ||
| 997 | + + " ( " + js + AND + NOT + " ( " + ws + " ) ) ) "; | ||
| 998 | + | ||
| 999 | + newPvl.addAll(pvl); | ||
| 1000 | + newPvl.addAll(jc.getPreparedValueList()); | ||
| 1001 | + newPvl.addAll(jc.getPreparedValueList()); | ||
| 1002 | + newPvl.addAll(pvl); | ||
| 1003 | + } | ||
| 1004 | + else { | ||
| 1005 | + logic = Logic.getType(j.getJoinType()); | ||
| 1006 | + | ||
| 1007 | + newWs += " ( " | ||
| 1008 | + + getCondition( | ||
| 1009 | + Logic.isNot(logic), | ||
| 1010 | + ws | ||
| 1011 | + + ( StringUtil.isEmpty(ws, true) ? "" : (Logic.isAnd(logic) ? AND : OR) ) | ||
| 1012 | + + " ( " + js + " ) " | ||
| 1013 | + ) | ||
| 1014 | + + " ) "; | ||
| 1015 | + | ||
| 1016 | + newPvl.addAll(pvl); | ||
| 1017 | + newPvl.addAll(jc.getPreparedValueList()); | ||
| 1018 | + } | ||
| 991 | 1019 | ||
| 992 | - preparedValueList.addAll(jc.getPreparedValueList()); | ||
| 993 | 1020 | break; | |
| 994 | 1021 | //可能 LEFT JOIN 和 INNER JOIN 同时存在 default: | |
| 995 | 1022 | // throw new UnsupportedOperationException(""); | |
| 996 | 1023 | } | |
| 997 | 1024 | } | |
| 1025 | + | ||
| 1026 | + whereString = newWs; | ||
| 1027 | + preparedValueList = newPvl; | ||
| 998 | 1028 | } | |
| 999 | 1029 | ||
| 1000 | 1030 | String s = whereString.isEmpty() ? "" : (hasPrefix ? " WHERE " : "") + whereString; | |
@@ -1581,8 +1611,10 @@ public String getJoinString() throws Exception { | |||
| 1581 | 1611 | case "|": //不支持 <>, [] ,避免太多符号 | |
| 1582 | 1612 | case "&": | |
| 1583 | 1613 | case "!": | |
| 1584 | - sql = " INNER JOIN " + jc.getTablePath() + " ON " + jc.getTable() + "." + j.getKey() + " = " | ||
| 1585 | - + j.getTargetName() + "." + j.getTargetKey(); | ||
| 1614 | + case "^": | ||
| 1615 | + case "*": | ||
| 1616 | + sql = ("*".equals(j.getJoinType()) ? " CROSS JOIN " : " INNER JOIN ") + jc.getTablePath() | ||
| 1617 | + + " ON " + jc.getTable() + "." + j.getKey() + " = " + j.getTargetName() + "." + j.getTargetKey(); | ||
| 1586 | 1618 | break; | |
| 1587 | 1619 | case "<": | |
| 1588 | 1620 | case ">": | |
@@ -1595,7 +1627,7 @@ public String getJoinString() throws Exception { | |||
| 1595 | 1627 | preparedValueList.addAll(jc.getPreparedValueList()); | |
| 1596 | 1628 | break; | |
| 1597 | 1629 | default: | |
| 1598 | - throw new UnsupportedOperationException("服务器内部错误:不支持JOIN类型 " + type + " !"); | ||
| 1630 | + throw new UnsupportedOperationException("服务器内部错误:不支持JOIN类型 " + j.getJoinType() + " !"); | ||
| 1599 | 1631 | } | |
| 1600 | 1632 | ||
| 1601 | 1633 | joinOns += " \n " + sql; | |
@@ -1882,11 +1914,11 @@ LEFT JOIN ( SELECT count(*) AS count FROM sys.Comment ) AS Comment ON Comment.m | |||
| 1882 | 1914 | if (RequestMethod.isHeadMethod(method, true)) { | |
| 1883 | 1915 | joinConfig.setMethod(GET); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId | |
| 1884 | 1916 | joinConfig.setColumn(j.getKey()); //优化性能,不取非必要的字段 | |
| 1885 | - | ||
| 1917 | + | ||
| 1886 | 1918 | cacheConfig.setMethod(GET); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId | |
| 1887 | 1919 | cacheConfig.setColumn(j.getKey()); //优化性能,不取非必要的字段 | |
| 1888 | 1920 | } | |
| 1889 | - | ||
| 1921 | + | ||
| 1890 | 1922 | j.setJoinConfig(joinConfig); | |
| 1891 | 1923 | j.setCacheConfig(cacheConfig); | |
| 1892 | 1924 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ | |||
| 21 | 21 | */ | |
| 22 | 22 | public class Join { | |
| 23 | 23 | ||
| 24 | - private String joinType; // "&" - INNER, "|" - FULL, "!" - OUTTER, "<" - LEFT, ">" - RIGHT | ||
| 24 | + private String joinType; // "&" - INNER, "|" - FULL, "!" - OUTTER, "<" - LEFT, ">" - RIGHT, "*" - CROSS, "^" - SIDE | ||
| 25 | 25 | private String relateType; // "" - 一对一, "{}" - 一对多, "<>" - 多对一 | |
| 26 | 26 | private JSONObject table; // { "id@":"/Moment/userId" } | |
| 27 | 27 | private String name; //User | |
@@ -114,23 +114,4 @@ else if (originKey.endsWith("<>")) { | |||
| 114 | 114 | } | |
| 115 | 115 | } | |
| 116 | 116 | ||
| 117 | - public String getJoinTypeName() throws Exception { | ||
| 118 | - return getJoinTypeName(joinType); | ||
| 119 | - } | ||
| 120 | - public static String getJoinTypeName(@NotNull String type) throws Exception { | ||
| 121 | - switch (type) { | ||
| 122 | - case "": | ||
| 123 | - case "&": | ||
| 124 | - case "|": //不支持 <>, [] ,避免太多符号 | ||
| 125 | - case "!": | ||
| 126 | - return " INNER JOIN "; //TODO & | ! 通过 WHERE NOT( {B.where} OR {B.where} ) 解决 | ||
| 127 | - case "<": | ||
| 128 | - return " LEFT JOIN "; | ||
| 129 | - case ">": | ||
| 130 | - return " RIGIHT JOIN "; | ||
| 131 | - default: | ||
| 132 | - throw new UnsupportedOperationException("服务器内部错误:不支持JOIN类型 " + type + " !"); | ||
| 133 | - } | ||
| 134 | - } | ||
| 135 | - | ||
| 136 | 117 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,13 +25,16 @@ | |||
| 25 | 25 | */ | |
| 26 | 26 | public interface SQLConfig { | |
| 27 | 27 | ||
| 28 | + String DATABASE_MYSQL = "MySQL"; | ||
| 29 | + String DATABASE_POSTGRESQL = "PostgreSQL"; | ||
| 30 | + | ||
| 28 | 31 | String SCHEMA_INFORMATION = "`information_schema`"; | |
| 29 | 32 | String TABLE_SCHEMA = "`table_schema`"; | |
| 30 | 33 | String TABLE_NAME = "`table_name`"; | |
| 31 | 34 | ||
| 32 | - public static final int TYPE_CHILD = 0; | ||
| 33 | - public static final int TYPE_ITEM = 1; | ||
| 34 | - public static final int TYPE_ITEM_CHILD_0 = 2; | ||
| 35 | + int TYPE_CHILD = 0; | ||
| 36 | + int TYPE_ITEM = 1; | ||
| 37 | + int TYPE_ITEM_CHILD_0 = 2; | ||
| 35 | 38 | ||
| 36 | 39 | /**获取数据库地址 | |
| 37 | 40 | * @return | |
| Back | FazBrowse Home | New Git URL |
0 commit comments