Skip to content

Commit b544c41

Browse files
committed
同步更新至 4.1.0
1 parent a7d0d86 commit b544c41

11 files changed

+273
-106
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>apijson.orm</groupId>
77
<artifactId>apijson-orm</artifactId>
8-
<version>4.0.0</version>
8+
<version>4.1.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONORM</name>

src/main/java/apijson/JSONObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public JSONObject setUserIdIn(List<Object> list) {
155155
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
156156
public static final String KEY_ORDER = "@order"; //排序方式
157157
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
158+
public static final String KEY_RAW = "@raw"; //自定义where条件拼接
158159

159160
public static final List<String> TABLE_KEY_LIST;
160161
static {
@@ -171,6 +172,7 @@ public JSONObject setUserIdIn(List<Object> list) {
171172
TABLE_KEY_LIST.add(KEY_HAVING);
172173
TABLE_KEY_LIST.add(KEY_ORDER);
173174
TABLE_KEY_LIST.add(KEY_JSON);
175+
TABLE_KEY_LIST.add(KEY_RAW);
174176
}
175177

176178
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>

src/main/java/apijson/JSONResponse.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ public JSONResponse(JSONObject object) {
6565
public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误
6666

6767

68+
public static final String KEY_OK = "ok";
6869
public static final String KEY_CODE = "code";
6970
public static final String KEY_MSG = "msg";
7071
public static final String KEY_COUNT = "count";
7172
public static final String KEY_TOTAL = "total";
73+
public static final String KEY_INFO = "info"; //详细的分页信息
74+
public static final String KEY_FIRST = "first"; //是否为首页
75+
public static final String KEY_LAST = "last"; //是否为尾页
76+
public static final String KEY_MAX = "max"; //最大页码
77+
public static final String KEY_MORE = "more"; //是否有更多
7278

7379
/**获取状态
7480
* @return

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ else if (value instanceof String) { // 引用赋值路径
357357
Log.i(TAG, "onParse targetPath = " + targetPath + "; target = " + target);
358358

359359
if (target == null) {//String#equals(null)会出错
360-
Log.d(TAG, "onParse target == null >> continue;");
360+
Log.d(TAG, "onParse target == null >> return true;");
361361
return true;
362362
}
363363
if (target instanceof Map) { //target可能是从requestObject里取出的 {}
364-
Log.d(TAG, "onParse target instanceof Map >> continue;");
365-
return false;
364+
if (isTable || targetPath.endsWith("[]/" + JSONResponse.KEY_INFO) == false) {
365+
Log.d(TAG, "onParse target instanceof Map >> return false;");
366+
return false; //FIXME 这个判断现在来看是否还有必要?为啥不允许为 JSONObject ?以前可能因为防止二次遍历再解析,现在只有一次遍历
367+
}
366368
}
367369
if (targetPath.equals(target)) {//必须valuePath和保证getValueByPath传进去的一致!
368370
Log.d(TAG, "onParse targetPath.equals(target) >>");
@@ -373,7 +375,7 @@ else if (value instanceof String) { // 引用赋值路径
373375
+ " || JSONRequest.TABLE_KEY_LIST.contains(key)) >> return null;");
374376
return false;//获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
375377
} else {
376-
Log.d(TAG, "onParse isTable(table) == false >> continue;");
378+
Log.d(TAG, "onParse isTable(table) == false >> return true;");
377379
return true;//舍去,对Table无影响
378380
}
379381
}
@@ -467,19 +469,20 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
467469
child = parser.onArrayParse(value, path, key, isSubquery);
468470
isEmpty = child == null || ((JSONArray) child).isEmpty();
469471
}
470-
else {//APIJSON Object
472+
else { //APIJSON Object
471473
boolean isTableKey = JSONRequest.isTableKey(Pair.parseEntry(key, true).getKey());
472474
if (type == TYPE_ITEM && isTableKey == false) {
473475
throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
474476
+ "数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{} !");
475477
}
476478

477-
if (//避免使用 "test":{"Test":{}} 绕过限制,实现查询爆炸 isTableKey &&
479+
if ( //避免使用 "test":{"Test":{}} 绕过限制,实现查询爆炸 isTableKey &&
478480
(arrayConfig == null || arrayConfig.getPosition() == 0)) {
479481
objectCount ++;
480482
int maxObjectCount = parser.getMaxObjectCount();
481-
if (objectCount > maxObjectCount) {
482-
throw new IllegalArgumentException(path + " 内截至 " + key + ":{} 时对象 key:{} 的数量达到 " + objectCount + " 已超限,必须在 0-" + maxObjectCount + " 内 !");
483+
if (objectCount > maxObjectCount) { //TODO 这里判断是批量新增/修改,然后上限为 maxUpdateCount
484+
throw new IllegalArgumentException(path + " 内截至 " + key + ":{} 时对象"
485+
+ " key:{} 的数量达到 " + objectCount + " 已超限,必须在 0-" + maxObjectCount + " 内 !");
483486
}
484487
}
485488

@@ -573,6 +576,7 @@ public void onTableArrayParse(String key, JSONArray value) throws Exception {
573576
int version = parser.getVersion();
574577
int maxUpdateCount = parser.getMaxUpdateCount();
575578

579+
String idKey = parser.createSQLConfig().getIdKey(); //Table[]: [{}] arrayConfig 为 null
576580
for (int i = 0; i < valueArray.size(); i++) { //只要有一条失败,则抛出异常,全部失败
577581
//TODO 改成一条多 VALUES 的 SQL 性能更高,报错也更会更好处理,更人性化
578582
JSONObject item;
@@ -596,14 +600,14 @@ public void onTableArrayParse(String key, JSONArray value) throws Exception {
596600
}
597601

598602
allCount += count;
599-
ids.add(result.get(JSONResponse.KEY_ID));
603+
ids.add(result.get(idKey));
600604
}
601605

602606
JSONObject allResult = AbstractParser.newSuccessResult();
603-
allResult.put(JSONResponse.KEY_ID_IN, ids);
604607
allResult.put(JSONResponse.KEY_COUNT, allCount);
608+
allResult.put(idKey + "[]", ids);
605609

606-
response.put(key, allResult); //不按原样返回,避免数据量过大
610+
response.put(childKey, allResult); //不按原样返回,避免数据量过大
607611
}
608612

609613

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

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
464464
}
465465

466466
if (StringUtil.isEmpty(tag, true)) {
467-
throw new IllegalArgumentException("请在最外层设置 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
467+
throw new IllegalArgumentException("请在最外层传 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
468468
}
469469

470470
//获取指定的JSON结构 <<<<<<<<<<<<
@@ -475,17 +475,28 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
475475
} catch (Exception e) {
476476
error = e.getMessage();
477477
}
478-
if (object == null) {//empty表示随意操作 || object.isEmpty()) {
479-
throw new UnsupportedOperationException("非开放请求必须是后端 Request 表中校验规则允许的操作!\n " + error);
478+
if (object == null) { //empty表示随意操作 || object.isEmpty()) {
479+
throw new UnsupportedOperationException("找不到 version: " + version + ", method: " + method.name() + ", tag: " + tag + " 对应的 structure !"
480+
+ "非开放请求必须是后端 Request 表中校验规则允许的操作!\n " + error + "\n如果需要则在 Request 表中新增配置!");
480481
}
481482

482-
JSONObject target = null;
483-
if (apijson.JSONObject.isTableKey(tag) && object.containsKey(tag) == false) {//tag是table名
484-
target = new JSONObject(true);
485-
target.put(tag, object);
486-
} else {
487-
target = object;
483+
JSONObject target = object;
484+
if (object.containsKey(tag) == false) { //tag 是 Table 名或 Table[]
485+
486+
boolean isArrayKey = tag.endsWith(":[]"); // JSONRequest.isArrayKey(tag);
487+
String key = isArrayKey ? tag.substring(0, tag.length() - 3) : tag;
488+
489+
if (apijson.JSONObject.isTableKey(key)) {
490+
if (isArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
491+
target.put(key + "[]", new JSONArray());
492+
}
493+
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
494+
target = new JSONObject(true);
495+
target.put(tag, object);
496+
}
497+
}
488498
}
499+
489500
//获取指定的JSON结构 >>>>>>>>>>>>>>
490501

491502
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
@@ -511,9 +522,13 @@ public static JSONObject extendResult(JSONObject object, int code, String msg) {
511522
if (object == null) {
512523
object = new JSONObject(true);
513524
}
525+
if (object.containsKey(JSONResponse.KEY_OK) == false) {
526+
object.put(JSONResponse.KEY_OK, JSONResponse.isSuccess(code));
527+
}
514528
if (object.containsKey(JSONResponse.KEY_CODE) == false) {
515529
object.put(JSONResponse.KEY_CODE, code);
516530
}
531+
517532
String m = StringUtil.getString(object.getString(JSONResponse.KEY_MSG));
518533
if (m.isEmpty() == false) {
519534
msg = m + " ;\n " + StringUtil.getString(msg);
@@ -738,9 +753,29 @@ public JSONObject onObjectParse(final JSONObject request
738753
int index = parentPath.lastIndexOf("]/");
739754
if (index >= 0) {
740755
int total = rp.getIntValue(JSONResponse.KEY_COUNT);
741-
putQueryResult(parentPath.substring(0, index) + "]/" + JSONResponse.KEY_TOTAL, total);
742-
743-
if (total <= arrayConfig.getCount()*arrayConfig.getPage()) {
756+
757+
String pathPrefix = parentPath.substring(0, index) + "]/";
758+
putQueryResult(pathPrefix + JSONResponse.KEY_TOTAL, total);
759+
760+
//详细的分页信息,主要为 PC 端提供
761+
int count = arrayConfig.getCount();
762+
int page = arrayConfig.getPage();
763+
int max = (int) ((total - 1)/count);
764+
if (max < 0) {
765+
max = 0;
766+
}
767+
768+
JSONObject pagination = new JSONObject(true);
769+
pagination.put(JSONResponse.KEY_TOTAL, total);
770+
pagination.put(JSONRequest.KEY_COUNT, count);
771+
pagination.put(JSONRequest.KEY_PAGE, page);
772+
pagination.put(JSONResponse.KEY_MAX, max);
773+
pagination.put(JSONResponse.KEY_MORE, page < max);
774+
pagination.put(JSONResponse.KEY_FIRST, page == 0);
775+
pagination.put(JSONResponse.KEY_LAST, page == max);
776+
putQueryResult(pathPrefix + JSONResponse.KEY_INFO, pagination);
777+
778+
if (total <= count*page) {
744779
query = JSONRequest.QUERY_TOTAL;//数量不够了,不再往后查询
745780
}
746781
}
@@ -1032,7 +1067,7 @@ else if (join != null){
10321067
j.setTargetKey(targetKey);
10331068
j.setKeyAndType(key);
10341069
j.setRequest(getJoinObject(table, tableObj, key));
1035-
j.setOutter((JSONObject) e.getValue());
1070+
j.setOuter((JSONObject) e.getValue());
10361071

10371072
joinList.add(j);
10381073

0 commit comments

Comments
 (0)