Skip to content

Commit 6566821

Browse files
committed
Server:增强 LEFT JOIN 和 RIGHT JOIN ,支持 "join": { "</User/id@": { "@column":"name" } } 定制子查询外层的 column
1 parent b07ea3a commit 6566821

3 files changed

Lines changed: 81 additions & 22 deletions

File tree

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashSet;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Map.Entry;
2728
import java.util.Set;
2829
import java.util.concurrent.TimeoutException;
2930

@@ -701,7 +702,7 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
701702
final String query = request.getString(JSONRequest.KEY_QUERY);
702703
final Integer count = request.getInteger(JSONRequest.KEY_COUNT); //TODO 如果不想用默认数量可以改成 getIntValue(JSONRequest.KEY_COUNT);
703704
final int page = request.getIntValue(JSONRequest.KEY_PAGE);
704-
final String join = request.getString(JSONRequest.KEY_JOIN);
705+
final Object join = request.get(JSONRequest.KEY_JOIN);
705706

706707
int query2;
707708
if (query == null) {
@@ -829,13 +830,33 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
829830
* @return
830831
* @throws Exception
831832
*/
832-
private List<Join> onJoinParse(String join, JSONObject request) throws Exception {
833-
String[] sArr = request == null || request.isEmpty() ? null : StringUtil.split(join);
834-
if (sArr == null || sArr.length <= 0) {
835-
Log.e(TAG, "doJoin sArr == null || sArr.length <= 0 >> return request;");
833+
private List<Join> onJoinParse(Object join, JSONObject request) throws Exception {
834+
JSONObject joinMap = null;
835+
836+
if (join instanceof JSONObject) {
837+
joinMap = (JSONObject) join;
838+
}
839+
else if (join instanceof String) {
840+
String[] sArr = request == null || request.isEmpty() ? null : StringUtil.split((String) join);
841+
if (sArr != null && sArr.length > 0) {
842+
joinMap = new JSONObject();
843+
for (int i = 0; i < sArr.length; i++) {
844+
joinMap.put(sArr[i], new JSONObject());
845+
}
846+
}
847+
}
848+
else if (join != null){
849+
throw new UnsupportedDataTypeException(TAG + ".onJoinParse join 只能是 String 或 JSONObject 类型!");
850+
}
851+
852+
Set<Entry<String, Object>> set = joinMap == null ? null : joinMap.entrySet();
853+
if (set == null || set.isEmpty()) {
854+
Log.e(TAG, "doJoin set == null || set.isEmpty() >> return null;");
836855
return null;
837856
}
838857

858+
859+
839860
List<Join> joinList = new ArrayList<>();
840861

841862

@@ -849,14 +870,19 @@ private List<Join> onJoinParse(String join, JSONObject request) throws Exception
849870
String path;
850871

851872
// List<String> onList = new ArrayList<>();
852-
for (int i = 0; i < sArr.length; i++) {//User/id@
873+
for (Entry<String, Object> e : set) {//User/id@
874+
if (e.getValue() instanceof JSONObject == false) {
875+
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":value 中value不合法!"
876+
+ "必须为 &/Table0/key0,</Table1/key1,... 或 { '&/Table0/key0':{}, '</Table1/key1':{},... } 这种形式!");
877+
}
878+
853879
//分割 /Table/key
854-
path = "" + sArr[i];
880+
path = "" + e.getKey();
855881

856882
int index = path.indexOf("/");
857883
if (index < 0) {
858884
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":value 中value不合法!"
859-
+ "必须为 &/Table0/key0,</Table1/key1,... 这种形式!");
885+
+ "必须为 &/Table0/key0,</Table1/key1,... 或 { '&/Table0/key0':{}, '</Table1/key1':{},... } 这种形式!");
860886
}
861887
String joinType = path.substring(0, index); //& | ! < > ( ) <> () *
862888
// if (StringUtil.isEmpty(joinType, true)) {
@@ -911,6 +937,7 @@ private List<Join> onJoinParse(String join, JSONObject request) throws Exception
911937
j.setTargetKey(targetKey);
912938
j.setKeyAndType(key);
913939
j.setTable(getJoinObject(table, tableObj, key));
940+
j.setOutter((JSONObject) e.getValue());
914941

915942
joinList.add(j);
916943

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public String getSQLTable() {
253253
@Override
254254
public String getTablePath() {
255255
String q = getQuote();
256-
256+
257257
String sqlTable = getSQLTable();
258258
String sch = getSchema();
259259
if (sch == null) { //PostgreSQL 的 pg_class 和 pg_attribute 表好像不属于任何 Schema StringUtil.isEmpty(sch, true)) {
@@ -267,7 +267,7 @@ else if ((PgAttribute.TABLE_NAME.equals(sqlTable) || PgClass.TABLE_NAME.equals(s
267267
sch = DEFAULT_SCHEMA;
268268
}
269269
}
270-
270+
271271
return (StringUtil.isEmpty(sch, true) ? "" : q + sch + q + ".") + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
272272
}
273273
@Override
@@ -536,7 +536,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
536536
throw new IllegalArgumentException("POST请求: 每一个 key:value 中的key都必须是1个单词!");
537537
}
538538
s += ((pfirst ? "" : ",") + getKey(c));
539-
539+
540540
pfirst = false;
541541
}
542542

@@ -546,6 +546,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
546546
boolean isQuery = RequestMethod.isQueryMethod(method);
547547
String joinColumn = "";
548548
if (isQuery && joinList != null) {
549+
SQLConfig ecfg;
549550
SQLConfig cfg;
550551
String c;
551552
boolean first = true;
@@ -554,7 +555,14 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
554555
continue;
555556
}
556557

557-
cfg = j.getJoinConfig();
558+
ecfg = j.getOutterConfig();
559+
if (ecfg != null && ecfg.getColumn() != null) { //优先级更高
560+
cfg = ecfg;
561+
}
562+
else {
563+
cfg = j.getJoinConfig();
564+
}
565+
558566
cfg.setAlias(cfg.getTable());
559567

560568
c = ((AbstractSQLConfig) cfg).getColumnString(true);
@@ -945,12 +953,12 @@ else if (andList == null || andList.contains(key) == false) {
945953
andList = new ArrayList<>();
946954
}
947955
else if (prior && andList.isEmpty() == false) {
948-
956+
949957
String idKey = getIdKey();
950958
String idInKey = idKey + "{}";
951959
String userIdKey = getUserIdKey();
952960
String userIdInKey = userIdKey + "{}";
953-
961+
954962
if (andList.contains(idKey)) {
955963
i ++;
956964
}
@@ -1625,7 +1633,7 @@ public String getContainString(String key, Object[] childs, int type) throws Ill
16251633
if (childs[i] instanceof JSON) {
16261634
throw new IllegalArgumentException(key + "<>:value 中value类型不能为JSON!");
16271635
}
1628-
1636+
16291637
if (DATABASE_POSTGRESQL.equals(getDatabase())) {
16301638
condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR))
16311639
+ getKey(key) + " @> " + getValue(newJSONArray(childs[i])); //operator does not exist: jsonb @> character varying "[" + childs[i] + "]");
@@ -1938,7 +1946,7 @@ public String getJoinString() throws Exception {
19381946
+ quote + tn + quote + "." + quote + j.getTargetKey() + quote;
19391947
jc.setMain(false).setKeyPrefix(true);
19401948

1941-
// preparedValueList.addAll(jc.getPreparedValueList());
1949+
// preparedValueList.addAll(jc.getPreparedValueList());
19421950

19431951
pvl.addAll(jc.getPreparedValueList());
19441952
changed = true;
@@ -1989,9 +1997,9 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
19891997
if (request.isEmpty()) { // User:{} 这种空内容在查询时也有效
19901998
return config; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
19911999
}
1992-
2000+
19932001
String schema = request.getString(KEY_SCHEMA);
1994-
2002+
19952003
String idKey = callback.getIdKey(schema, table);
19962004
String idInKey = idKey + "{}";
19972005
String userIdKey = callback.getUserIdKey(schema, table);
@@ -2295,6 +2303,12 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
22952303
}
22962304

22972305
joinConfig.setMain(false).setKeyPrefix(true);
2306+
2307+
if ("<".equals(j.getJoinType()) || ">".equals(j.getJoinType())) {
2308+
SQLConfig outterConfig = newSQLConfig(method, name, j.getOutter(), null, callback);
2309+
outterConfig.setMain(false).setKeyPrefix(true);
2310+
j.setOutterConfig(outterConfig);
2311+
}
22982312
}
22992313

23002314
//解决 query: 1/2 查数量时报错
@@ -2444,22 +2458,22 @@ public static interface Callback {
24442458
* @return
24452459
*/
24462460
Object newId(RequestMethod method, String table);
2447-
2461+
24482462
/**获取主键名
24492463
* @param schema
24502464
* @param table
24512465
* @return
24522466
*/
24532467
String getIdKey(String schema, String table);
2454-
2468+
24552469
/**获取 User 的主键名
24562470
* @param schema
24572471
* @param table
24582472
* @return
24592473
*/
24602474
String getUserIdKey(String schema, String table);
24612475
}
2462-
2476+
24632477
public static abstract class SimpleCallback implements Callback {
24642478

24652479

@@ -2477,7 +2491,7 @@ public String getIdKey(String schema, String table) {
24772491
public String getUserIdKey(String schema, String table) {
24782492
return KEY_USER_ID;
24792493
}
2480-
2494+
24812495
}
24822496

24832497
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ public class Join {
3636
private String targetName; // Moment
3737
private String targetKey; // userId
3838

39+
private JSONObject outter;
40+
3941
private SQLConfig joinConfig;
4042
private SQLConfig cacheConfig;
43+
private SQLConfig outterConfig;
4144

4245
public String getPath() {
4346
return path;
@@ -103,6 +106,12 @@ public void setTargetKey(String targetKey) {
103106
this.targetKey = targetKey;
104107
}
105108

109+
public JSONObject getOutter() {
110+
return outter;
111+
}
112+
public void setOutter(JSONObject outter) {
113+
this.outter = outter;
114+
}
106115

107116
public SQLConfig getJoinConfig() {
108117
return joinConfig;
@@ -116,6 +125,13 @@ public SQLConfig getCacheConfig() {
116125
public void setCacheConfig(SQLConfig cacheConfig) {
117126
this.cacheConfig = cacheConfig;
118127
}
128+
129+
public SQLConfig getOutterConfig() {
130+
return outterConfig;
131+
}
132+
public void setOutterConfig(SQLConfig outterConfig) {
133+
this.outterConfig = outterConfig;
134+
}
119135

120136

121137
public void setKeyAndType(@NotNull String originKey) throws Exception { //id, id@, id{}@, contactIdList<>@ ...
@@ -157,6 +173,8 @@ public boolean isAppJoin() {
157173
public static boolean isAppJoin(Join j) {
158174
return j != null && j.isAppJoin();
159175
}
176+
177+
160178

161179

162180
}

0 commit comments

Comments
 (0)