@@ -193,8 +193,21 @@ public boolean isBreakParse() {
193193 protected JSONObject response ;
194194 protected JSONObject sqlRequest ;
195195 protected JSONObject sqlReponse ;
196+ /**
197+ * 自定义关键词
198+ */
196199 protected Map <String , Object > customMap ;
197- protected Map <String , String > functionMap ;
200+ /**
201+ * 远程函数
202+ * {"-":{ "key-()":value }, "0":{ "key()":value }, "+":{ "key+()":value } }
203+ * - : 在executeSQL前解析
204+ * 0 : 在executeSQL后、onChildParse前解析
205+ * + : 在onChildParse后解析
206+ */
207+ protected Map <String , Map <String , String >> functionMap ;
208+ /**
209+ * 子对象
210+ */
198211 protected Map <String , JSONObject > childMap ;
199212
200213 /**解析成员
@@ -221,7 +234,7 @@ public AbstractObjectParser parse() throws Exception {
221234 customMap = new LinkedHashMap <String , Object >();
222235 childMap = new LinkedHashMap <String , JSONObject >();
223236 }
224- functionMap = new LinkedHashMap <String , String >();//必须执行
237+ functionMap = new LinkedHashMap <String , Map < String , String > >();//必须执行
225238
226239
227240 //条件<<<<<<<<<<<<<<<<<<<
@@ -283,6 +296,8 @@ else if (method == PUT && value instanceof JSONArray
283296 invalidate ();//忽略错误,还原request
284297 }
285298 }
299+
300+ onFunctionResponse ("-" );
286301 }
287302 }
288303
@@ -352,10 +367,35 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
352367 if (value instanceof String == false ) {
353368 throw new IllegalArgumentException (path + "/" + key + ":function() 后面必须为函数String!" );
354369 }
355- functionMap .put (key , (String ) value );
356- } else if (isTable && key .startsWith ("@" ) && JSONRequest .TABLE_KEY_LIST .contains (key ) == false ) {
370+
371+ String k = key .substring (0 , key .length () - 2 );
372+
373+ String type ; //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里用 "-","0","+" 更直观,转用 -1,0,1 对性能提升不大。
374+ if (k .endsWith ("-" )) {
375+ type = "-" ;
376+ k = k .substring (0 , k .length () - 1 );
377+ }
378+ else if (k .endsWith ("+" )) {
379+ type = "+" ;
380+ k = k .substring (0 , k .length () - 1 );
381+ }
382+ else {
383+ type = "0" ;
384+ }
385+
386+ //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
387+ Map <String , String > map = functionMap .get (type );
388+ if (map == null ) {
389+ map = new LinkedHashMap <>();
390+ }
391+ map .put (k , (String ) value );
392+
393+ functionMap .put (type , map );
394+ }
395+ else if (isTable && key .startsWith ("@" ) && JSONRequest .TABLE_KEY_LIST .contains (key ) == false ) {
357396 customMap .put (key , value );
358- } else {
397+ }
398+ else {
359399 sqlRequest .put (key , value );
360400 }
361401
@@ -554,18 +594,45 @@ public JSONObject response() throws Exception {
554594 }
555595
556596
597+ onFunctionResponse ("0" );
598+
599+ onChildResponse ();
600+
601+ onFunctionResponse ("+" );
602+
603+ onComplete ();
604+
605+ return response ;
606+ }
607+
608+
609+ @ Override
610+ public void onFunctionResponse (String type ) throws Exception {
611+ Map <String , String > map = functionMap == null ? null : functionMap .get (type );
557612
558613 //解析函数function
559- if (functionMap != null ) {
560- Set <Entry <String , String >> functionSet = functionMap == null ? null : functionMap .entrySet ();
561- if (functionSet != null && functionSet .isEmpty () == false ) {
562- for (Entry <String , String > entry : functionSet ) {
563- response .put (AbstractSQLConfig .getRealKey (method , entry .getKey (), false , false )
564- , onFunctionParse (response , entry .getValue ()));
614+ Set <Entry <String , String >> functionSet = map == null ? null : map .entrySet ();
615+ if (functionSet != null && functionSet .isEmpty () == false ) {
616+ JSONObject json = "-" .equals (type ) ? request : response ;
617+
618+ String key ;
619+ Object value ;
620+ for (Entry <String , String > entry : functionSet ) {
621+
622+ value = onFunctionParse (json , entry .getValue ());
623+
624+ if (value != null ) {
625+ key = AbstractSQLConfig .getRealKey (method , entry .getKey (), false , false );
626+
627+ response .put (key , value );
628+ parser .putQueryResult (AbstractParser .getAbsPath (path , key ), value );
565629 }
566630 }
567631 }
568-
632+ }
633+
634+ @ Override
635+ public void onChildResponse () throws Exception {
569636 //把isTable时取出去child解析后重新添加回来
570637 Set <Entry <String , JSONObject >> set = childMap == null ? null : childMap .entrySet ();
571638 if (set != null ) {
@@ -577,12 +644,9 @@ public JSONObject response() throws Exception {
577644 }
578645 }
579646 }
580-
581- onComplete ();
582-
583- return response ;
584647 }
585648
649+
586650 @ Override
587651 public Object onFunctionParse (JSONObject json , String function ) throws Exception {
588652 return null ;
@@ -712,7 +776,7 @@ public Map<String, Object> getCustomMap() {
712776 return customMap ;
713777 }
714778 @ Override
715- public Map <String , String > getFunctionMap () {
779+ public Map <String , Map < String , String > > getFunctionMap () {
716780 return functionMap ;
717781 }
718782 @ Override
0 commit comments