20
20
21
21
import javax .activation .UnsupportedDataTypeException ;
22
22
import java .rmi .ServerException ;
23
- import java .sql .SQLException ;
24
23
import java .util .ArrayList ;
25
24
import java .util .Arrays ;
26
25
import java .util .LinkedHashMap ;
@@ -70,10 +69,6 @@ public AbstractObjectParser setParser(AbstractParser<?> parser) {
70
69
protected final boolean drop ;
71
70
72
71
/**for single object
73
- * @param parentPath
74
- * @param request
75
- * @param name
76
- * @throws Exception
77
72
*/
78
73
public AbstractObjectParser (@ NotNull JSONObject request , String parentPath , SQLConfig arrayConfig
79
74
, boolean isSubquery , boolean isTable , boolean isArrayMainTable ) throws Exception {
@@ -157,7 +152,7 @@ public boolean isBreakParse() {
157
152
158
153
protected JSONObject response ;
159
154
protected JSONObject sqlRequest ;
160
- protected JSONObject sqlReponse ;
155
+ protected JSONObject sqlResponse ;
161
156
/**
162
157
* 自定义关键词
163
158
*/
@@ -200,7 +195,7 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
200
195
breakParse = false ;
201
196
202
197
response = new JSONObject (true );//must init
203
- sqlReponse = null ;//must init
198
+ sqlResponse = null ;//must init
204
199
205
200
if (isReuse == false ) {
206
201
sqlRequest = new JSONObject (true );//must init
@@ -239,23 +234,27 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
239
234
}
240
235
//条件>>>>>>>>>>>>>>>>>>>
241
236
242
- String key ;
243
- Object value ;
244
237
int index = 0 ;
238
+ //hasOtherKeyNotFun = false;
245
239
246
240
for (Entry <String , Object > entry : set ) {
247
241
if (isBreakParse ()) {
248
242
break ;
249
243
}
250
244
251
- value = entry .getValue ();
245
+ Object value = entry .getValue ();
252
246
if (value == null ) {
253
247
continue ;
254
248
}
255
- key = entry .getKey ();
249
+ String key = entry .getKey ();
256
250
257
251
try {
258
- if (key .startsWith ("@" ) || key .endsWith ("@" ) || (key .endsWith ("<>" ) && value instanceof JSONObject )) {
252
+ boolean startsWithAt = key .startsWith ("@" );
253
+ //if (startsWithAt || (key.endsWith("()") == false)) {
254
+ // hasOtherKeyNotFun = true;
255
+ //}
256
+
257
+ if (startsWithAt || key .endsWith ("@" ) || (key .endsWith ("<>" ) && value instanceof JSONObject )) {
259
258
if (onParse (key , value ) == false ) {
260
259
invalidate ();
261
260
}
@@ -331,8 +330,9 @@ else if (method == PUT && value instanceof JSONArray && (whereList == null || wh
331
330
332
331
333
332
333
+ //private boolean hasOtherKeyNotFun = false;
334
334
335
- /**解析普通成员
335
+ /**解析普通成员
336
336
* @param key
337
337
* @param value
338
338
* @return whether parse succeed
@@ -438,23 +438,23 @@ else if (value instanceof String) { // //key{}@ getRealKey, 引用赋值路径
438
438
439
439
String type ; //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里用 "-","0","+" 更直观,转用 -1,0,1 对性能提升不大。
440
440
boolean isMinus = k .endsWith ("-" );
441
+ boolean isPlus = isMinus == false && k .endsWith ("+" );
441
442
if (isMinus ) { //不能封装到functionMap后批量执行,否则会导致非Table内的 key-():function() 在onChildParse后执行!
442
443
type = "-" ;
443
444
k = k .substring (0 , k .length () - 1 );
444
-
445
- if (isTable == false ) {
446
- parseFunction (k , (String ) value , parentPath , name , request );
447
- }
448
445
}
449
- else if (k . endsWith ( "+" ) ) {
446
+ else if (isPlus ) {
450
447
type = "+" ;
451
448
k = k .substring (0 , k .length () - 1 );
452
449
}
453
450
else {
454
451
type = "0" ;
455
452
}
456
453
457
- if (isMinus == false || isTable ) {
454
+ if (isPlus == false && isTable == false ) {
455
+ parseFunction (k , (String ) value , parentPath , name , request , isMinus );
456
+ }
457
+ else {
458
458
//远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
459
459
Map <String , String > map = functionMap .get (type );
460
460
if (map == null ) {
@@ -466,7 +466,7 @@ else if (k.endsWith("+")) {
466
466
}
467
467
}
468
468
else if (isTable && key .startsWith ("@" ) && JSONRequest .TABLE_KEY_LIST .contains (key ) == false ) {
469
- customMap .put (key , value );
469
+ customMap .put (key , value );
470
470
}
471
471
else {
472
472
sqlRequest .put (key , value );
@@ -718,37 +718,37 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
718
718
public AbstractObjectParser executeSQL () throws Exception {
719
719
//执行SQL操作数据库
720
720
if (isTable == false ) {//提高性能
721
- sqlReponse = new JSONObject (sqlRequest );
721
+ sqlResponse = new JSONObject (sqlRequest );
722
722
}
723
723
else {
724
- try {
725
- sqlReponse = onSQLExecute ();
726
- }
727
- catch (Exception e ) {
728
- if (e instanceof NotExistException || (e instanceof CommonException && e .getCause () instanceof NotExistException )) {
729
- // Log.e(TAG, "getObject try { response = getSQLObject(config2); } catch (Exception e) {");
730
- // if (e instanceof NotExistException) {//非严重异常,有时候只是数据不存在
731
- // // e.printStackTrace();
732
- sqlReponse = null ;//内部吃掉异常,put到最外层
733
- // requestObject.put(JSONResponse.KEY_MSG
734
- // , StringUtil.getString(requestObject.get(JSONResponse.KEY_MSG)
735
- // + "; query " + path + " cath NotExistException:"
736
- // + newErrorResult(e).getString(JSONResponse.KEY_MSG)));
737
- // } else {
738
- // throw e;
739
- // }
740
- }
741
- else {
742
- throw e ;
724
+ try {
725
+ sqlResponse = onSQLExecute ();
726
+ }
727
+ catch (Exception e ) {
728
+ if (e instanceof NotExistException || (e instanceof CommonException && e .getCause () instanceof NotExistException )) {
729
+ // Log.e(TAG, "getObject try { response = getSQLObject(config2); } catch (Exception e) {");
730
+ // if (e instanceof NotExistException) {//非严重异常,有时候只是数据不存在
731
+ // // e.printStackTrace();
732
+ sqlResponse = null ;//内部吃掉异常,put到最外层
733
+ // requestObject.put(JSONResponse.KEY_MSG
734
+ // , StringUtil.getString(requestObject.get(JSONResponse.KEY_MSG)
735
+ // + "; query " + path + " cath NotExistException:"
736
+ // + newErrorResult(e).getString(JSONResponse.KEY_MSG)));
737
+ // } else {
738
+ // throw e;
739
+ // }
740
+ }
741
+ else {
742
+ throw e ;
743
+ }
744
+ }
743
745
}
744
- }
745
- }
746
746
747
- if (drop ) {//丢弃Table,只为了向下提供条件
748
- sqlReponse = null ;
749
- }
747
+ if (drop ) {//丢弃Table,只为了向下提供条件
748
+ sqlResponse = null ;
749
+ }
750
750
751
- return this ;
751
+ return this ;
752
752
}
753
753
754
754
/**
@@ -757,12 +757,12 @@ public AbstractObjectParser executeSQL() throws Exception {
757
757
*/
758
758
@ Override
759
759
public JSONObject response () throws Exception {
760
- if (sqlReponse == null || sqlReponse .isEmpty ()) {
760
+ if (sqlResponse == null || sqlResponse .isEmpty ()) {
761
761
if (isTable ) {//Table自身都获取不到值,则里面的Child都无意义,不需要再解析
762
762
return null ; // response;
763
763
}
764
764
} else {
765
- response .putAll (sqlReponse );
765
+ response .putAll (sqlResponse );
766
766
}
767
767
768
768
@@ -791,17 +791,22 @@ public void onFunctionResponse(String type) throws Exception {
791
791
//解析函数function
792
792
Set <Entry <String , String >> functionSet = map == null ? null : map .entrySet ();
793
793
if (functionSet != null && functionSet .isEmpty () == false ) {
794
- JSONObject json = "-" .equals (type ) ? request : response ; // key-():function 是实时执行,而不是在这里批量执行
794
+ boolean isMinus = "-" .equals (type );
795
+ JSONObject json = isMinus ? sqlRequest : response ; // key-():function 是实时执行,而不是在这里批量执行
795
796
796
797
for (Entry <String , String > entry : functionSet ) {
797
- parseFunction (entry .getKey (), entry .getValue (), parentPath , name , json );
798
+ parseFunction (entry .getKey (), entry .getValue (), parentPath , name , json , isMinus );
798
799
}
799
800
}
800
801
}
801
802
802
- public void parseFunction (String key , String value , String parentPath , String currentName , JSONObject currentObject ) throws Exception {
803
+ //public void parseFunction(String key, String value, String parentPath, String currentName, JSONObject currentObject) throws Exception {
804
+ // parseFunction(key, value, parentPath, currentName, currentObject, false);
805
+ //}
806
+ public void parseFunction (String key , String value , String parentPath , String currentName , JSONObject currentObject , boolean isMinus ) throws Exception {
803
807
Object result ;
804
- if (key .startsWith ("@" )) {
808
+ boolean isProcedure = key .startsWith ("@" );
809
+ if (isProcedure ) {
805
810
FunctionBean fb = AbstractFunctionParser .parseFunction (value , currentObject , true );
806
811
807
812
SQLConfig config = newSQLConfig (true );
@@ -814,12 +819,23 @@ public void parseFunction(String key, String value, String parentPath, String cu
814
819
result = parser .onFunctionParse (key , value , parentPath , currentName , currentObject );
815
820
}
816
821
817
- if (result != null ) {
818
- String k = AbstractSQLConfig .getRealKey (method , key , false , false );
822
+ String k = AbstractSQLConfig .getRealKey (method , key , false , false );
819
823
820
- response .put (k , result );
821
- parser .putQueryResult (AbstractParser .getAbsPath (path , k ), result );
822
- }
824
+ if (isProcedure == false && isMinus ) {
825
+ if (result != null ) {
826
+ sqlRequest .put (k , result );
827
+ } else {
828
+ sqlRequest .remove (k );
829
+ }
830
+ }
831
+
832
+ if (result != null ) {
833
+ response .put (k , result );
834
+ } else {
835
+ response .remove (k );
836
+ }
837
+
838
+ parser .putQueryResult (AbstractParser .getAbsPath (path , k ), result );
823
839
}
824
840
825
841
@ Override
@@ -863,46 +879,46 @@ public JSONObject onSQLExecute() throws Exception {
863
879
result = parser .executeSQL (sqlConfig , isSubquery );
864
880
865
881
boolean isSimpleArray = false ;
866
- // 提取并缓存数组主表的列表数据
867
- List <JSONObject > rawList = result == null ? null : (List <JSONObject >) result .remove (AbstractSQLExecutor .KEY_RAW_LIST );
882
+ // 提取并缓存数组主表的列表数据
883
+ List <JSONObject > rawList = result == null ? null : (List <JSONObject >) result .remove (AbstractSQLExecutor .KEY_RAW_LIST );
868
884
869
- if (isArrayMainTable && position == 0 && rawList != null ) {
885
+ if (isArrayMainTable && position == 0 && rawList != null ) {
870
886
871
- isSimpleArray = (functionMap == null || functionMap .isEmpty ())
872
- && (customMap == null || customMap .isEmpty ())
873
- && (childMap == null || childMap .isEmpty ())
874
- && (table .equals (arrayTable ));
887
+ isSimpleArray = (functionMap == null || functionMap .isEmpty ())
888
+ && (customMap == null || customMap .isEmpty ())
889
+ && (childMap == null || childMap .isEmpty ())
890
+ && (table .equals (arrayTable ));
875
891
876
- // APP JOIN 副表时副表返回了这个字段 rawList = (List<JSONObject>) result.remove(AbstractSQLExecutor.KEY_RAW_LIST);
877
- String arrayPath = parentPath .substring (0 , parentPath .lastIndexOf ("[]" ) + 2 );
892
+ // APP JOIN 副表时副表返回了这个字段 rawList = (List<JSONObject>) result.remove(AbstractSQLExecutor.KEY_RAW_LIST);
893
+ String arrayPath = parentPath .substring (0 , parentPath .lastIndexOf ("[]" ) + 2 );
878
894
879
- if (isSimpleArray == false ) {
880
- long startTime = System .currentTimeMillis ();
895
+ if (isSimpleArray == false ) {
896
+ long startTime = System .currentTimeMillis ();
881
897
882
- for (int i = 1 ; i < rawList .size (); i ++) { // 从 1 开始,0 已经处理过
883
- JSONObject obj = rawList .get (i );
898
+ for (int i = 1 ; i < rawList .size (); i ++) { // 从 1 开始,0 已经处理过
899
+ JSONObject obj = rawList .get (i );
884
900
885
- if (obj != null ) {
886
- parser .putQueryResult (arrayPath + "/" + i + "/" + name , obj ); // 解决获取关联数据时requestObject里不存在需要的关联数据
887
- }
888
- }
901
+ if (obj != null ) {
902
+ parser .putQueryResult (arrayPath + "/" + i + "/" + name , obj ); // 解决获取关联数据时requestObject里不存在需要的关联数据
903
+ }
904
+ }
889
905
890
- long endTime = System .currentTimeMillis (); // 3ms - 8ms
891
- Log .e (TAG , "\n onSQLExecute <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n for (int i = 1; i < list.size(); i++) startTime = " + startTime
892
- + "; endTime = " + endTime + "; duration = " + (endTime - startTime ) + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
893
- }
906
+ long endTime = System .currentTimeMillis (); // 3ms - 8ms
907
+ Log .e (TAG , "\n onSQLExecute <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n for (int i = 1; i < list.size(); i++) startTime = " + startTime
908
+ + "; endTime = " + endTime + "; duration = " + (endTime - startTime ) + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
909
+ }
894
910
895
- parser .putArrayMainCache (arrayPath , rawList );
896
- }
911
+ parser .putArrayMainCache (arrayPath , rawList );
912
+ }
897
913
898
- if (isSubquery == false && result != null ) {
899
- parser .putQueryResult (path , result ); // 解决获取关联数据时requestObject里不存在需要的关联数据
914
+ if (isSubquery == false && result != null ) {
915
+ parser .putQueryResult (path , result ); // 解决获取关联数据时requestObject里不存在需要的关联数据
900
916
901
- if (isSimpleArray && rawList != null ) {
902
- result .put (AbstractSQLExecutor .KEY_RAW_LIST , rawList );
903
- }
904
- }
905
- }
917
+ if (isSimpleArray && rawList != null ) {
918
+ result .put (AbstractSQLExecutor .KEY_RAW_LIST , rawList );
919
+ }
920
+ }
921
+ }
906
922
907
923
return result ;
908
924
}
@@ -941,7 +957,7 @@ public void recycle() {
941
957
request = null ;
942
958
response = null ;
943
959
sqlRequest = null ;
944
- sqlReponse = null ;
960
+ sqlResponse = null ;
945
961
946
962
functionMap = null ;
947
963
customMap = null ;
@@ -959,7 +975,7 @@ public AbstractObjectParser setMethod(RequestMethod method) {
959
975
if (this .method != method ) {
960
976
this .method = method ;
961
977
sqlConfig = null ;
962
- //TODO ? sqlReponse = null;
978
+ //TODO ? sqlResponse = null;
963
979
}
964
980
return this ;
965
981
}
@@ -1007,8 +1023,8 @@ public JSONObject getSqlRequest() {
1007
1023
return sqlRequest ;
1008
1024
}
1009
1025
@ Override
1010
- public JSONObject getSqlReponse () {
1011
- return sqlReponse ;
1026
+ public JSONObject getSqlResponse () {
1027
+ return sqlResponse ;
1012
1028
}
1013
1029
1014
1030
@ Override
0 commit comments