102
102
* @author Rod Johnson
103
103
* @author Juergen Hoeller
104
104
* @author Thomas Risberg
105
+ * @author Yanming Zhou
105
106
* @since May 3, 2001
106
107
* @see JdbcOperations
107
108
* @see PreparedStatementCreator
@@ -473,12 +474,12 @@ public String getSql() {
473
474
474
475
@ Override
475
476
public void query (String sql , RowCallbackHandler rch ) throws DataAccessException {
476
- query (sql , new RowCallbackHandlerResultSetExtractor (rch ));
477
+ query (sql , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
477
478
}
478
479
479
480
@ Override
480
481
public <T > List <T > query (String sql , RowMapper <T > rowMapper ) throws DataAccessException {
481
- return result (query (sql , new RowMapperResultSetExtractor <>(rowMapper )));
482
+ return result (query (sql , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
482
483
}
483
484
484
485
@ Override
@@ -488,7 +489,7 @@ class StreamStatementCallback implements StatementCallback<Stream<T>>, SqlProvid
488
489
public Stream <T > doInStatement (Statement stmt ) throws SQLException {
489
490
ResultSet rs = stmt .executeQuery (sql );
490
491
Connection con = stmt .getConnection ();
491
- return new ResultSetSpliterator <>(rs , rowMapper ).stream ().onClose (() -> {
492
+ return new ResultSetSpliterator <>(rs , rowMapper , JdbcTemplate . this . maxRows ).stream ().onClose (() -> {
492
493
JdbcUtils .closeResultSet (rs );
493
494
JdbcUtils .closeStatement (stmt );
494
495
DataSourceUtils .releaseConnection (con , getDataSource ());
@@ -756,12 +757,12 @@ private String appendSql(@Nullable String sql, String statement) {
756
757
757
758
@ Override
758
759
public void query (PreparedStatementCreator psc , RowCallbackHandler rch ) throws DataAccessException {
759
- query (psc , new RowCallbackHandlerResultSetExtractor (rch ));
760
+ query (psc , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
760
761
}
761
762
762
763
@ Override
763
764
public void query (String sql , @ Nullable PreparedStatementSetter pss , RowCallbackHandler rch ) throws DataAccessException {
764
- query (sql , pss , new RowCallbackHandlerResultSetExtractor (rch ));
765
+ query (sql , pss , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
765
766
}
766
767
767
768
@ Override
@@ -782,28 +783,28 @@ public void query(String sql, RowCallbackHandler rch, @Nullable Object @Nullable
782
783
783
784
@ Override
784
785
public <T > List <T > query (PreparedStatementCreator psc , RowMapper <T > rowMapper ) throws DataAccessException {
785
- return result (query (psc , new RowMapperResultSetExtractor <>(rowMapper )));
786
+ return result (query (psc , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
786
787
}
787
788
788
789
@ Override
789
790
public <T > List <T > query (String sql , @ Nullable PreparedStatementSetter pss , RowMapper <T > rowMapper ) throws DataAccessException {
790
- return result (query (sql , pss , new RowMapperResultSetExtractor <>(rowMapper )));
791
+ return result (query (sql , pss , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
791
792
}
792
793
793
794
@ Override
794
795
public <T > List <T > query (String sql , @ Nullable Object @ Nullable [] args , int [] argTypes , RowMapper <T > rowMapper ) throws DataAccessException {
795
- return result (query (sql , args , argTypes , new RowMapperResultSetExtractor <>(rowMapper )));
796
+ return result (query (sql , args , argTypes , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
796
797
}
797
798
798
799
@ Deprecated
799
800
@ Override
800
801
public <T > List <T > query (String sql , @ Nullable Object @ Nullable [] args , RowMapper <T > rowMapper ) throws DataAccessException {
801
- return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper )));
802
+ return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
802
803
}
803
804
804
805
@ Override
805
806
public <T > List <T > query (String sql , RowMapper <T > rowMapper , @ Nullable Object @ Nullable ... args ) throws DataAccessException {
806
- return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper )));
807
+ return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
807
808
}
808
809
809
810
/**
@@ -828,7 +829,7 @@ public <T> Stream<T> queryForStream(PreparedStatementCreator psc, @Nullable Prep
828
829
}
829
830
ResultSet rs = ps .executeQuery ();
830
831
Connection con = ps .getConnection ();
831
- return new ResultSetSpliterator <>(rs , rowMapper ).stream ().onClose (() -> {
832
+ return new ResultSetSpliterator <>(rs , rowMapper , this . maxRows ).stream ().onClose (() -> {
832
833
JdbcUtils .closeResultSet (rs );
833
834
if (pss instanceof ParameterDisposer parameterDisposer ) {
834
835
parameterDisposer .cleanupParameters ();
@@ -1347,7 +1348,7 @@ protected Map<String, Object> processResultSet(
1347
1348
}
1348
1349
else if (param .getRowCallbackHandler () != null ) {
1349
1350
RowCallbackHandler rch = param .getRowCallbackHandler ();
1350
- (new RowCallbackHandlerResultSetExtractor (rch )).extractData (rs );
1351
+ (new RowCallbackHandlerResultSetExtractor (rch , - 1 )).extractData (rs );
1351
1352
return Collections .singletonMap (param .getName (),
1352
1353
"ResultSet returned from stored procedure was processed" );
1353
1354
}
@@ -1730,13 +1731,17 @@ private static class RowCallbackHandlerResultSetExtractor implements ResultSetEx
1730
1731
1731
1732
private final RowCallbackHandler rch ;
1732
1733
1733
- public RowCallbackHandlerResultSetExtractor (RowCallbackHandler rch ) {
1734
+ private final int maxRows ;
1735
+
1736
+ public RowCallbackHandlerResultSetExtractor (RowCallbackHandler rch , int maxRows ) {
1734
1737
this .rch = rch ;
1738
+ this .maxRows = maxRows ;
1735
1739
}
1736
1740
1737
1741
@ Override
1738
1742
public @ Nullable Object extractData (ResultSet rs ) throws SQLException {
1739
- while (rs .next ()) {
1743
+ int processed = 0 ;
1744
+ while (rs .next () && (this .maxRows == -1 || (processed ++) < this .maxRows )) {
1740
1745
this .rch .processRow (rs );
1741
1746
}
1742
1747
return null ;
@@ -1754,17 +1759,20 @@ private static class ResultSetSpliterator<T> implements Spliterator<T> {
1754
1759
1755
1760
private final RowMapper <T > rowMapper ;
1756
1761
1762
+ private final int maxRows ;
1763
+
1757
1764
private int rowNum = 0 ;
1758
1765
1759
- public ResultSetSpliterator (ResultSet rs , RowMapper <T > rowMapper ) {
1766
+ public ResultSetSpliterator (ResultSet rs , RowMapper <T > rowMapper , int maxRows ) {
1760
1767
this .rs = rs ;
1761
1768
this .rowMapper = rowMapper ;
1769
+ this .maxRows = maxRows ;
1762
1770
}
1763
1771
1764
1772
@ Override
1765
1773
public boolean tryAdvance (Consumer <? super T > action ) {
1766
1774
try {
1767
- if (this .rs .next ()) {
1775
+ if (this .rs .next () && ( this . maxRows == - 1 || this . rowNum < this . maxRows ) ) {
1768
1776
action .accept (this .rowMapper .mapRow (this .rs , this .rowNum ++));
1769
1777
return true ;
1770
1778
}
0 commit comments