@@ -148,21 +148,24 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except
148
148
*/
149
149
@ Override
150
150
public JSONObject execute (@ NotNull SQLConfig config , boolean unknowType ) throws Exception {
151
- boolean prepared = config .isPrepared ();
151
+ boolean isPrepared = config .isPrepared ();
152
152
153
153
final String sql = config .getSQL (false );
154
154
155
- config .setPrepared (prepared );
155
+ config .setPrepared (isPrepared );
156
156
157
157
if (StringUtil .isEmpty (sql , true )) {
158
158
Log .e (TAG , "execute StringUtil.isEmpty(sql, true) >> return null;" );
159
159
return null ;
160
160
}
161
+
162
+ boolean isExplain = config .isExplain ();
163
+ boolean isHead = RequestMethod .isHeadMethod (config .getMethod (), true );
161
164
162
165
final int position = config .getPosition ();
163
166
JSONObject result ;
164
167
165
- if (config . isExplain () == false ) {
168
+ if (isExplain == false ) {
166
169
generatedSQLCount ++;
167
170
}
168
171
@@ -192,17 +195,6 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
192
195
}
193
196
else {
194
197
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
-
206
198
case POST :
207
199
case PUT :
208
200
case DELETE :
@@ -224,10 +216,12 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
224
216
result .put (config .getIdKey () + "[]" , config .getWhere (config .getIdKey () + "{}" , true ));
225
217
}
226
218
return result ;
227
-
219
+
228
220
case GET :
229
221
case GETS :
230
- result = getCacheItem (sql , position , config .getCache ());
222
+ case HEAD :
223
+ case HEADS :
224
+ result = isHead ? null : getCacheItem (sql , position , config .getCache ());
231
225
Log .i (TAG , ">>> execute result = getCache('" + sql + "', " + position + ") = " + result );
232
226
if (result != null ) {
233
227
cachedSQLCount ++;
@@ -238,7 +232,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
238
232
239
233
rs = executeQuery (config ); //FIXME SQL Server 是一次返回两个结果集,包括查询结果和执行计划,需要 moreResults
240
234
241
- if (config . isExplain () == false ) { //只有 SELECT 才能 EXPLAIN
235
+ if (isExplain == false ) { //只有 SELECT 才能 EXPLAIN
242
236
executedSQLCount ++;
243
237
}
244
238
break ;
@@ -249,58 +243,68 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
249
243
}
250
244
}
251
245
246
+
247
+ if (isExplain == false && isHead ) {
248
+ if (rs .next () == false ) {
249
+ return AbstractParser .newErrorResult (new SQLException ("数据库错误, rs.next() 失败!" ));
250
+ }
252
251
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");
253
262
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 ;
260
264
261
- ResultSetMetaData rsmd = rs .getMetaData ();
262
- final int length = rsmd .getColumnCount ();
265
+ ResultSetMetaData rsmd = rs .getMetaData ();
266
+ final int length = rsmd .getColumnCount ();
263
267
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吧
267
271
268
272
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 " );
274
278
275
- JSONObject item = new JSONObject (true );
279
+ JSONObject item = new JSONObject (true );
276
280
277
- for (int i = 1 ; i <= length ; i ++) {
281
+ for (int i = 1 ; i <= length ; i ++) {
278
282
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
+ // }
282
286
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 ();
286
290
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
+ }
292
297
}
293
- }
294
298
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
+ }
297
301
298
- resultList = onPutTable (config , rs , rsmd , resultList , index , item );
302
+ resultList = onPutTable (config , rs , rsmd , resultList , index , item );
299
303
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
+ }
302
307
}
303
-
304
308
}
305
309
finally {
306
310
if (rs != null ) {
@@ -317,50 +321,51 @@ else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
317
321
return null ;
318
322
}
319
323
320
- if (unknowType || config . isExplain () ) {
321
- if (config . isExplain () ) {
324
+ if (unknowType || isExplain ) {
325
+ if (isExplain ) {
322
326
if (result == null ) {
323
327
result = new JSONObject (true );
324
328
}
325
- boolean explain = config .isExplain ();
326
329
config .setExplain (false );
327
330
result .put ("sql" , config .getSQL (false ));
328
- config .setExplain (explain );
329
- config .setPrepared (prepared );
331
+ config .setExplain (isExplain );
332
+ config .setPrepared (isPrepared );
330
333
}
331
334
result .put ("list" , resultList );
332
335
return result ;
333
336
}
337
+
338
+ if (isHead == false ) {
339
+ // @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
334
340
335
- // @ APP JOIN 查询副表并缓存到 childMap <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
341
+ executeAppJoin ( config , resultList , childMap );
336
342
337
- executeAppJoin ( config , resultList , childMap );
343
+ // @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
338
344
339
- // @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
345
+ //子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
346
+ Set <Entry <String , JSONObject >> set = childMap .entrySet ();
340
347
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
+ }
343
354
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 ());
350
357
351
- putCache (sql , resultList , config .getCache ());
352
- Log .i (TAG , ">>> execute putCache('" + sql + "', resultList); resultList.size() = " + resultList .size ());
358
+ // 数组主表对象额外一次返回全部,方便 Parser 缓存来提高性能
353
359
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);" );
361
365
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
+ }
364
369
}
365
370
366
371
long endTime = System .currentTimeMillis ();
@@ -396,7 +401,7 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
396
401
}
397
402
continue ;
398
403
}
399
-
404
+
400
405
jc = j .getJoinConfig ();
401
406
402
407
//取出 "id@": "@/User/userId" 中所有 userId 的值
@@ -550,7 +555,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
550
555
}
551
556
}
552
557
}
553
-
558
+
554
559
}
555
560
556
561
Object value = getValue (config , rs , rsmd , tablePosition , table , columnIndex , lable , childMap );
@@ -561,7 +566,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
561
566
}
562
567
finalTable .put (lable , value );
563
568
}
564
-
569
+
565
570
return table ;
566
571
}
567
572
@@ -581,7 +586,7 @@ protected List<JSONObject> onPutTable(@NotNull SQLConfig config, @NotNull Result
581
586
return resultList ;
582
587
}
583
588
584
-
589
+
585
590
586
591
protected String getKey (@ NotNull SQLConfig config , @ NotNull ResultSet rs , @ NotNull ResultSetMetaData rsmd
587
592
, final int tablePosition , @ NotNull JSONObject table , final int columnIndex , Map <String , JSONObject > childMap ) throws Exception {
0 commit comments