Skip to content

Commit 1776cc1

Browse files
committed
Server:纠正并完善 sql count 的计算和显示;解决 newSQLConfig 抛 NotExistException 没被 catch 到导致最终返回错误结果
1 parent a794cd6 commit 1776cc1

File tree

5 files changed

+112
-45
lines changed

5 files changed

+112
-45
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,12 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
611611
}
612612

613613
if (sqlConfig == null) {
614-
sqlConfig = newSQLConfig(false);
614+
try {
615+
sqlConfig = newSQLConfig(false);
616+
}
617+
catch (NotExistException e) {
618+
return this;
619+
}
615620
}
616621
sqlConfig.setCount(count).setPage(page).setPosition(position);
617622

@@ -639,18 +644,19 @@ public AbstractObjectParser executeSQL() throws Exception {
639644
} else {
640645
try {
641646
sqlReponse = onSQLExecute();
642-
} catch (Exception e) {
643-
Log.e(TAG, "getObject try { response = getSQLObject(config2); } catch (Exception e) {");
644-
if (e instanceof NotExistException) {//非严重异常,有时候只是数据不存在
645-
// e.printStackTrace();
646-
sqlReponse = null;//内部吃掉异常,put到最外层
647-
// requestObject.put(JSONResponse.KEY_MSG
648-
// , StringUtil.getString(requestObject.get(JSONResponse.KEY_MSG)
649-
// + "; query " + path + " cath NotExistException:"
650-
// + newErrorResult(e).getString(JSONResponse.KEY_MSG)));
651-
} else {
652-
throw e;
653-
}
647+
}
648+
catch (NotExistException e) {
649+
// Log.e(TAG, "getObject try { response = getSQLObject(config2); } catch (Exception e) {");
650+
// if (e instanceof NotExistException) {//非严重异常,有时候只是数据不存在
651+
// // e.printStackTrace();
652+
sqlReponse = null;//内部吃掉异常,put到最外层
653+
// requestObject.put(JSONResponse.KEY_MSG
654+
// , StringUtil.getString(requestObject.get(JSONResponse.KEY_MSG)
655+
// + "; query " + path + " cath NotExistException:"
656+
// + newErrorResult(e).getString(JSONResponse.KEY_MSG)));
657+
// } else {
658+
// throw e;
659+
// }
654660
}
655661

656662
if (drop) {//丢弃Table,只为了向下提供条件
@@ -724,13 +730,11 @@ public void parseFunction(JSONObject json, String key, String value) throws Exce
724730

725731
SQLExecutor executor = null;
726732
try {
727-
executor = parser.createSQLExecutor();
733+
executor = parser.getSQLExecutor();
728734
result = executor.execute(config, true);
729735
}
730-
finally {
731-
if (executor != null) {
732-
executor.close();
733-
}
736+
catch (NotExistException e) {
737+
return;
734738
}
735739
}
736740
else {

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public AbstractParser<T> setRequest(JSONObject request) {
129129
}
130130

131131

132-
protected Verifier<T> verifier;
133132
protected RequestRole globleRole;
134133
public AbstractParser<T> setGlobleRole(RequestRole globleRole) {
135134
this.globleRole = globleRole;
@@ -199,9 +198,18 @@ public AbstractParser<T> setNoVerifyContent(boolean noVerifyContent) {
199198

200199

201200
protected SQLExecutor sqlExecutor;
201+
protected Verifier<T> verifier;
202202
protected Map<String, Object> queryResultMap;//path-result
203203

204-
204+
@Override
205+
public SQLExecutor getSQLExecutor() {
206+
return sqlExecutor;
207+
}
208+
@Override
209+
public Verifier<T> getVerifier() {
210+
return verifier;
211+
}
212+
205213
/**解析请求json并获取对应结果
206214
* @param request
207215
* @return
@@ -240,7 +248,6 @@ public JSONObject parseResponse(String request) {
240248
}
241249

242250
private int queryDepth;
243-
private int sqlCount;
244251

245252
/**解析请求json并获取对应结果
246253
* @param request
@@ -301,19 +308,22 @@ public JSONObject parseResponse(JSONObject request) {
301308
sqlExecutor = createSQLExecutor();
302309
try {
303310
queryDepth = 0;
304-
sqlCount = 0;
305311
requestObject = onObjectParse(request, null, null, null, false);
306312
} catch (Exception e) {
307313
e.printStackTrace();
308314
error = e;
309315
}
310-
sqlExecutor.close();
311-
sqlExecutor = null;
312-
313316

314317
requestObject = error == null ? extendSuccessResult(requestObject) : extendErrorResult(requestObject, error);
315318

316-
319+
if (Log.DEBUG) {
320+
requestObject.put("query:depth/max", queryDepth + "/" + getMaxQueryDepth());
321+
requestObject.put("sql:generate/cache/execute/maxExecute", sqlExecutor.getGeneratedSQLCount() + "/" + sqlExecutor.getCachedSQLCount() + "/" + sqlExecutor.getExecutedSQLCount() + "/" + getMaxSQLCount());
322+
}
323+
324+
sqlExecutor.close();
325+
sqlExecutor = null;
326+
317327
queryResultMap.clear();
318328

319329
//会不会导致原来的session = null? session = null;
@@ -329,11 +339,6 @@ public JSONObject parseResponse(JSONObject request) {
329339
Log.d(TAG, "parseResponse endTime = " + endTime + "; duration = " + (endTime - startTime)
330340
+ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
331341

332-
if (Log.DEBUG) {
333-
requestObject.put("query:depth/max", queryDepth + "/" + getMaxQueryDepth());
334-
requestObject.put("sql:count/max", sqlCount + "/" + getMaxSQLCount());
335-
}
336-
337342
return globleFormat && JSONResponse.isSuccess(requestObject) ? new JSONResponse(requestObject) : requestObject;
338343
}
339344

@@ -1274,28 +1279,37 @@ public static JSONObject getJSONObject(JSONObject object, String key) {
12741279
* @throws Exception
12751280
*/
12761281
@Override
1277-
public synchronized JSONObject executeSQL(@NotNull SQLConfig config, boolean isSubquery) throws Exception {
1282+
public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Exception {
1283+
if (config == null) {
1284+
Log.d(TAG, "executeSQL config == null >> return null;");
1285+
return null;
1286+
}
1287+
12781288
if (isSubquery) {
12791289
JSONObject sqlObj = new JSONObject(true);
12801290
sqlObj.put(KEY_CONFIG, config);
12811291
return sqlObj;//容易丢失信息 JSON.parseObject(config);
12821292
}
12831293

1284-
sqlCount += 1;//config.isMain() ? 1 : 0;
1285-
int maxSQLCount = getMaxSQLCount();
1286-
Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n\n 已生成 " + sqlCount + "/" + maxSQLCount + "条 SQL \n\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
1287-
if (sqlCount > maxSQLCount) {
1288-
throw new IllegalArgumentException("截至 " + config.getTable() + " 已生成 " + sqlCount + "条 SQL,数量已超限,必须在 0-" + maxSQLCount + " 内 !");
1289-
}
1290-
12911294
try {
12921295
return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config, false));
1293-
} catch (Exception e) {
1296+
}
1297+
catch (Exception e) {
12941298
if (Log.DEBUG == false && e instanceof SQLException) {
12951299
throw new SQLException("数据库驱动执行异常SQLException,非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e);
12961300
}
12971301
throw e;
12981302
}
1303+
finally {
1304+
if (config.getPosition() == 0) {
1305+
int maxSQLCount = getMaxSQLCount();
1306+
int sqlCount = sqlExecutor.getExecutedSQLCount();
1307+
Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n\n 已执行 " + sqlCount + "/" + maxSQLCount + " 条 SQL \n\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
1308+
if (sqlCount > maxSQLCount) {
1309+
throw new IllegalArgumentException("截至 " + config.getTable() + " 已执行 " + sqlCount + " 条 SQL,数量已超限,必须在 0-" + maxSQLCount + " 内 !");
1310+
}
1311+
}
1312+
}
12991313
}
13001314

13011315

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public abstract class AbstractSQLExecutor implements SQLExecutor {
4141
private static final String TAG = "AbstractSQLExecutor";
4242

4343

44+
private int generatedSQLCount;
45+
private int cachedSQLCount;
46+
private int executedSQLCount;
47+
public AbstractSQLExecutor() {
48+
generatedSQLCount = 0;
49+
cachedSQLCount = 0;
50+
executedSQLCount = 0;
51+
}
52+
53+
@Override
54+
public int getGeneratedSQLCount() {
55+
return generatedSQLCount;
56+
}
57+
@Override
58+
public int getCachedSQLCount() {
59+
return cachedSQLCount;
60+
}
61+
@Override
62+
public int getExecutedSQLCount() {
63+
return executedSQLCount;
64+
}
65+
4466
//访问一次后丢失,可能因为static导致内存共享,别的地方改变了内部对象的值
4567
// private static final Map<String, Map<Integer, JSONObject>> staticCacheMap;
4668
// static {
@@ -115,18 +137,28 @@ public JSONObject getCacheItem(String sql, int position, boolean isStatic) {
115137
public void close() {
116138
cacheMap.clear();
117139
cacheMap = null;
140+
141+
generatedSQLCount = 0;
142+
cachedSQLCount = 0;
143+
executedSQLCount = 0;
118144
}
119145

120146
@Override
121147
public ResultSet executeQuery(@NotNull Statement statement, String sql) throws Exception {
148+
executedSQLCount ++;
149+
122150
return statement.executeQuery(sql);
123151
}
124152
@Override
125153
public int executeUpdate(@NotNull Statement statement, String sql) throws Exception {
154+
executedSQLCount ++;
155+
126156
return statement.executeUpdate(sql);
127157
}
128158
@Override
129159
public ResultSet execute(@NotNull Statement statement, String sql) throws Exception {
160+
executedSQLCount ++;
161+
130162
statement.execute(sql);
131163
return statement.getResultSet();
132164
}
@@ -138,10 +170,6 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
138170
*/
139171
@Override
140172
public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws Exception {
141-
// if (config == null) {
142-
// Log.e(TAG, "select config==null >> return null;");
143-
// return null;
144-
// }
145173
boolean prepared = config.isPrepared();
146174

147175
final String sql = config.getSQL(false);
@@ -156,8 +184,11 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
156184
final int position = config.getPosition();
157185
JSONObject result;
158186

187+
generatedSQLCount ++;
188+
159189
long startTime = System.currentTimeMillis();
160190
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
191+
+ "\n 已生成 " + generatedSQLCount + " 条 SQL"
161192
+ "\n select startTime = " + startTime
162193
+ "\n sql = \n " + sql
163194
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
@@ -178,6 +209,8 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
178209
switch (config.getMethod()) {
179210
case HEAD:
180211
case HEADS:
212+
executedSQLCount ++;
213+
181214
rs = executeQuery(config);
182215

183216
result = rs.next() ? AbstractParser.newSuccessResult()
@@ -190,6 +223,8 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
190223
case POST:
191224
case PUT:
192225
case DELETE:
226+
executedSQLCount ++;
227+
193228
int updateCount = executeUpdate(config);
194229

195230
result = AbstractParser.newResult(updateCount > 0 ? JSONResponse.CODE_SUCCESS : JSONResponse.CODE_NOT_FOUND
@@ -209,10 +244,14 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
209244
result = getCacheItem(sql, position, config.isCacheStatic());
210245
Log.i(TAG, ">>> select result = getCache('" + sql + "', " + position + ") = " + result);
211246
if (result != null) {
247+
cachedSQLCount ++;
248+
212249
Log.d(TAG, "\n\n select result != null >> return result;" + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
213250
return result;
214251
}
215252

253+
executedSQLCount ++;
254+
216255
rs = executeQuery(config);
217256
break;
218257

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface Parser<T> {
2929
int MAX_QUERY_PAGE = 100;
3030
int MAX_QUERY_COUNT = 100;
3131
int MAX_UPDATE_COUNT = 10;
32-
int MAX_SQL_COUNT = 1000;
32+
int MAX_SQL_COUNT = 200;
3333
int MAX_OBJECT_COUNT = 5;
3434
int MAX_ARRAY_COUNT = 5;
3535
int MAX_QUERY_DEPTH = 5;
@@ -122,5 +122,8 @@ public interface Parser<T> {
122122
void onVerifyRole(SQLConfig config) throws Exception;
123123

124124
JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Exception;
125+
126+
SQLExecutor getSQLExecutor();
127+
Verifier<T> getVerifier();
125128

126129
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,12 @@ public interface SQLExecutor {
9393
int executeUpdate(@NotNull Statement statement, String sql) throws Exception;
9494

9595
ResultSet execute(@NotNull Statement statement, String sql) throws Exception;
96+
97+
int getGeneratedSQLCount();
98+
99+
int getCachedSQLCount();
100+
101+
int getExecutedSQLCount();
102+
96103

97104
}

0 commit comments

Comments
 (0)