@@ -123,6 +123,18 @@ public AbstractObjectParser setParentPath(String parentPath) {
123
123
return this ;
124
124
}
125
125
126
+ protected JSONObject cache ;
127
+ @ Override
128
+ public JSONObject getCache () {
129
+ return cache ;
130
+ }
131
+
132
+ @ Override
133
+ public AbstractObjectParser <T > setCache (JSONObject cache ) {
134
+ this .cache = cache ;
135
+ return this ;
136
+ }
137
+
126
138
protected int position ;
127
139
public int getPosition () {
128
140
return position ;
@@ -243,6 +255,7 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
243
255
244
256
int index = 0 ;
245
257
// hasOtherKeyNotFun = false;
258
+ JSONObject viceItem = null ;
246
259
247
260
for (Entry <String , Object > entry : set ) {
248
261
if (isBreakParse ()) {
@@ -280,7 +293,14 @@ else if (value instanceof JSONObject) { // JSONObject,往下一级提取
280
293
childMap .put (key , (JSONObject )value );
281
294
}
282
295
else { // 直接解析并替换原来的,[]:{} 内必须直接解析,否则会因为丢掉count等属性,并且total@:"/[]/total"必须在[]:{} 后!
283
- response .put (key , onChildParse (index , key , (JSONObject )value ));
296
+ JSON cache = index <= 0 || type != TYPE_ITEM || viceItem == null ? null : viceItem .getJSONObject (key );
297
+ JSON result = onChildParse (index , key , (JSONObject ) value , cache );
298
+ if (index <= 0 && type == TYPE_ITEM ) {
299
+ JSONObject mainItem = (JSONObject ) result ;
300
+ viceItem = result == null ? null : (JSONObject ) mainItem .remove (AbstractSQLExecutor .KEY_VICE_ITEM );
301
+ }
302
+
303
+ response .put (key , result );
284
304
index ++;
285
305
}
286
306
}
@@ -368,7 +388,7 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
368
388
+ JSONRequest .SUBQUERY_RANGE_ALL + ", " + JSONRequest .SUBQUERY_RANGE_ANY + "] 中的一个!" );
369
389
}
370
390
371
- JSONArray arr = parser .onArrayParse (subquery , path , key , true );
391
+ JSONArray arr = parser .onArrayParse (subquery , path , key , true , null );
372
392
373
393
JSONObject obj = arr == null || arr .isEmpty () ? null : arr .getJSONObject (0 );
374
394
if (obj == null ) {
@@ -530,18 +550,19 @@ else if (isTable && key.startsWith("@") && JSONRequest.TABLE_KEY_LIST.contains(k
530
550
* @param index
531
551
* @param key
532
552
* @param value
553
+ * @param cache
533
554
* @return
534
555
* @throws Exception
535
556
*/
536
557
@ Override
537
- public JSON onChildParse (int index , String key , JSONObject value ) throws Exception {
558
+ public JSON onChildParse (int index , String key , JSONObject value , JSON cache ) throws Exception {
538
559
boolean isFirst = index <= 0 ;
539
560
boolean isMain = isFirst && type == TYPE_ITEM ;
540
561
541
562
JSON child ;
542
563
boolean isEmpty ;
543
564
544
- if (apijson .JSONObject .isArrayKey (key )) {// APIJSON Array
565
+ if (apijson .JSONObject .isArrayKey (key )) { // APIJSON Array
545
566
if (isMain ) {
546
567
throw new IllegalArgumentException (parentPath + "/" + key + ":{} 不合法!"
547
568
+ "数组 []:{} 中第一个 key:{} 必须是主表 TableKey:{} !不能为 arrayKey[]:{} !" );
@@ -557,7 +578,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
557
578
}
558
579
559
580
String query = value .getString (KEY_QUERY );
560
- child = parser .onArrayParse (value , path , key , isSubquery );
581
+ child = parser .onArrayParse (value , path , key , isSubquery , cache instanceof JSONArray ? ( JSONArray ) cache : null );
561
582
isEmpty = child == null || ((JSONArray ) child ).isEmpty ();
562
583
563
584
if ("2" .equals (query ) || "ALL" .equals (query )) { // 不判断 isEmpty,因为分页数据可能只是某页没有
@@ -594,7 +615,8 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
594
615
}
595
616
}
596
617
597
- child = parser .onObjectParse (value , path , key , isMain ? arrayConfig .setType (SQLConfig .TYPE_ITEM_CHILD_0 ) : null , isSubquery );
618
+ child = parser .onObjectParse (value , path , key , isMain ? arrayConfig .setType (SQLConfig .TYPE_ITEM_CHILD_0 ) : null
619
+ , isSubquery , cache instanceof JSONObject ? (JSONObject ) cache : null );
598
620
599
621
isEmpty = child == null || ((JSONObject ) child ).isEmpty ();
600
622
if (isFirst && isEmpty ) {
@@ -776,7 +798,7 @@ public void onTableArrayParse(String key, JSONArray valueArray) throws Exception
776
798
req = parser .parseCorrectRequest (method , childKey , version , "" , req , maxUpdateCount , parser );
777
799
}
778
800
//parser.getMaxSQLCount() ? 可能恶意调用接口,把数据库拖死
779
- result = (JSONObject ) onChildParse (0 , "" + i , req );
801
+ result = (JSONObject ) onChildParse (0 , "" + i , req , null );
780
802
}
781
803
catch (Exception e ) {
782
804
if (allowPartialFailed == false ) {
@@ -1080,7 +1102,7 @@ public void onChildResponse() throws Exception {
1080
1102
if (set != null ) {
1081
1103
int index = 0 ;
1082
1104
for (Entry <String , JSONObject > entry : set ) {
1083
- Object child = entry == null ? null : onChildParse (index , entry .getKey (), entry .getValue ());
1105
+ Object child = entry == null ? null : onChildParse (index , entry .getKey (), entry .getValue (), null );
1084
1106
if (child == null
1085
1107
|| (child instanceof JSONObject && ((JSONObject ) child ).isEmpty ())
1086
1108
|| (child instanceof JSONArray && ((JSONArray ) child ).isEmpty ())
@@ -1106,8 +1128,11 @@ public Object onReferenceParse(@NotNull String path) {
1106
1128
public JSONObject onSQLExecute () throws Exception {
1107
1129
int position = getPosition ();
1108
1130
1109
- JSONObject result ;
1110
- if (isArrayMainTable && position > 0 ) { // 数组主表使用专门的缓存数据
1131
+ JSONObject result = getCache ();
1132
+ if (result != null ) {
1133
+ parser .putQueryResult (path , result );
1134
+ }
1135
+ else if (isArrayMainTable && position > 0 ) { // 数组主表使用专门的缓存数据
1111
1136
result = parser .getArrayMainCacheItem (parentPath .substring (0 , parentPath .lastIndexOf ("[]" ) + 2 ), position );
1112
1137
}
1113
1138
else {
@@ -1134,7 +1159,8 @@ public JSONObject onSQLExecute() throws Exception {
1134
1159
JSONObject obj = rawList .get (i );
1135
1160
1136
1161
if (obj != null ) {
1137
- parser .putQueryResult (arrayPath + "/" + i + "/" + name , obj ); // 解决获取关联数据时requestObject里不存在需要的关联数据
1162
+ // obj.remove(AbstractSQLExecutor.KEY_VICE_ITEM);
1163
+ parser .putQueryResult (arrayPath + "/" + i + "/" + name , obj ); // 解决获取关联数据时requestObject里不存在需要的关联数据
1138
1164
}
1139
1165
}
1140
1166
0 commit comments