Skip to content

Commit 198d897

Browse files
committed
Server:Parser新增getMaxQueryCount和getMaxUpdateCount,可重写方法来自定义最大查询数量和最大增删改数量
1 parent 6421c4e commit 198d897

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoParser.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ protected void onVerifyContent() throws Exception {
128128

129129
@Override
130130
public JSONObject parseCorrectRequest(JSONObject target) throws Exception {
131-
return Structure.parseRequest(requestMethod, "", target, requestObject, this);
131+
return Structure.parseRequest(requestMethod, "", target, requestObject, getMaxUpdateCount(), this);
132132
}
133133

134+
// //可重写来设置最大查询数量
135+
// @Override
136+
// public int getMaxQueryCount() {
137+
// return 50;
138+
// }
139+
134140
}

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
670670

671671

672672
//不用total限制数量了,只用中断机制,total只在query = 1,2的时候才获取
673-
int size = count <= 0 || count > 100 ? 100 : count;//count为每页数量,size为第page页实际数量,max(size) = count
673+
int max = getMaxQueryCount();
674+
int size = count <= 0 || count > max ? max : count;//count为每页数量,size为第page页实际数量,max(size) = count
674675
Log.d(TAG, "getArray size = " + size + "; page = " + page);
675676

676677

@@ -907,6 +908,15 @@ private JSONObject getJoinObject(String table, JSONObject obj, String key) {
907908
return requestObj;
908909
}
909910

911+
@Override
912+
public int getMaxQueryCount() {
913+
return MAX_QUERY_COUNT;
914+
}
915+
@Override
916+
public int getMaxUpdateCount() {
917+
return MAX_UPDATE_COUNT;
918+
}
919+
910920

911921
/**根据路径取值
912922
* @param parent

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,7 @@ public static List<Join> parseJoin(RequestMethod method, List<Join> joinList, Ca
18721872
String name;
18731873
for (Join j : joinList) {
18741874
name = j.getName();
1875+
//JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
18751876
SQLConfig joinConfig = newSQLConfig(method, name, j.getTable(), null, callback).setMain(false).setKeyPrefix(true);
18761877
SQLConfig cacheConfig = newSQLConfig(method, name, j.getTable(), null, callback).setCount(1);
18771878

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525
public interface Parser {
2626

27+
int MAX_QUERY_COUNT = 100;
28+
int MAX_UPDATE_COUNT = 10;
29+
30+
2731
@NotNull
2832
Visitor getVisitor();
2933
Parser setVisitor(@NotNull Visitor visitor);
@@ -83,6 +87,9 @@ public interface Parser {
8387

8488
ObjectParser createObjectParser(JSONObject request, String parentPath, String name, SQLConfig arrayConfig) throws Exception;
8589

90+
int getMaxQueryCount();
91+
int getMaxUpdateCount();
92+
8693
void putQueryResult(String path, Object result);
8794

8895

@@ -91,5 +98,4 @@ public interface Parser {
9198

9299
JSONObject executeSQL(SQLConfig config) throws Exception;
93100

94-
95101
}

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,34 @@
5959
public class Structure {
6060
private static final String TAG = "Structure";
6161

62-
63-
6462
private Structure() {}
6563

6664

67-
68-
6965
/**从request提取target指定的内容
7066
* @param method
7167
* @param name
7268
* @param target
7369
* @param request
74-
* @param creator
70+
* @param creator
7571
* @return
7672
* @throws Exception
7773
*/
7874
public static JSONObject parseRequest(@NotNull final RequestMethod method, final String name
7975
, final JSONObject target, final JSONObject request, final SQLCreator creator) throws Exception {
76+
return parseRequest(method, name, target, request, Parser.MAX_UPDATE_COUNT, creator);
77+
}
78+
/**从request提取target指定的内容
79+
* @param method
80+
* @param name
81+
* @param target
82+
* @param request
83+
* @param maxUpdateCount
84+
* @param creator
85+
* @return
86+
* @throws Exception
87+
*/
88+
public static JSONObject parseRequest(@NotNull final RequestMethod method, final String name
89+
, final JSONObject target, final JSONObject request, final int maxUpdateCount, final SQLCreator creator) throws Exception {
8090
Log.i(TAG, "parseRequest method = " + method + "; name = " + name
8191
+ "; target = \n" + JSON.toJSONString(target)
8292
+ "\n request = \n" + JSON.toJSONString(request));
@@ -107,13 +117,13 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
107117
}
108118
} else {
109119
if (RequestMethod.isQueryMethod(method) == false) {
110-
verifyId(method.name(), name, key, robj, KEY_ID, true);
111-
verifyId(method.name(), name, key, robj, KEY_USER_ID, false);
120+
verifyId(method.name(), name, key, robj, KEY_ID, maxUpdateCount, true);
121+
verifyId(method.name(), name, key, robj, KEY_USER_ID, maxUpdateCount, false);
112122
}
113123
}
114124
}
115125

116-
return parseRequest(method, key, tobj, robj, creator);
126+
return parseRequest(method, key, tobj, robj, maxUpdateCount, creator);
117127
}
118128
});
119129

@@ -128,7 +138,7 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
128138
* @param atLeastOne 至少有一个不为null
129139
*/
130140
private static void verifyId(@NotNull String method, @NotNull String name, @NotNull String key
131-
, @NotNull JSONObject robj, @NotNull String idKey, boolean atLeastOne) {
141+
, @NotNull JSONObject robj, @NotNull String idKey, final int maxUpdateCount, boolean atLeastOne) {
132142
//单个修改或删除
133143
Object id = null;
134144
try {
@@ -154,9 +164,9 @@ private static void verifyId(@NotNull String method, @NotNull String name, @NotN
154164
+ " 里面 " + idKey + " 和 " + idInKey + " 至少传其中一个!");
155165
}
156166
} else {
157-
if (idIn.size() > 10) { //不允许一次操作10条以上记录
167+
if (idIn.size() > maxUpdateCount) { //不允许一次操作 maxUpdateCount 条以上记录
158168
throw new IllegalArgumentException(method + "请求," + name + "/" + key
159-
+ " 里面的 " + idInKey + ":[] 中[]的长度不能超过10!");
169+
+ " 里面的 " + idInKey + ":[] 中[]的长度不能超过 " + maxUpdateCount + " !");
160170
}
161171
//解决 id{}: ["1' OR 1='1'))--"] 绕过id{}限制
162172
//new ArrayList<Long>(idIn) 不能检查类型,Java泛型擦除问题,居然能把 ["a"] 赋值进去还不报错

0 commit comments

Comments
 (0)