@@ -350,23 +350,25 @@ public List<Map> all(String query, Object ... params) {
350
350
* This method returns entire resultset as one list. Do not use it for large result sets.
351
351
* Example:
352
352
* <pre>
353
- * List<Map> people = db.findAll("select * from people where first_name = ?", "John");
354
- * for (Map person : people)
355
- * System.out.println(person.get("first_name"));
353
+ * <code>List<Map<String, Object>> people = Base.findAll("select * from people where first_name = ?", "John");
354
+ * for(Map person: people)
355
+ * System.out.println(person.get("first_name"));
356
+ * </code>
356
357
* </pre>
357
358
*
358
359
* @param query raw SQL query. This query is parametrized.
359
360
* @param params list of parameters for a parametrized query.
360
361
* @return entire result set corresponding to the query.
361
362
*/
362
- public List <Map > findAll (String query , Object ... params ) {
363
+ public List <Map > findAll (String query , Object ... params ) {
364
+
363
365
final List <Map > results = new ArrayList <Map >();
364
366
long start = System .currentTimeMillis ();
365
- findWith (new RowListenerAdapter () {
367
+ find ( query , params ). with (new RowListenerAdapter () {
366
368
@ Override public void onNext (Map <String , Object > row ) {
367
369
results .add (row );
368
370
}
369
- }, false , query , params );
371
+ });
370
372
LogFilter .logQuery (logger , query , params , start );
371
373
return results ;
372
374
}
@@ -423,13 +425,14 @@ public List<Map> all(String query) {
423
425
* @return entire result set corresponding to the query.
424
426
*/
425
427
public List <Map > findAll (String query ) {
426
- final List <Map > results = new ArrayList <Map >();
428
+
429
+ final ArrayList <Map > results = new ArrayList <Map >();
427
430
long start = System .currentTimeMillis ();
428
- findWith (new RowListenerAdapter () {
431
+ find ( query ). with (new RowListenerAdapter () {
429
432
@ Override public void onNext (Map <String , Object > row ) {
430
433
results .add (row );
431
434
}
432
- }, false , query );
435
+ });
433
436
434
437
LogFilter .logQuery (logger , query , null , start );
435
438
return results ;
@@ -451,60 +454,27 @@ public void onNext(Map row) {
451
454
* @param params list of parameters if query is parametrized.
452
455
* @return instance of <code>RowProcessor</code> which has with() method for convenience.
453
456
*/
454
- public RowProcessor find (String query , Object ... params ) {
457
+ public RowProcessor find (String query , Object ... params ) {
458
+
459
+ //TODO: count ? signs and number of params, throw exception if do not match
460
+
455
461
if (query .indexOf ('?' ) == -1 && params .length != 0 ) throw new IllegalArgumentException ("you passed arguments, but the query does not have placeholders: (?)" );
456
462
457
463
if (!SELECT_PATTERN .matcher (query ).find ()) { throw new IllegalArgumentException ("query must be 'select' query" ); }
458
464
459
- try {
460
- PreparedStatement ps = prepareStreamingStatement (query );
461
- setParameters (ps , params );
462
- ResultSet rs = ps .executeQuery ();
463
- return new RowProcessor (rs , ps );
464
- // ps and rs will be closed by RowProcessor
465
- } catch (SQLException e ) { throw new DBException (query , params , e ); }
466
- }
467
-
468
- /**
469
- * Executes a raw query and calls the listener to handle the results. The listener should extend
470
- * {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
471
- * ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
472
- * create a streaming PreparedStatement (currently only available for MySQL). Example:
473
- *
474
- * <blockquote><pre>
475
- * db.findWith(new RowListenerAdapter() {
476
- * @Override public void onNext(Map row) {
477
- * // write your code here
478
- * Object o1 = row.get("first_name");
479
- * Object o2 = row.get("last_name");
480
- * }
481
- * }, true, "select first_name, last_name from really_large_table");
482
- * </pre></blockquote>
483
- *
484
- * @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
485
- * {@link ResultSetListener} to process the whole ResultSet
486
- * @param streaming true to create a streaming PreparedStatement, false otherwise
487
- * @param query raw SQL query
488
- * @param params parameters of parametrized query
489
- */
490
- public void findWith (ResultSetListener listener , boolean streaming , String query , Object ... params ) {
491
465
//TODO: cache prepared statements here too
492
- PreparedStatement ps = null ;
493
- ResultSet rs = null ;
466
+ PreparedStatement ps ;
467
+ ResultSet rs ;
494
468
try {
495
- ps = streaming ? prepareStreamingStatement ( query ) : connection (). prepareStatement (query );
469
+ ps = createStreamingPreparedStatement (query );
496
470
setParameters (ps , params );
497
471
rs = ps .executeQuery ();
498
- listener .onResultSet (rs );
499
- } catch (SQLException e ) {
500
- throw new DBException (query , params , e );
501
- } finally {
502
- closeQuietly (rs );
503
- closeQuietly (ps );
504
- }
472
+ return new RowProcessor (rs , ps );
473
+
474
+ } catch (SQLException e ) { throw new DBException (query , params , e ); }
505
475
}
506
476
507
- private PreparedStatement prepareStreamingStatement (String query ) throws SQLException {
477
+ private PreparedStatement createStreamingPreparedStatement (String query ) throws SQLException {
508
478
Connection conn = connection ();
509
479
PreparedStatement res ;
510
480
if ("mysql" .equalsIgnoreCase (conn .getMetaData ().getDatabaseProductName ())) {
@@ -524,29 +494,16 @@ private PreparedStatement prepareStreamingStatement(String query) throws SQLExce
524
494
* @param listener client listener implementation for processing individual rows.
525
495
*/
526
496
public void find (String sql , RowListener listener ) {
527
- findWith (listener , true , sql );
528
- }
529
497
530
- /**
531
- * Executes a raw query and calls the listener to handle the results. The listener should extend
532
- * {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
533
- * ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
534
- * create a streaming Statement (currently only available for MySQL).
535
- *
536
- * @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
537
- * {@link ResultSetListener} to handle the whole ResultSet
538
- * @param streaming true to create a streaming Statement, false otherwise
539
- * @param query raw SQL query
540
- */
541
- public void findWith (ResultSetListener listener , boolean streaming , String query ) {
542
498
Statement s = null ;
543
499
ResultSet rs = null ;
544
500
try {
545
- s = streaming ? createStreamingStatement () : connection ().createStatement ();
546
- rs = s .executeQuery (query );
547
- listener .onResultSet (rs );
501
+ s = createStreamingStatement ();
502
+ rs = s .executeQuery (sql );
503
+ RowProcessor p = new RowProcessor (rs , s );
504
+ p .with (listener );
548
505
} catch (SQLException e ) {
549
- throw new DBException (query , null , e );
506
+ throw new DBException (sql , null , e );
550
507
} finally {
551
508
closeQuietly (rs );
552
509
closeQuietly (s );
@@ -687,7 +644,8 @@ Object execInsert(String query, String autoIncrementColumnName, Object... params
687
644
} catch (SQLException e ) {
688
645
throw new DBException (query , params , e );
689
646
} finally {
690
- // don't close ps as it is cached!
647
+ // don't close ps as it could have come from the cache!
648
+ //TODO: close ps if not cached?
691
649
}
692
650
}
693
651
0 commit comments