Skip to content

Commit e5001aa

Browse files
committed
解决 JOIN 报错 JSON 转换类型不匹配
1 parent f11c3a1 commit e5001aa

10 files changed

Lines changed: 28 additions & 186 deletions

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Lemon
2323
*/
2424
public abstract class AbstractFunctionParser<T, M extends Map<String, Object>, L extends List<Object>>
25-
implements FunctionParser<T, M, L> { //, JSONParser<M, L> {
25+
implements FunctionParser<T, M, L> {
2626
private static final String TAG = "AbstractFunctionParser";
2727

2828
/**是否解析参数 key 的对应的值,不用手动编码 curObj.getString(key)

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Lemon
3131
*/
3232
public abstract class AbstractObjectParser<T, M extends Map<String, Object>, L extends List<Object>>
33-
implements ObjectParser<T, M, L> { //, JSONParser<M, L> {
33+
implements ObjectParser<T, M, L> {
3434
private static final String TAG = "AbstractObjectParser";
3535

3636
@NotNull
@@ -1320,5 +1320,4 @@ public Map<String, M> getChildMap() {
13201320
return childMap;
13211321
}
13221322

1323-
13241323
}

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ public M onObjectParse(final M request, String parentPath, String name
12231223
page += min;
12241224
max += min;
12251225

1226-
Map<String, Object> pagination = new LinkedHashMap<String, Object>();
1226+
M pagination = (M) JSON.createJSONObject();
12271227
Object explain = rp.get(JSONResponse.KEY_EXPLAIN);
12281228
if (explain instanceof Map<?, ?>) {
12291229
pagination.put(JSONResponse.KEY_EXPLAIN, explain);
@@ -1509,7 +1509,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
15091509
* @return
15101510
* @throws Exception
15111511
*/
1512-
private List<Join<T, M, L>> onJoinParse(Object join, Map<String, Object> request) throws Exception {
1512+
private List<Join<T, M, L>> onJoinParse(Object join, M request) throws Exception {
15131513
Map<String, Object> joinMap = null;
15141514

15151515
if (join instanceof Map<?, ?>) {
@@ -1584,8 +1584,8 @@ else if (join != null){
15841584
}
15851585

15861586
// 取出Table对应的JSONObject,及内部引用赋值 key:value
1587-
Map<String, Object> tableObj;
1588-
Map<String, Object> parentPathObj; // 保留
1587+
M tableObj;
1588+
M parentPathObj; // 保留
15891589
try {
15901590
parentPathObj = arrKey == null ? request : JSON.get(request, arrKey); // 保留
15911591
tableObj = parentPathObj == null ? null : JSON.get(parentPathObj, tableKey);
@@ -1595,7 +1595,7 @@ else if (join != null){
15951595
}
15961596
catch (Exception e2) {
15971597
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":'" + e.getKey() + "' 对应的 " + tableKey + ":value 中 value 类型不合法!" +
1598-
"必须是 {} 这种 Map<String, Object> 格式!" + e2.getMessage());
1598+
"必须是 {} 这种 Map<String, Object> 格式!" + e2.getMessage());
15991599
}
16001600

16011601
if (arrKey != null) {
@@ -1613,7 +1613,7 @@ else if (join != null){
16131613

16141614
boolean isAppJoin = "@".equals(joinType);
16151615

1616-
Map<String, Object> refObj = new LinkedHashMap<String, Object>(tableObj.size());
1616+
M refObj = (M) JSON.createJSONObject();
16171617

16181618
String key = index < 0 ? null : path.substring(index + 1); // id@
16191619
if (key != null) { // 指定某个 key 为 JOIN ON 条件
@@ -1625,7 +1625,7 @@ else if (join != null){
16251625

16261626
if (tableObj.get(key) instanceof String == false) {
16271627
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":" + e.getKey() + "' 对应的 "
1628-
+ tableKey + ":{ " + key + ": value } 中 value 类型不合法!必须为同层级引用赋值路径 String!");
1628+
+ tableKey + ":{ " + key + ": value } 中 value 类型不合法!必须为同层级引用赋值路径 String!");
16291629
}
16301630

16311631
if (isAppJoin && StringUtil.isName(key.substring(0, key.length() - 1)) == false) {
@@ -1639,7 +1639,7 @@ else if (join != null){
16391639

16401640
Set<Entry<String, Object>> tableSet = tableObj.entrySet();
16411641
// 取出所有 join 条件
1642-
Map<String, Object> requestObj = new LinkedHashMap<String, Object>(); // (Map<String, Object>) obj.clone();
1642+
M requestObj = (M) JSON.createJSONObject(); // (Map<String, Object>) obj.clone();
16431643

16441644
boolean matchSingle = false;
16451645
for (Entry<String, Object> tableEntry : tableSet) {
@@ -1717,13 +1717,16 @@ else if (join != null){
17171717
}
17181718

17191719

1720-
Join j = new Join();
1720+
Join<T, M, L> j = new Join<>();
17211721
j.setPath(e.getKey());
17221722
j.setJoinType(joinType);
17231723
j.setTable(table);
17241724
j.setAlias(alias);
1725-
j.setOuter((Map<String, Object>) outer);
1725+
1726+
M outerObj = (M) JSON.createJSONObject((Map<String, Object>) outer);
1727+
j.setOuter(outerObj);
17261728
j.setRequest(requestObj);
1729+
17271730
if (arrKey != null) {
17281731
Integer count = getInteger(parentPathObj, JSONRequest.KEY_COUNT);
17291732
j.setCount(count == null ? getDefaultQueryCount() : count);

APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
* @param <T> id 与 userId 的类型,一般为 Long
6969
*/
7070
public abstract class AbstractVerifier<T, M extends Map<String, Object>, L extends List<Object>>
71-
implements Verifier<T, M, L>, IdCallback<T> { // , JSONParser<M, L> {
71+
implements Verifier<T, M, L>, IdCallback<T> {
7272
private static final String TAG = "AbstractVerifier";
7373

7474
/**为 PUT, DELETE 强制要求必须有 id/id{}/id{}@ 条件
@@ -381,7 +381,7 @@ public void verifyUseRole(SQLConfig<T, M, L> config, String table, RequestMethod
381381
Collection<Object> requestIdArray = (Collection<Object>) config.getWhere(visitorIdKey + "{}", true); // 不能是 &{}, |{} 不要传,直接 {}
382382
if (requestId != null) {
383383
if (requestIdArray == null) {
384-
requestIdArray = createJSONArray();
384+
requestIdArray = (L) JSON.createJSONArray();
385385
}
386386
requestIdArray.add(requestId);
387387
}
@@ -530,13 +530,13 @@ public void verifyRepeat(String table, String key, Object value, long exceptId)
530530
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
531531
}
532532

533-
M tblObj = createJSONObject();
533+
M tblObj = (M) JSON.createJSONObject();
534534
tblObj.put(key, value);
535535
if (exceptId > 0) {//允许修改自己的属性为该属性原来的值
536536
tblObj.put(JSONRequest.KEY_ID + "!", exceptId); // FIXME 这里 id 写死了,不支持自定义
537537
}
538538

539-
M req = createJSONObject();
539+
M req = (M) JSON.createJSONObject();
540540
req.put(table, tblObj);
541541
Map<String, Object> repeat = createParser().setMethod(HEAD).setNeedVerify(true).parseResponse(req);
542542

@@ -1620,7 +1620,7 @@ else if (tk.endsWith("<>")) { //rv包含tv内的值
16201620

16211621
L array = AbstractSQLConfig.newJSONArray(tv, new JSONCreator<Map<String, Object>, L>() {
16221622
@Override
1623-
public Map<String, Object> createJSONObject() {
1623+
public M createJSONObject() {
16241624
return (M) JSON.createJSONObject();
16251625
}
16261626

@@ -1885,5 +1885,4 @@ public static String getCacheKeyForRequest(String method, String tag) {
18851885
return method + "/" + tag;
18861886
}
18871887

1888-
18891888
}

APIJSONORM/src/main/java/apijson/orm/FunctionParser.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**远程函数解析器
1515
* @author Lemon
1616
*/
17-
public interface FunctionParser<T extends Object, M extends Map<String, Object>, L extends List<Object>> extends JSONParser<M, L> {
17+
public interface FunctionParser<T, M extends Map<String, Object>, L extends List<Object>> {
1818

1919
Object invoke(@NotNull String function, @NotNull M currentObject) throws Exception;
2020
Object invoke(@NotNull String function, @NotNull M currentObject, boolean containRaw) throws Exception;
@@ -51,36 +51,4 @@ public interface FunctionParser<T extends Object, M extends Map<String, Object>,
5151
M getCurrentObject();
5252
FunctionParser<T, M, L> setCurrentObject(@NotNull M currentObject);
5353

54-
default M createJSONObject() {
55-
return (M) new JSONObject();
56-
}
57-
58-
default L createJSONArray() {
59-
return (L) new JSONArray();
60-
}
61-
62-
default String toJSONString(Object obj) {
63-
return JSON.toJSONString(obj);
64-
}
65-
66-
default Object parseJSON(Object json) {
67-
return JSON.parseJSON(json);
68-
}
69-
70-
default M parseObject(Object json) {
71-
return (M) parseObject(json, JSONObject.class);
72-
}
73-
74-
default <T> T parseObject(Object json, Class<T> clazz) {
75-
return JSON.parseObject(json, clazz);
76-
}
77-
78-
default L parseArray(Object json) {
79-
return (L) parseObject(json, JSONArray.class);
80-
}
81-
82-
default <T> List<T> parseArray(Object json, Class<T> clazz) {
83-
return JSON.parseArray(json, clazz);
84-
}
85-
8654
}

APIJSONORM/src/main/java/apijson/orm/Join.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ public boolean isSQLJoin() {
161161
return ! isAppJoin();
162162
}
163163

164-
public static boolean isSQLJoin(Join j) {
164+
public static boolean isSQLJoin(Join<?, ?, ?> j) {
165165
return j != null && j.isSQLJoin();
166166
}
167167

168-
public static boolean isAppJoin(Join j) {
168+
public static boolean isAppJoin(Join<?, ?, ?> j) {
169169
return j != null && j.isAppJoin();
170170
}
171171

172-
public static boolean isLeftOrRightJoin(Join j) {
172+
public static boolean isLeftOrRightJoin(Join<?, ?, ?> j) {
173173
return j != null && j.isLeftOrRightJoin();
174174
}
175175

APIJSONORM/src/main/java/apijson/orm/ObjectParser.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**简化Parser,getObject和getArray(getArrayConfig)都能用
1414
* @author Lemon
1515
*/
16-
public interface ObjectParser<T, M extends Map<String, Object>, L extends List<Object>> { // extends JSONParser<M, L> {
16+
public interface ObjectParser<T, M extends Map<String, Object>, L extends List<Object>> {
1717

1818
Parser<T, M, L> getParser();
1919
ObjectParser<T, M, L> setParser(Parser<T, M, L> parser);
@@ -164,36 +164,4 @@ public interface ObjectParser<T, M extends Map<String, Object>, L extends List<O
164164
Map<String, Map<String, String>> getFunctionMap();
165165
Map<String, M> getChildMap();
166166

167-
// default M createJSONObject() {
168-
// return (M) new JSONObject();
169-
// }
170-
//
171-
// default L createJSONArray() {
172-
// return (L) new JSONArray();
173-
// }
174-
//
175-
// default String toJSONString(Object obj) {
176-
// return JSON.toJSONString(obj);
177-
// }
178-
//
179-
// default Object parseJSON(Object json) {
180-
// return JSON.parseJSON(json);
181-
// }
182-
//
183-
// default M parseObject(Object json) {
184-
// return (M) parseObject(json, JSONObject.class);
185-
// }
186-
//
187-
// default <T> T parseObject(Object json, Class<T> clazz) {
188-
// return JSON.parseObject(json, clazz);
189-
// }
190-
//
191-
// default L parseArray(Object json) {
192-
// return (L) parseObject(json, JSONArray.class);
193-
// }
194-
//
195-
// default <T> List<T> parseArray(Object json, Class<T> clazz) {
196-
// return JSON.parseArray(json, clazz);
197-
// }
198-
199167
}

APIJSONORM/src/main/java/apijson/orm/Parser.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**解析器
1616
* @author Lemon
1717
*/
18-
public interface Parser<T, M extends Map<String, Object>, L extends List<Object>> { // extends JSONParser<M, L> {
18+
public interface Parser<T, M extends Map<String, Object>, L extends List<Object>> {
1919

2020
@NotNull
2121
Visitor<T> getVisitor();
@@ -130,36 +130,4 @@ M parseCorrectRequest(RequestMethod method, String tag, int version, String name
130130
M newSuccessResult();
131131
M newErrorResult(Exception e);
132132

133-
// default M createJSONObject() {
134-
// return (M) new JSONObject();
135-
// }
136-
//
137-
// default L createJSONArray() {
138-
// return (L) new JSONArray();
139-
// }
140-
//
141-
// default String toJSONString(Object obj) {
142-
// return JSON.toJSONString(obj);
143-
// }
144-
//
145-
// default Object parseJSON(Object json) {
146-
// return JSON.parseJSON(json);
147-
// }
148-
//
149-
// default M parseObject(Object json) {
150-
// return (M) parseObject(json, JSONObject.class);
151-
// }
152-
//
153-
// default <T> T parseObject(Object json, Class<T> clazz) {
154-
// return JSON.parseObject(json, clazz);
155-
// }
156-
//
157-
// default L parseArray(Object json) {
158-
// return (L) parseObject(json, JSONArray.class);
159-
// }
160-
//
161-
// default <T> List<T> parseArray(Object json, Class<T> clazz) {
162-
// return JSON.parseArray(json, clazz);
163-
// }
164-
165133
}

APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**executor for query(read) or update(write) MySQL database
2020
* @author Lemon
2121
*/
22-
public interface SQLExecutor<T, M extends Map<String, Object>, L extends List<Object>> { // extends JSONParser<M, L> {
22+
public interface SQLExecutor<T, M extends Map<String, Object>, L extends List<Object>> {
2323
Parser<T, M, L> getParser();
2424
SQLExecutor<T, M, L> setParser(Parser<T, M, L> parser);
2525

@@ -135,36 +135,4 @@ default Statement getStatement(@NotNull SQLConfig<T, M, L> config) throws Except
135135

136136
long getSqlResultDuration();
137137

138-
// default M createJSONObject() {
139-
// return (M) new JSONObject();
140-
// }
141-
//
142-
// default L createJSONArray() {
143-
// return (L) new JSONArray();
144-
// }
145-
//
146-
// default String toJSONString(Object obj) {
147-
// return JSON.toJSONString(obj);
148-
// }
149-
//
150-
// default Object parseJSON(Object json) {
151-
// return JSON.parseJSON(json);
152-
// }
153-
//
154-
// default M parseObject(Object json) {
155-
// return (M) parseObject(json, JSONObject.class);
156-
// }
157-
//
158-
// default <T> T parseObject(Object json, Class<T> clazz) {
159-
// return JSON.parseObject(json, clazz);
160-
// }
161-
//
162-
// default L parseArray(Object json) {
163-
// return (L) parseObject(json, JSONArray.class);
164-
// }
165-
//
166-
// default <T> List<T> parseArray(Object json, Class<T> clazz) {
167-
// return JSON.parseArray(json, clazz);
168-
// }
169-
170138
}

0 commit comments

Comments
 (0)