Skip to content

Commit d567d7b

Browse files
committed
Server:完善子查询的关键词
1 parent 931dfef commit d567d7b

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,25 @@ public JSONRequest setFormat(Boolean format) {
8888
public static final int QUERY_TOTAL = 1;
8989
public static final int QUERY_ALL = 2;
9090

91+
public static final String SUBQUERY_RANGE_ALL = "ALL";
92+
public static final String SUBQUERY_RANGE_ANY = "ANY";
93+
9194
public static final String KEY_QUERY = "query";
9295
public static final String KEY_COUNT = "count";
9396
public static final String KEY_PAGE = "page";
9497
public static final String KEY_JOIN = "join";
98+
public static final String KEY_SUBQUERY_RANGE = "range";
99+
public static final String KEY_SUBQUERY_FROM = "from";
95100

96101
public static final List<String> ARRAY_KEY_LIST;
97102
static {
98103
ARRAY_KEY_LIST = new ArrayList<String>();
99104
ARRAY_KEY_LIST.add(KEY_QUERY);
100105
ARRAY_KEY_LIST.add(KEY_COUNT);
101106
ARRAY_KEY_LIST.add(KEY_PAGE);
107+
ARRAY_KEY_LIST.add(KEY_JOIN);
108+
ARRAY_KEY_LIST.add(KEY_SUBQUERY_RANGE);
109+
ARRAY_KEY_LIST.add(KEY_SUBQUERY_FROM);
102110
}
103111

104112
/**set what to query in Array layer
@@ -125,6 +133,33 @@ public JSONRequest setCount(int count) {
125133
public JSONRequest setPage(int page) {
126134
return puts(KEY_PAGE, page);
127135
}
136+
137+
/**set joins of Main Table and it's Vice Tables in Array layer
138+
* @param joins "@/User/id@", "&/User/id@,>/Comment/momentId@" ...
139+
* @return
140+
*/
141+
public JSONRequest setJoin(String... joins) {
142+
return puts(KEY_JOIN, StringUtil.getString(joins));
143+
}
144+
145+
/**set range for Subquery
146+
* @param range
147+
* @return
148+
* @see {@link #SUBQUERY_RANGE_ALL}
149+
* @see {@link #SUBQUERY_RANGE_ANY}
150+
*/
151+
public JSONRequest setSubqueryRange(String range) {
152+
return puts(KEY_SUBQUERY_RANGE, range);
153+
}
154+
155+
/**set from for Subquery
156+
* @param range
157+
* @return
158+
*/
159+
public JSONRequest setSubqueryFrom(String from) {
160+
return puts(KEY_SUBQUERY_FROM, from);
161+
}
162+
128163
//array object >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
129164

130165

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,23 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
333333
String replaceKey = key.substring(0, key.length() - 1);//key{}@ getRealKey
334334

335335
JSONObject subquery = (JSONObject) value;
336-
String range = subquery.getString("range");
337-
if (range != null && "ANY".equals(range) == false && "ALL".equals(range) == false) {
338-
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ range:value } 中 value 只能为 [ANY, ALL] 中的一个!");
336+
String range = subquery.getString(JSONRequest.KEY_SUBQUERY_RANGE);
337+
if (range != null && JSONRequest.SUBQUERY_RANGE_ALL.equals(range) == false && JSONRequest.SUBQUERY_RANGE_ANY.equals(range) == false) {
338+
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ range:value } 中 value 只能为 [" + JSONRequest.SUBQUERY_RANGE_ALL + ", " + JSONRequest.SUBQUERY_RANGE_ANY + "] 中的一个!");
339339
}
340340

341341

342342
JSONArray arr = parser.onArrayParse(subquery, AbstractParser.getAbsPath(path, replaceKey), "[]", true);
343343

344344
JSONObject obj = arr == null || arr.isEmpty() ? null : arr.getJSONObject(0);
345+
if (obj == null) {
346+
throw new Exception("服务器内部错误,解析子查询 " + path + "/" + key + ":{ } 为 Subquery 对象失败!");
347+
}
345348

346-
String from = subquery.getString("from");
347-
JSONObject arrObj = obj.getJSONObject(from);
349+
String from = subquery.getString(JSONRequest.KEY_SUBQUERY_FROM);
350+
JSONObject arrObj = from == null ? null : obj.getJSONObject(from);
348351
if (arrObj == null) {
349-
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ from:value } 中 value 对应的主表对象不存在!");
352+
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ from:value } 中 value 对应的主表对象 " + from + ":{} 不存在!");
350353
}
351354
//
352355
SQLConfig cfg = (SQLConfig) arrObj.get(AbstractParser.KEY_CONFIG);

0 commit comments

Comments
 (0)