Skip to content

Commit

Permalink
修复多表 gets bug、别名从代码解析改为数据库配置、支持 sql@ update/delete,感谢 cloudAndMonkey …
Browse files Browse the repository at this point in the history
…的贡献 #551

#551
  • Loading branch information
TommyLemon authored Apr 17, 2023
2 parents 570e9fa + 0d75cc7 commit 26475f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
64 changes: 9 additions & 55 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2237,13 +2237,15 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
}

String _tag = buildTag(request, key, method, tag);
JSONObject requestItem = new JSONObject();
// key 处理别名
String _key = keyDelAlias(key);
requestItem.put(_key, obj);
JSONObject object = getRequestStructure(_method, _tag, version);
JSONObject ret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
jsonObject.put(key, ret.get(_key));
if(method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
JSONObject requestItem = new JSONObject();
requestItem.put(key, obj);
JSONObject ret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
jsonObject.put(key, ret.get(key));
} else {
return objectVerify(_method, _tag, version, name, request, maxUpdateCount, creator, object);
}
} else {
jsonObject.put(key, obj);
}
Expand Down Expand Up @@ -2283,64 +2285,16 @@ protected void setRequestAttribute(String key, boolean isArray, String attrKey,
}
}

protected String keyDelAlias(String key) {
int keyIndex = key.indexOf(":");
if (keyIndex != -1) {
String _key = key.substring(0, keyIndex);
if (apijson.JSONObject.isTableArray(key)) {
_key += apijson.JSONObject.KEY_ARRAY;
}
return _key;
}
return key;
}

protected String buildTag(JSONObject request, String key, RequestMethod method, String tag) {
Object val = request.get(key);

if (method == RequestMethod.CRUD) {
Map<String, Object> attrMap = keyObjectAttributesMap.get(key);
Object _tag = attrMap == null ? null : attrMap.get(JSONRequest.KEY_TAG);

if (_tag != null) {
if (val instanceof JSONArray) {
return _tag.toString();
}

tag = _tag.toString();
} else {
// key 作为默认的 tag
if (StringUtil.isEmpty(tag)) {
if (val instanceof JSONArray) {
return keyDelAlias(key);
}

tag = key;
} else {
if (val instanceof JSONArray) {
return tag;
}
}
}
return _tag != null ? _tag.toString() : StringUtil.isEmpty(tag) ? key : tag;
} else {
if (StringUtil.isEmpty(tag, true)) {
throw new IllegalArgumentException("请在最外层传 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
}
if (val instanceof JSONArray) {
return tag;
}
}

// 通用判断
// 对象, 需处理别名
if (val instanceof JSONObject && StringUtil.isNotEmpty(tag)) {
int keyIndex = tag.indexOf(":");
if (keyIndex != -1) {
return tag.substring(0, keyIndex);
}
return tag;
}

return tag;
}

Expand Down
2 changes: 1 addition & 1 deletion APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
}

// 不在target内的 key:{}
if (rk.startsWith("@") == false && objKeySet.contains(rk) == false) {
if (rk.startsWith("@") == false && rk.endsWith("@") == false && objKeySet.contains(rk) == false) {
if (rv instanceof JSONObject) {
throw new UnsupportedOperationException(method + " 请求,"
+ name + " 里面不允许传 " + rk + ":{} !");
Expand Down

0 comments on commit 26475f6

Please sign in to comment.