Skip to content

Commit 0991ece

Browse files
committed
Server:APIJSONORM完善getMaxSQLCount限制
1 parent 335879d commit 0991ece

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractObjectParser.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
101101

102102
this.objectCount = 0;
103103
this.arrayCount = 0;
104-
this.sqlCount = 0;
105-
104+
106105
boolean isEmpty = request.isEmpty();//empty有效 User:{}
107106
if (isEmpty) {
108107
this.tri = false;
@@ -222,7 +221,6 @@ public boolean isBreakParse() {
222221

223222
private int objectCount;
224223
private int arrayCount;
225-
private int sqlCount;
226224
/**解析成员
227225
* response重新赋值
228226
* @return null or this
@@ -273,7 +271,7 @@ public AbstractObjectParser parse() throws Exception {
273271
String key;
274272
Object value;
275273
int index = 0;
276-
274+
277275
for (Entry<String, Object> entry : set) {
278276
if (isBreakParse()) {
279277
break;
@@ -336,19 +334,19 @@ else if (method == PUT && value instanceof JSONArray
336334
@Override
337335
public boolean onParse(@NotNull String key, @NotNull Object value) throws Exception {
338336
if (key.endsWith("@")) {//StringUtil.isPath((String) value)) {
339-
337+
340338
if (value instanceof JSONObject) { // SQL 子查询对象,JSONObject -> SQLConfig.getSQL
341339
String replaceKey = key.substring(0, key.length() - 1);//key{}@ getRealKey
342-
340+
343341
JSONObject subquery = (JSONObject) value;
344342
String range = subquery.getString(JSONRequest.KEY_SUBQUERY_RANGE);
345343
if (range != null && JSONRequest.SUBQUERY_RANGE_ALL.equals(range) == false && JSONRequest.SUBQUERY_RANGE_ANY.equals(range) == false) {
346344
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ range:value } 中 value 只能为 [" + JSONRequest.SUBQUERY_RANGE_ALL + ", " + JSONRequest.SUBQUERY_RANGE_ANY + "] 中的一个!");
347345
}
348346

349-
347+
350348
JSONArray arr = parser.onArrayParse(subquery, AbstractParser.getAbsPath(path, replaceKey), "[]", true);
351-
349+
352350
JSONObject obj = arr == null || arr.isEmpty() ? null : arr.getJSONObject(0);
353351
if (obj == null) {
354352
throw new Exception("服务器内部错误,解析子查询 " + path + "/" + key + ":{ } 为 Subquery 对象失败!");
@@ -359,12 +357,12 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
359357
if (arrObj == null) {
360358
throw new IllegalArgumentException("子查询 " + path + "/" + key + ":{ from:value } 中 value 对应的主表对象 " + from + ":{} 不存在!");
361359
}
362-
//
360+
//
363361
SQLConfig cfg = (SQLConfig) arrObj.get(AbstractParser.KEY_CONFIG);
364362
if (cfg == null) {
365363
throw new NotExistException(TAG + ".onParse cfg == null");
366364
}
367-
365+
368366
Subquery s = new Subquery();
369367
s.setPath(path);
370368
s.setOriginKey(key);
@@ -493,7 +491,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
493491
if (arrayConfig == null || arrayConfig.getPosition() == 0) {
494492
arrayCount ++;
495493
}
496-
494+
497495
if (isMain) {
498496
throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
499497
+ "数组 []:{} 中第一个 key:{} 必须是主表 TableKey:{} !不能为 arrayKey[]:{} !");
@@ -510,7 +508,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
510508
if (arrayConfig == null || arrayConfig.getPosition() == 0) {
511509
arrayCount ++;
512510
}
513-
511+
514512
if (type == TYPE_ITEM && JSONRequest.isTableKey(Pair.parseEntry(key, true).getKey()) == false) {
515513
throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
516514
+ "数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{} !");
@@ -613,12 +611,6 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
613611
}
614612

615613
if (sqlConfig == null) {
616-
sqlCount ++;
617-
int maxSQLCount = parser.getMaxSQLCount();
618-
if (sqlCount > maxSQLCount) {
619-
throw new IllegalArgumentException(path + " 内生成的 SQL 为 " + sqlCount + " 已超限,必须在 0-" + maxSQLCount + " 内 !");
620-
}
621-
622614
sqlConfig = newSQLConfig();
623615
}
624616
sqlConfig.setCount(count).setPage(page).setPosition(position);
@@ -628,8 +620,8 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
628620
return this;
629621
}
630622

631-
632-
623+
624+
633625

634626
protected SQLConfig sqlConfig = null;//array item复用
635627
/**SQL查询,for array item

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ public JSONObject parseResponse(String request) {
240240
}
241241

242242
private int queryDepth;
243+
private int sqlCount;
244+
243245
/**解析请求json并获取对应结果
244246
* @param request
245247
* @return requestObject
@@ -299,6 +301,7 @@ public JSONObject parseResponse(JSONObject request) {
299301
sqlExecutor = createSQLExecutor();
300302
try {
301303
queryDepth = 0;
304+
sqlCount = 0;
302305
requestObject = onObjectParse(request, null, null, null, false);
303306
} catch (Exception e) {
304307
e.printStackTrace();
@@ -1269,6 +1272,13 @@ public synchronized JSONObject executeSQL(@NotNull SQLConfig config, boolean isS
12691272
return sqlObj;//容易丢失信息 JSON.parseObject(config);
12701273
}
12711274

1275+
sqlCount += 1;
1276+
int maxSQLCount = getMaxSQLCount();
1277+
Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n\n 已生成 " + sqlCount + "/" + maxSQLCount + "条 SQL \n\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
1278+
if (sqlCount > maxSQLCount) {
1279+
throw new IllegalArgumentException("已生成 " + sqlCount + "条 SQL,数量已超限,必须在 0-" + maxSQLCount + " 内 !");
1280+
}
1281+
12721282
try {
12731283
return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config));
12741284
} catch (Exception e) {

0 commit comments

Comments
 (0)