@@ -176,127 +176,142 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
176176 + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
177177
178178 ResultSet rs = null ;
179+ List <JSONObject > resultList = null ;
180+ Map <String , JSONObject > childMap = null ;
179181
180- if (unknowType ) {
181- Statement statement = getStatement (config );
182- rs = execute (statement , sql );
182+ try {
183+ if (unknowType ) {
184+ Statement statement = getStatement (config );
185+ rs = execute (statement , sql );
186+
187+ result = new JSONObject (true );
188+ int updateCount = statement .getUpdateCount ();
189+ result .put (JSONResponse .KEY_COUNT , updateCount );
190+ result .put ("update" , updateCount >= 0 );
191+ //导致后面 rs.getMetaData() 报错 Operation not allowed after ResultSet closed result.put("moreResults", statement.getMoreResults());
192+ }
193+ else {
194+ switch (config .getMethod ()) {
195+ case HEAD :
196+ case HEADS :
197+ rs = executeQuery (config );
183198
184- result = new JSONObject (true );
185- int updateCount = statement .getUpdateCount ();
186- result .put (JSONResponse .KEY_COUNT , updateCount );
187- result .put ("update" , updateCount >= 0 );
188- //导致后面 rs.getMetaData() 报错 Operation not allowed after ResultSet closed result.put("moreResults", statement.getMoreResults());
189- }
190- else {
191- switch (config .getMethod ()) {
192- case HEAD :
193- case HEADS :
194- rs = executeQuery (config );
199+ executedSQLCount ++;
195200
196- executedSQLCount ++;
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 ;
197205
198- result = rs .next () ? AbstractParser .newSuccessResult ()
199- : AbstractParser .newErrorResult (new SQLException ("数据库错误, rs.next() 失败!" ));
200- result .put (JSONResponse .KEY_COUNT , rs .getLong (1 ));
206+ case POST :
207+ case PUT :
208+ case DELETE :
209+ executedSQLCount ++;
201210
202- rs .close ();
203- return result ;
211+ int updateCount = executeUpdate (config );
204212
205- case POST :
206- case PUT :
207- case DELETE :
208- executedSQLCount ++;
213+ result = AbstractParser .newResult (updateCount > 0 ? JSONResponse .CODE_SUCCESS : JSONResponse .CODE_NOT_FOUND
214+ , updateCount > 0 ? JSONResponse .MSG_SUCCEED : "没权限访问或对象不存在!" );
209215
210- int updateCount = executeUpdate (config );
216+ //id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
217+ result .put (JSONResponse .KEY_COUNT , updateCount );//返回修改的记录数
218+ if (config .getId () != null ) {
219+ result .put (config .getIdKey (), config .getId ());
220+ } else {
221+ result .put (config .getIdKey () + "[]" , config .getWhere (config .getIdKey () + "{}" , true ));
222+ }
223+ return result ;
211224
212- result = AbstractParser .newResult (updateCount > 0 ? JSONResponse .CODE_SUCCESS : JSONResponse .CODE_NOT_FOUND
213- , updateCount > 0 ? JSONResponse .MSG_SUCCEED : "没权限访问或对象不存在!" );
225+ case GET :
226+ case GETS :
227+ result = getCacheItem (sql , position , config .getCache ());
228+ Log .i (TAG , ">>> select result = getCache('" + sql + "', " + position + ") = " + result );
229+ if (result != null ) {
230+ cachedSQLCount ++;
214231
215- //id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
216- result .put (JSONResponse .KEY_COUNT , updateCount );//返回修改的记录数
217- if (config .getId () != null ) {
218- result .put (config .getIdKey (), config .getId ());
219- } else {
220- result .put (config .getIdKey () + "[]" , config .getWhere (config .getIdKey () + "{}" , true ));
221- }
222- return result ;
232+ Log .d (TAG , "\n \n select result != null >> return result;" + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n \n " );
233+ return result ;
234+ }
223235
224- case GET :
225- case GETS :
226- result = getCacheItem (sql , position , config .getCache ());
227- Log .i (TAG , ">>> select result = getCache('" + sql + "', " + position + ") = " + result );
228- if (result != null ) {
229- cachedSQLCount ++;
236+ rs = executeQuery (config ); //FIXME SQL Server 是一次返回两个结果集,包括查询结果和执行计划,需要 moreResults
230237
231- Log .d (TAG , "\n \n select result != null >> return result;" + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n \n " );
232- return result ;
233- }
234-
235- rs = executeQuery (config ); //FIXME SQL Server 是一次返回两个结果集,包括查询结果和执行计划,需要 moreResults
238+ if (config .isExplain () == false ) { //只有 SELECT 才能 EXPLAIN
239+ executedSQLCount ++;
240+ }
241+ break ;
236242
237- if (config .isExplain () == false ) { //只有 SELECT 才能 EXPLAIN
238- executedSQLCount ++;
243+ default ://OPTIONS, TRACE等
244+ Log .e (TAG , "select sql = " + sql + " ; method = " + config .getMethod () + " >> return null;" );
245+ return null ;
239246 }
240- break ;
241-
242- default ://OPTIONS, TRACE等
243- Log .e (TAG , "select sql = " + sql + " ; method = " + config .getMethod () + " >> return null;" );
244- return null ;
245247 }
246- }
247248
248249
249250
250- // final boolean cache = config.getCount() != 1;
251- List < JSONObject > resultList = new ArrayList <>();
252- // Log.d(TAG, "select cache = " + cache + "; resultList" + (resultList == null ? "=" : "!=") + "null");
251+ // final boolean cache = config.getCount() != 1;
252+ resultList = new ArrayList <>();
253+ // Log.d(TAG, "select cache = " + cache + "; resultList" + (resultList == null ? "=" : "!=") + "null");
253254
254- int index = -1 ;
255+ int index = -1 ;
255256
256- ResultSetMetaData rsmd = rs .getMetaData ();
257- final int length = rsmd .getColumnCount ();
257+ ResultSetMetaData rsmd = rs .getMetaData ();
258+ final int length = rsmd .getColumnCount ();
258259
259- //<SELECT * FROM Comment WHERE momentId = '470', { id: 1, content: "csdgs" }>
260- Map < String , JSONObject > childMap = new HashMap <>(); //要存到cacheMap
261- // WHERE id = ? AND ... 或 WHERE ... AND id = ? 强制排序 remove 再 put,还是重新 getSQL吧
260+ //<SELECT * FROM Comment WHERE momentId = '470', { id: 1, content: "csdgs" }>
261+ childMap = new HashMap <>(); //要存到cacheMap
262+ // WHERE id = ? AND ... 或 WHERE ... AND id = ? 强制排序 remove 再 put,还是重新 getSQL吧
262263
263264
264- boolean hasJoin = config .hasJoin ();
265- int viceColumnStart = length + 1 ; //第一个副表字段的index
266- while (rs .next ()) {
267- index ++;
268- Log .d (TAG , "\n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n select while (rs.next()){ index = " + index + "\n \n " );
265+ boolean hasJoin = config .hasJoin ();
266+ int viceColumnStart = length + 1 ; //第一个副表字段的index
267+ while (rs .next ()) {
268+ index ++;
269+ Log .d (TAG , "\n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n select while (rs.next()){ index = " + index + "\n \n " );
269270
270- JSONObject item = new JSONObject (true );
271+ JSONObject item = new JSONObject (true );
271272
272- for (int i = 1 ; i <= length ; i ++) {
273+ for (int i = 1 ; i <= length ; i ++) {
273274
274- // if (hasJoin && viceColumnStart > length && config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
275- // viceColumnStart = i;
276- // }
275+ // if (hasJoin && viceColumnStart > length && config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
276+ // viceColumnStart = i;
277+ // }
277278
278- // bugfix-修复非常规数据库字段,获取表名失败导致输出异常
279- if (config .isExplain () == false && hasJoin && viceColumnStart > length ) {
280- List <String > column = config .getColumn ();
279+ // bugfix-修复非常规数据库字段,获取表名失败导致输出异常
280+ if (config .isExplain () == false && hasJoin && viceColumnStart > length ) {
281+ List <String > column = config .getColumn ();
281282
282- if (column != null && column .isEmpty () == false ) {
283- viceColumnStart = column .size () + 1 ;
284- }
285- else if (config .getSQLTable ().equalsIgnoreCase (rsmd .getTableName (i )) == false ) {
286- viceColumnStart = i ;
283+ if (column != null && column .isEmpty () == false ) {
284+ viceColumnStart = column .size () + 1 ;
285+ }
286+ else if (config .getSQLTable ().equalsIgnoreCase (rsmd .getTableName (i )) == false ) {
287+ viceColumnStart = i ;
288+ }
287289 }
290+
291+ item = onPutColumn (config , rs , rsmd , index , item , i , config .isExplain () == false && hasJoin && i >= viceColumnStart ? childMap : null );
288292 }
289293
290- item = onPutColumn (config , rs , rsmd , index , item , i , config .isExplain () == false && hasJoin && i >= viceColumnStart ? childMap : null );
291- }
294+ resultList = onPutTable (config , rs , rsmd , resultList , index , item );
292295
293- resultList = onPutTable (config , rs , rsmd , resultList , index , item );
296+ Log .d (TAG , "\n select while (rs.next()) { resultList.put( " + index + ", result); "
297+ + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n \n " );
298+ }
294299
295- Log .d (TAG , "\n select while (rs.next()) { resultList.put( " + index + ", result); "
296- + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n \n " );
300+ }
301+ finally {
302+ if (rs != null ) {
303+ try {
304+ rs .close ();
305+ }
306+ catch (Exception e ) {
307+ e .printStackTrace ();
308+ }
309+ }
297310 }
298311
299- rs .close ();
312+ if (resultList == null ) {
313+ return null ;
314+ }
300315
301316 if (unknowType || config .isExplain ()) {
302317 if (config .isExplain ()) {
@@ -397,41 +412,51 @@ protected void executeAppJoin(SQLConfig config, List<JSONObject> resultList, Map
397412 + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n " );
398413
399414 //执行副表的批量查询 并 缓存到 childMap
400- ResultSet rs = executeQuery (jc );
415+ ResultSet rs = null ;
416+ try {
417+ rs = executeQuery (jc );
401418
402- int index = -1 ;
419+ int index = -1 ;
403420
404- ResultSetMetaData rsmd = rs .getMetaData ();
405- final int length = rsmd .getColumnCount ();
421+ ResultSetMetaData rsmd = rs .getMetaData ();
422+ final int length = rsmd .getColumnCount ();
406423
407- JSONObject result ;
408- String cacheSql ;
409- while (rs .next ()) { //FIXME 同时有 @ APP JOIN 和 < 等 SQL JOIN 时,next = false 总是无法进入循环,导致缓存失效,可能是连接池或线程问题
410- index ++;
411- Log .d (TAG , "\n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n executeAppJoin while (rs.next()){ index = " + index + "\n \n " );
424+ JSONObject result ;
425+ String cacheSql ;
426+ while (rs .next ()) { //FIXME 同时有 @ APP JOIN 和 < 等 SQL JOIN 时,next = false 总是无法进入循环,导致缓存失效,可能是连接池或线程问题
427+ index ++;
428+ Log .d (TAG , "\n \n <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n executeAppJoin while (rs.next()){ index = " + index + "\n \n " );
412429
413- result = new JSONObject (true );
430+ result = new JSONObject (true );
414431
415- for (int i = 1 ; i <= length ; i ++) {
432+ for (int i = 1 ; i <= length ; i ++) {
416433
417- result = onPutColumn (jc , rs , rsmd , index , result , i , null );
418- }
434+ result = onPutColumn (jc , rs , rsmd , index , result , i , null );
435+ }
419436
420- //每个 result 都要用新的 SQL 来存 childResultMap = onPutTable(config, rs, rsmd, childResultMap, index, result);
437+ //每个 result 都要用新的 SQL 来存 childResultMap = onPutTable(config, rs, rsmd, childResultMap, index, result);
421438
422- Log .d (TAG , "\n executeAppJoin while (rs.next()) { resultList.put( " + index + ", result); "
423- + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n \n " );
439+ Log .d (TAG , "\n executeAppJoin while (rs.next()) { resultList.put( " + index + ", result); "
440+ + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n \n " );
424441
425- //缓存到 childMap
426- cc .putWhere (j .getKey (), result .get (j .getKey ()), false );
427- cacheSql = cc .getSQL (false );
428- childMap .put (cacheSql , result );
442+ //缓存到 childMap
443+ cc .putWhere (j .getKey (), result .get (j .getKey ()), false );
444+ cacheSql = cc .getSQL (false );
445+ childMap .put (cacheSql , result );
429446
430- Log .d (TAG , ">>> executeAppJoin childMap.put('" + cacheSql + "', result); childMap.size() = " + childMap .size ());
447+ Log .d (TAG , ">>> executeAppJoin childMap.put('" + cacheSql + "', result); childMap.size() = " + childMap .size ());
448+ }
449+ }
450+ finally {
451+ if (rs != null ) {
452+ try {
453+ rs .close ();
454+ }
455+ catch (Exception e ) {
456+ e .printStackTrace ();
457+ }
458+ }
431459 }
432-
433- rs .close ();
434-
435460
436461 long endTime = System .currentTimeMillis ();
437462 Log .d (TAG , "\n \n executeAppJoin endTime = " + endTime + "; duration = " + (endTime - startTime )
@@ -576,6 +601,13 @@ else if (value instanceof Clob) { //SQL Server TEXT 类型 居然走这个
576601 s = br .readLine ();
577602 }
578603 value = sb .toString ();
604+
605+ try {
606+ br .close ();
607+ }
608+ catch (Exception e ) {
609+ e .printStackTrace ();
610+ }
579611 }
580612
581613 if (castToJson == false ) {
0 commit comments