Skip to content

Commit 175bd81

Browse files
TommyLemonchenyanlann
authored andcommitted
分页: 在分页详情 info 内返回查询计划 @Explain
1 parent b1b814e commit 175bd81

File tree

3 files changed

+100
-89
lines changed

3 files changed

+100
-89
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,18 @@ public JSONObject onObjectParse(final JSONObject request
871871
}
872872

873873
JSONObject pagination = new JSONObject(true);
874+
Object explain = rp.get(JSONResponse.KEY_EXPLAIN);
875+
if (explain instanceof JSONObject) {
876+
pagination.put(JSONResponse.KEY_EXPLAIN, explain);
877+
}
874878
pagination.put(JSONResponse.KEY_TOTAL, total);
875879
pagination.put(JSONRequest.KEY_COUNT, count);
876880
pagination.put(JSONRequest.KEY_PAGE, page);
877881
pagination.put(JSONResponse.KEY_MAX, max);
878882
pagination.put(JSONResponse.KEY_MORE, page < max);
879883
pagination.put(JSONResponse.KEY_FIRST, page == 0);
880884
pagination.put(JSONResponse.KEY_LAST, page == max);
885+
881886
putQueryResult(pathPrefix + JSONResponse.KEY_INFO, pagination);
882887

883888
if (total <= count*page) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,12 +2647,13 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
26472647

26482648
config.setPreparedValueList(new ArrayList<Object>());
26492649
String column = config.getColumnString();
2650-
if(config.isOracle()){
2650+
if (config.isOracle()) {
26512651
//When config's database is oracle,Using subquery since Oracle12 below does not support OFFSET FETCH paging syntax.
2652-
return explain + "SELECT * FROM (SELECT"+ (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM "+getConditionString(column, tablePath, config)+ ") "+config.getLimitString();
2653-
}else
2654-
return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config);
2652+
return explain + "SELECT * FROM (SELECT"+ (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + ") " + config.getLimitString();
26552653
}
2654+
2655+
return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + config.getLimitString();
2656+
}
26562657
}
26572658

26582659
/**获取条件SQL字符串
@@ -2679,7 +2680,7 @@ private static String getConditionString(String column, String table, AbstractSQ
26792680

26802681
//no need to optimize
26812682
// if (config.getPage() <= 0 || ID.equals(column.trim())) {
2682-
return config.isOracle()? condition:condition + config.getLimitString();
2683+
return condition; // config.isOracle() ? condition : condition + config.getLimitString();
26832684
// }
26842685
//
26852686
//

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

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,24 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
148148
*/
149149
@Override
150150
public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws Exception {
151-
boolean prepared = config.isPrepared();
151+
boolean isPrepared = config.isPrepared();
152152

153153
final String sql = config.getSQL(false);
154154

155-
config.setPrepared(prepared);
155+
config.setPrepared(isPrepared);
156156

157157
if (StringUtil.isEmpty(sql, true)) {
158158
Log.e(TAG, "execute StringUtil.isEmpty(sql, true) >> return null;");
159159
return null;
160160
}
161+
162+
boolean isExplain = config.isExplain();
163+
boolean isHead = RequestMethod.isHeadMethod(config.getMethod(), true);
161164

162165
final int position = config.getPosition();
163166
JSONObject result;
164167

165-
if (config.isExplain() == false) {
168+
if (isExplain == false) {
166169
generatedSQLCount ++;
167170
}
168171

@@ -192,17 +195,6 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
192195
}
193196
else {
194197
switch (config.getMethod()) {
195-
case HEAD:
196-
case HEADS:
197-
rs = executeQuery(config);
198-
199-
executedSQLCount ++;
200-
201-
result = rs.next() ? AbstractParser.newSuccessResult()
202-
: AbstractParser.newErrorResult(new SQLException("数据库错误, rs.next() 失败!"));
203-
result.put(JSONResponse.KEY_COUNT, rs.getLong(1));
204-
return result;
205-
206198
case POST:
207199
case PUT:
208200
case DELETE:
@@ -224,10 +216,12 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
224216
result.put(config.getIdKey() + "[]", config.getWhere(config.getIdKey() + "{}", true));
225217
}
226218
return result;
227-
219+
228220
case GET:
229221
case GETS:
230-
result = getCacheItem(sql, position, config.getCache());
222+
case HEAD:
223+
case HEADS:
224+
result = isHead ? null : getCacheItem(sql, position, config.getCache());
231225
Log.i(TAG, ">>> execute result = getCache('" + sql + "', " + position + ") = " + result);
232226
if (result != null) {
233227
cachedSQLCount ++;
@@ -238,7 +232,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
238232

239233
rs = executeQuery(config); //FIXME SQL Server 是一次返回两个结果集,包括查询结果和执行计划,需要 moreResults
240234

241-
if (config.isExplain() == false) { //只有 SELECT 才能 EXPLAIN
235+
if (isExplain == false) { //只有 SELECT 才能 EXPLAIN
242236
executedSQLCount ++;
243237
}
244238
break;
@@ -249,58 +243,68 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
249243
}
250244
}
251245

246+
247+
if (isExplain == false && isHead) {
248+
if (rs.next() == false) {
249+
return AbstractParser.newErrorResult(new SQLException("数据库错误, rs.next() 失败!"));
250+
}
252251

252+
result = AbstractParser.newSuccessResult();
253+
result.put(JSONResponse.KEY_COUNT, rs.getLong(1));
254+
resultList = new ArrayList<>(1);
255+
resultList.add(result);
256+
}
257+
else {
258+
// final boolean cache = config.getCount() != 1;
259+
// TODO 设置初始容量为查到的数据量,解决频繁扩容导致的延迟,貌似只有 rs.last 取 rs.getRow() ? 然后又得 rs.beforeFirst 重置位置以便下方取值
260+
resultList = new ArrayList<>(config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount());
261+
// Log.d(TAG, "select cache = " + cache + "; resultList" + (resultList == null ? "=" : "!=") + "null");
253262

254-
// final boolean cache = config.getCount() != 1;
255-
// TODO 设置初始容量为查到的数据量,解决频繁扩容导致的延迟,貌似只有 rs.last 取 rs.getRow() ? 然后又得 rs.beforeFirst 重置位置以便下方取值
256-
resultList = new ArrayList<>(config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount());
257-
// Log.d(TAG, "select cache = " + cache + "; resultList" + (resultList == null ? "=" : "!=") + "null");
258-
259-
int index = -1;
263+
int index = -1;
260264

261-
ResultSetMetaData rsmd = rs.getMetaData();
262-
final int length = rsmd.getColumnCount();
265+
ResultSetMetaData rsmd = rs.getMetaData();
266+
final int length = rsmd.getColumnCount();
263267

264-
//<SELECT * FROM Comment WHERE momentId = '470', { id: 1, content: "csdgs" }>
265-
childMap = new HashMap<>(); //要存到cacheMap
266-
// WHERE id = ? AND ... 或 WHERE ... AND id = ? 强制排序 remove 再 put,还是重新 getSQL吧
268+
//<SELECT * FROM Comment WHERE momentId = '470', { id: 1, content: "csdgs" }>
269+
childMap = new HashMap<>(); //要存到cacheMap
270+
// WHERE id = ? AND ... 或 WHERE ... AND id = ? 强制排序 remove 再 put,还是重新 getSQL吧
267271

268272

269-
boolean hasJoin = config.hasJoin();
270-
int viceColumnStart = length + 1; //第一个副表字段的index
271-
while (rs.next()) {
272-
index ++;
273-
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n execute while (rs.next()){ index = " + index + "\n\n");
273+
boolean hasJoin = config.hasJoin();
274+
int viceColumnStart = length + 1; //第一个副表字段的index
275+
while (rs.next()) {
276+
index ++;
277+
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n execute while (rs.next()){ index = " + index + "\n\n");
274278

275-
JSONObject item = new JSONObject(true);
279+
JSONObject item = new JSONObject(true);
276280

277-
for (int i = 1; i <= length; i++) {
281+
for (int i = 1; i <= length; i++) {
278282

279-
// if (hasJoin && viceColumnStart > length && config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
280-
// viceColumnStart = i;
281-
// }
283+
// if (hasJoin && viceColumnStart > length && config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
284+
// viceColumnStart = i;
285+
// }
282286

283-
// bugfix-修复非常规数据库字段,获取表名失败导致输出异常
284-
if (config.isExplain() == false && hasJoin && viceColumnStart > length) {
285-
List<String> column = config.getColumn();
287+
// bugfix-修复非常规数据库字段,获取表名失败导致输出异常
288+
if (isExplain == false && hasJoin && viceColumnStart > length) {
289+
List<String> column = config.getColumn();
286290

287-
if (column != null && column.isEmpty() == false) {
288-
viceColumnStart = column.size() + 1;
289-
}
290-
else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
291-
viceColumnStart = i;
291+
if (column != null && column.isEmpty() == false) {
292+
viceColumnStart = column.size() + 1;
293+
}
294+
else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
295+
viceColumnStart = i;
296+
}
292297
}
293-
}
294298

295-
item = onPutColumn(config, rs, rsmd, index, item, i, config.isExplain() == false && hasJoin && i >= viceColumnStart ? childMap : null);
296-
}
299+
item = onPutColumn(config, rs, rsmd, index, item, i, isExplain == false && hasJoin && i >= viceColumnStart ? childMap : null);
300+
}
297301

298-
resultList = onPutTable(config, rs, rsmd, resultList, index, item);
302+
resultList = onPutTable(config, rs, rsmd, resultList, index, item);
299303

300-
Log.d(TAG, "\n execute while (rs.next()) { resultList.put( " + index + ", result); "
301-
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n");
304+
Log.d(TAG, "\n execute while (rs.next()) { resultList.put( " + index + ", result); "
305+
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n");
306+
}
302307
}
303-
304308
}
305309
finally {
306310
if (rs != null) {
@@ -317,50 +321,51 @@ else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
317321
return null;
318322
}
319323

320-
if (unknowType || config.isExplain()) {
321-
if (config.isExplain()) {
324+
if (unknowType || isExplain) {
325+
if (isExplain) {
322326
if (result == null) {
323327
result = new JSONObject(true);
324328
}
325-
boolean explain = config.isExplain();
326329
config.setExplain(false);
327330
result.put("sql", config.getSQL(false));
328-
config.setExplain(explain);
329-
config.setPrepared(prepared);
331+
config.setExplain(isExplain);
332+
config.setPrepared(isPrepared);
330333
}
331334
result.put("list", resultList);
332335
return result;
333336
}
337+
338+
if (isHead == false) {
339+
// @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
334340

335-
// @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
341+
executeAppJoin(config, resultList, childMap);
336342

337-
executeAppJoin(config, resultList, childMap);
343+
// @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
338344

339-
// @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
345+
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
346+
Set<Entry<String, JSONObject>> set = childMap.entrySet();
340347

341-
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
342-
Set<Entry<String, JSONObject>> set = childMap.entrySet();
348+
//<sql, Table>
349+
for (Entry<String, JSONObject> entry : set) {
350+
List<JSONObject> l = new ArrayList<>();
351+
l.add(entry.getValue());
352+
putCache(entry.getKey(), l, JSONRequest.CACHE_ROM);
353+
}
343354

344-
//<sql, Table>
345-
for (Entry<String, JSONObject> entry : set) {
346-
List<JSONObject> l = new ArrayList<>();
347-
l.add(entry.getValue());
348-
putCache(entry.getKey(), l, JSONRequest.CACHE_ROM);
349-
}
355+
putCache(sql, resultList, config.getCache());
356+
Log.i(TAG, ">>> execute putCache('" + sql + "', resultList); resultList.size() = " + resultList.size());
350357

351-
putCache(sql, resultList, config.getCache());
352-
Log.i(TAG, ">>> execute putCache('" + sql + "', resultList); resultList.size() = " + resultList.size());
358+
// 数组主表对象额外一次返回全部,方便 Parser 缓存来提高性能
353359

354-
// 数组主表对象额外一次返回全部,方便 Parser 缓存来提高性能
355-
356-
result = position >= resultList.size() ? new JSONObject() : resultList.get(position);
357-
if (position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false) {
358-
// 不是 main 不会直接执行,count=1 返回的不会超过 1 && config.isMain() && config.getCount() != 1
359-
Log.i(TAG, ">>> execute position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false"
360-
+ " >> result = new JSONObject(result); result.put(KEY_RAW_LIST, resultList);");
360+
result = position >= resultList.size() ? new JSONObject() : resultList.get(position);
361+
if (position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false) {
362+
// 不是 main 不会直接执行,count=1 返回的不会超过 1 && config.isMain() && config.getCount() != 1
363+
Log.i(TAG, ">>> execute position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false"
364+
+ " >> result = new JSONObject(result); result.put(KEY_RAW_LIST, resultList);");
361365

362-
result = new JSONObject(result);
363-
result.put(KEY_RAW_LIST, resultList);
366+
result = new JSONObject(result);
367+
result.put(KEY_RAW_LIST, resultList);
368+
}
364369
}
365370

366371
long endTime = System.currentTimeMillis();
@@ -396,7 +401,7 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
396401
}
397402
continue;
398403
}
399-
404+
400405
jc = j.getJoinConfig();
401406

402407
//取出 "id@": "@/User/userId" 中所有 userId 的值
@@ -550,7 +555,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
550555
}
551556
}
552557
}
553-
558+
554559
}
555560

556561
Object value = getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
@@ -561,7 +566,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
561566
}
562567
finalTable.put(lable, value);
563568
}
564-
569+
565570
return table;
566571
}
567572

@@ -581,7 +586,7 @@ protected List<JSONObject> onPutTable(@NotNull SQLConfig config, @NotNull Result
581586
return resultList;
582587
}
583588

584-
589+
585590

586591
protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
587592
, final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map<String, JSONObject> childMap) throws Exception {

0 commit comments

Comments
 (0)