Skip to content

Commit 0858141

Browse files
committed
远程函数:解决 key-() 优先执行结果不会作为 SQL 部分,解决非表对象中 key() 执行时机滞后于子对象
1 parent 31a5571 commit 0858141

File tree

5 files changed

+112
-96
lines changed

5 files changed

+112
-96
lines changed

APIJSONORM/src/main/java/apijson/Log.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Log {
1414

1515
public static boolean DEBUG = true;
1616

17-
public static final String VERSION = "5.3.0";
17+
public static final String VERSION = "5.4.0";
1818
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
1919

2020
public static final String OS_NAME;

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
144144

145145
/**反射调用
146146
* @param parser
147-
* @param request
148147
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
149-
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
148+
* @param currentObject
149+
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
150150
*/
151151
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject) throws Exception {
152152

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

Lines changed: 107 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import javax.activation.UnsupportedDataTypeException;
2222
import java.rmi.ServerException;
23-
import java.sql.SQLException;
2423
import java.util.ArrayList;
2524
import java.util.Arrays;
2625
import java.util.LinkedHashMap;
@@ -70,10 +69,6 @@ public AbstractObjectParser setParser(AbstractParser<?> parser) {
7069
protected final boolean drop;
7170

7271
/**for single object
73-
* @param parentPath
74-
* @param request
75-
* @param name
76-
* @throws Exception
7772
*/
7873
public AbstractObjectParser(@NotNull JSONObject request, String parentPath, SQLConfig arrayConfig
7974
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
@@ -157,7 +152,7 @@ public boolean isBreakParse() {
157152

158153
protected JSONObject response;
159154
protected JSONObject sqlRequest;
160-
protected JSONObject sqlReponse;
155+
protected JSONObject sqlResponse;
161156
/**
162157
* 自定义关键词
163158
*/
@@ -200,7 +195,7 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
200195
breakParse = false;
201196

202197
response = new JSONObject(true);//must init
203-
sqlReponse = null;//must init
198+
sqlResponse = null;//must init
204199

205200
if (isReuse == false) {
206201
sqlRequest = new JSONObject(true);//must init
@@ -239,23 +234,27 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
239234
}
240235
//条件>>>>>>>>>>>>>>>>>>>
241236

242-
String key;
243-
Object value;
244237
int index = 0;
238+
//hasOtherKeyNotFun = false;
245239

246240
for (Entry<String, Object> entry : set) {
247241
if (isBreakParse()) {
248242
break;
249243
}
250244

251-
value = entry.getValue();
245+
Object value = entry.getValue();
252246
if (value == null) {
253247
continue;
254248
}
255-
key = entry.getKey();
249+
String key = entry.getKey();
256250

257251
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)) {
259258
if (onParse(key, value) == false) {
260259
invalidate();
261260
}
@@ -331,8 +330,9 @@ else if (method == PUT && value instanceof JSONArray && (whereList == null || wh
331330

332331

333332

333+
//private boolean hasOtherKeyNotFun = false;
334334

335-
/**解析普通成员
335+
/**解析普通成员
336336
* @param key
337337
* @param value
338338
* @return whether parse succeed
@@ -438,23 +438,23 @@ else if (value instanceof String) { // //key{}@ getRealKey, 引用赋值路径
438438

439439
String type; //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里用 "-","0","+" 更直观,转用 -1,0,1 对性能提升不大。
440440
boolean isMinus = k.endsWith("-");
441+
boolean isPlus = isMinus == false && k.endsWith("+");
441442
if (isMinus) { //不能封装到functionMap后批量执行,否则会导致非Table内的 key-():function() 在onChildParse后执行!
442443
type = "-";
443444
k = k.substring(0, k.length() - 1);
444-
445-
if (isTable == false) {
446-
parseFunction(k, (String) value, parentPath, name, request);
447-
}
448445
}
449-
else if (k.endsWith("+")) {
446+
else if (isPlus) {
450447
type = "+";
451448
k = k.substring(0, k.length() - 1);
452449
}
453450
else {
454451
type = "0";
455452
}
456453

457-
if (isMinus == false || isTable) {
454+
if (isPlus == false && isTable == false) {
455+
parseFunction(k, (String) value, parentPath, name, request, isMinus);
456+
}
457+
else {
458458
//远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
459459
Map<String, String> map = functionMap.get(type);
460460
if (map == null) {
@@ -466,7 +466,7 @@ else if (k.endsWith("+")) {
466466
}
467467
}
468468
else if (isTable && key.startsWith("@") && JSONRequest.TABLE_KEY_LIST.contains(key) == false) {
469-
customMap.put(key, value);
469+
customMap.put(key, value);
470470
}
471471
else {
472472
sqlRequest.put(key, value);
@@ -718,37 +718,37 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
718718
public AbstractObjectParser executeSQL() throws Exception {
719719
//执行SQL操作数据库
720720
if (isTable == false) {//提高性能
721-
sqlReponse = new JSONObject(sqlRequest);
721+
sqlResponse = new JSONObject(sqlRequest);
722722
}
723723
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+
}
743745
}
744-
}
745-
}
746746

747-
if (drop) {//丢弃Table,只为了向下提供条件
748-
sqlReponse = null;
749-
}
747+
if (drop) {//丢弃Table,只为了向下提供条件
748+
sqlResponse = null;
749+
}
750750

751-
return this;
751+
return this;
752752
}
753753

754754
/**
@@ -757,12 +757,12 @@ public AbstractObjectParser executeSQL() throws Exception {
757757
*/
758758
@Override
759759
public JSONObject response() throws Exception {
760-
if (sqlReponse == null || sqlReponse.isEmpty()) {
760+
if (sqlResponse == null || sqlResponse.isEmpty()) {
761761
if (isTable) {//Table自身都获取不到值,则里面的Child都无意义,不需要再解析
762762
return null; // response;
763763
}
764764
} else {
765-
response.putAll(sqlReponse);
765+
response.putAll(sqlResponse);
766766
}
767767

768768

@@ -791,17 +791,22 @@ public void onFunctionResponse(String type) throws Exception {
791791
//解析函数function
792792
Set<Entry<String, String>> functionSet = map == null ? null : map.entrySet();
793793
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 是实时执行,而不是在这里批量执行
795796

796797
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);
798799
}
799800
}
800801
}
801802

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 {
803807
Object result;
804-
if (key.startsWith("@")) {
808+
boolean isProcedure = key.startsWith("@");
809+
if (isProcedure) {
805810
FunctionBean fb = AbstractFunctionParser.parseFunction(value, currentObject, true);
806811

807812
SQLConfig config = newSQLConfig(true);
@@ -814,12 +819,23 @@ public void parseFunction(String key, String value, String parentPath, String cu
814819
result = parser.onFunctionParse(key, value, parentPath, currentName, currentObject);
815820
}
816821

817-
if (result != null) {
818-
String k = AbstractSQLConfig.getRealKey(method, key, false, false);
822+
String k = AbstractSQLConfig.getRealKey(method, key, false, false);
819823

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);
823839
}
824840

825841
@Override
@@ -863,46 +879,46 @@ public JSONObject onSQLExecute() throws Exception {
863879
result = parser.executeSQL(sqlConfig, isSubquery);
864880

865881
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);
868884

869-
if (isArrayMainTable && position == 0 && rawList != null) {
885+
if (isArrayMainTable && position == 0 && rawList != null) {
870886

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));
875891

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);
878894

879-
if (isSimpleArray == false) {
880-
long startTime = System.currentTimeMillis();
895+
if (isSimpleArray == false) {
896+
long startTime = System.currentTimeMillis();
881897

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);
884900

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+
}
889905

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+
}
894910

895-
parser.putArrayMainCache(arrayPath, rawList);
896-
}
911+
parser.putArrayMainCache(arrayPath, rawList);
912+
}
897913

898-
if (isSubquery == false && result != null) {
899-
parser.putQueryResult(path, result); // 解决获取关联数据时requestObject里不存在需要的关联数据
914+
if (isSubquery == false && result != null) {
915+
parser.putQueryResult(path, result); // 解决获取关联数据时requestObject里不存在需要的关联数据
900916

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+
}
906922

907923
return result;
908924
}
@@ -941,7 +957,7 @@ public void recycle() {
941957
request = null;
942958
response = null;
943959
sqlRequest = null;
944-
sqlReponse = null;
960+
sqlResponse = null;
945961

946962
functionMap = null;
947963
customMap = null;
@@ -959,7 +975,7 @@ public AbstractObjectParser setMethod(RequestMethod method) {
959975
if (this.method != method) {
960976
this.method = method;
961977
sqlConfig = null;
962-
//TODO ? sqlReponse = null;
978+
//TODO ? sqlResponse = null;
963979
}
964980
return this;
965981
}
@@ -1007,8 +1023,8 @@ public JSONObject getSqlRequest() {
10071023
return sqlRequest;
10081024
}
10091025
@Override
1010-
public JSONObject getSqlReponse() {
1011-
return sqlReponse;
1026+
public JSONObject getSqlResponse() {
1027+
return sqlResponse;
10121028
}
10131029

10141030
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ public JSONObject onObjectParse(final JSONObject request
10841084
else {
10851085
// 对聚合函数字段通过 query:2 分页查总数返回值错误
10861086
RequestMethod method = op.getMethod();
1087-
rp = op.setMethod(RequestMethod.HEAD).setSQLConfig().executeSQL().getSqlReponse();
1087+
rp = op.setMethod(RequestMethod.HEAD).setSQLConfig().executeSQL().getSqlResponse();
10881088
op.setMethod(method);
10891089
}
10901090

APIJSONORM/src/main/java/apijson/orm/ObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public interface ObjectParser {
156156
SQLConfig getSQLConfig();
157157
JSONObject getResponse();
158158
JSONObject getSqlRequest();
159-
JSONObject getSqlReponse();
159+
JSONObject getSqlResponse();
160160

161161
Map<String, Object> getCustomMap();
162162
Map<String, Map<String, String>> getFunctionMap();

0 commit comments

Comments
 (0)