Skip to content

Commit 00d1452

Browse files
committedSep 7, 2022
解决耗时判断错误;优化对 executeQuery 还是 executeUpdate 的判断,兼容 nGQL, openCypher 等图数据库语言
1 parent 505f1c7 commit 00d1452

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed
 

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -1364,31 +1364,38 @@ public String execute(@RequestBody String request, HttpSession session) {
13641364
config.setPrepared(true);
13651365
config.setPreparedValueList(valueList);
13661366

1367+
String sqlPrefix = EXECUTE_STRICTLY ? sql.substring(0, 7).toUpperCase() : "";
1368+
boolean isWrite = sqlPrefix.startsWith("INSERT ") || sqlPrefix.startsWith("UPDATE ") || sqlPrefix.startsWith("DELETE ");
1369+
1370+
long executeStartTime = System.currentTimeMillis();
1371+
13671372
Statement statement = executor.getStatement(config, sql);
13681373
if (statement instanceof PreparedStatement) {
13691374
if (EXECUTE_STRICTLY) {
1370-
if (sql.startsWith("SELECT ")) {
1371-
((PreparedStatement) statement).executeQuery();
1372-
} else {
1375+
if (isWrite) {
13731376
((PreparedStatement) statement).executeUpdate();
1377+
} else {
1378+
((PreparedStatement) statement).executeQuery();
13741379
}
13751380
}
13761381
else {
13771382
((PreparedStatement) statement).execute();
13781383
}
13791384
} else {
13801385
if (EXECUTE_STRICTLY) {
1381-
if (sql.startsWith("SELECT ")) {
1382-
statement.executeQuery(sql);
1383-
} else {
1386+
if (isWrite) {
13841387
statement.executeUpdate(sql);
1388+
} else {
1389+
statement.executeQuery(sql);
13851390
}
13861391
}
13871392
else {
13881393
statement.execute(sql);
13891394
}
13901395
}
13911396

1397+
long executeDuration = System.currentTimeMillis() - executeStartTime;
1398+
13921399
ResultSet rs = statement.getResultSet();
13931400
ResultSetMetaData rsmd = rs.getMetaData();
13941401
int length = rsmd.getColumnCount();
@@ -1424,14 +1431,17 @@ public String execute(@RequestBody String request, HttpSession session) {
14241431
long endTime = System.currentTimeMillis();
14251432
long duration = endTime - startTime;
14261433

1427-
long sqlDuration = cursorDuration + rsDuration;
1434+
long sqlDuration = executeDuration + cursorDuration + rsDuration;
14281435
long parseDuration = duration - sqlDuration;
14291436

14301437
result.put("time:start|duration|end|parse|sql", startTime + "|" + duration + "|" + endTime + "|" + parseDuration + "|" + sqlDuration);
14311438

14321439
return result.toJSONString();
14331440
} catch (Exception e) {
1434-
return DemoParser.newErrorResult(e).toJSONString();
1441+
JSONObject result = DemoParser.newErrorResult(e);
1442+
result.put("throw", e.getClass().getName());
1443+
result.put("trace:stack", e.getStackTrace());
1444+
return result.toJSONString();
14351445
}
14361446

14371447
}

0 commit comments

Comments
 (0)
Please sign in to comment.