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
@@ -493,12 +494,12 @@ public String getSql() {
493
494
494
495
@ Override
495
496
public void query (String sql , RowCallbackHandler rch ) throws DataAccessException {
496
- query (sql , new RowCallbackHandlerResultSetExtractor (rch ));
497
+ query (sql , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
497
498
}
498
499
499
500
@ Override
500
501
public <T > List <T > query (String sql , RowMapper <T > rowMapper ) throws DataAccessException {
501
- return result (query (sql , new RowMapperResultSetExtractor <>(rowMapper )));
502
+ return result (query (sql , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
502
503
}
503
504
504
505
@ Override
@@ -508,7 +509,7 @@ class StreamStatementCallback implements StatementCallback<Stream<T>>, SqlProvid
508
509
public Stream <T > doInStatement (Statement stmt ) throws SQLException {
509
510
ResultSet rs = stmt .executeQuery (sql );
510
511
Connection con = stmt .getConnection ();
511
- return new ResultSetSpliterator <>(rs , rowMapper ).stream ().onClose (() -> {
512
+ return new ResultSetSpliterator <>(rs , rowMapper , JdbcTemplate . this . maxRows ).stream ().onClose (() -> {
512
513
JdbcUtils .closeResultSet (rs );
513
514
JdbcUtils .closeStatement (stmt );
514
515
DataSourceUtils .releaseConnection (con , getDataSource ());
@@ -773,12 +774,12 @@ private String appendSql(@Nullable String sql, String statement) {
773
774
774
775
@ Override
775
776
public void query (PreparedStatementCreator psc , RowCallbackHandler rch ) throws DataAccessException {
776
- query (psc , new RowCallbackHandlerResultSetExtractor (rch ));
777
+ query (psc , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
777
778
}
778
779
779
780
@ Override
780
781
public void query (String sql , @ Nullable PreparedStatementSetter pss , RowCallbackHandler rch ) throws DataAccessException {
781
- query (sql , pss , new RowCallbackHandlerResultSetExtractor (rch ));
782
+ query (sql , pss , new RowCallbackHandlerResultSetExtractor (rch , this . maxRows ));
782
783
}
783
784
784
785
@ Override
@@ -799,28 +800,28 @@ public void query(String sql, RowCallbackHandler rch, @Nullable Object @Nullable
799
800
800
801
@ Override
801
802
public <T > List <T > query (PreparedStatementCreator psc , RowMapper <T > rowMapper ) throws DataAccessException {
802
- return result (query (psc , new RowMapperResultSetExtractor <>(rowMapper )));
803
+ return result (query (psc , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
803
804
}
804
805
805
806
@ Override
806
807
public <T > List <T > query (String sql , @ Nullable PreparedStatementSetter pss , RowMapper <T > rowMapper ) throws DataAccessException {
807
- return result (query (sql , pss , new RowMapperResultSetExtractor <>(rowMapper )));
808
+ return result (query (sql , pss , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
808
809
}
809
810
810
811
@ Override
811
812
public <T > List <T > query (String sql , @ Nullable Object @ Nullable [] args , int [] argTypes , RowMapper <T > rowMapper ) throws DataAccessException {
812
- return result (query (sql , args , argTypes , new RowMapperResultSetExtractor <>(rowMapper )));
813
+ return result (query (sql , args , argTypes , new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
813
814
}
814
815
815
816
@ Deprecated (since = "5.3" )
816
817
@ Override
817
818
public <T > List <T > query (String sql , @ Nullable Object @ Nullable [] args , RowMapper <T > rowMapper ) throws DataAccessException {
818
- return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper )));
819
+ return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
819
820
}
820
821
821
822
@ Override
822
823
public <T > List <T > query (String sql , RowMapper <T > rowMapper , @ Nullable Object @ Nullable ... args ) throws DataAccessException {
823
- return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper )));
824
+ return result (query (sql , newArgPreparedStatementSetter (args ), new RowMapperResultSetExtractor <>(rowMapper , 0 , this . maxRows )));
824
825
}
825
826
826
827
/**
@@ -845,7 +846,7 @@ public <T> Stream<T> queryForStream(PreparedStatementCreator psc, @Nullable Prep
845
846
}
846
847
ResultSet rs = ps .executeQuery ();
847
848
Connection con = ps .getConnection ();
848
- return new ResultSetSpliterator <>(rs , rowMapper ).stream ().onClose (() -> {
849
+ return new ResultSetSpliterator <>(rs , rowMapper , this . maxRows ).stream ().onClose (() -> {
849
850
JdbcUtils .closeResultSet (rs );
850
851
if (pss instanceof ParameterDisposer parameterDisposer ) {
851
852
parameterDisposer .cleanupParameters ();
@@ -1364,7 +1365,7 @@ protected Map<String, Object> processResultSet(
1364
1365
}
1365
1366
else if (param .getRowCallbackHandler () != null ) {
1366
1367
RowCallbackHandler rch = param .getRowCallbackHandler ();
1367
- (new RowCallbackHandlerResultSetExtractor (rch )).extractData (rs );
1368
+ (new RowCallbackHandlerResultSetExtractor (rch , - 1 )).extractData (rs );
1368
1369
return Collections .singletonMap (param .getName (),
1369
1370
"ResultSet returned from stored procedure was processed" );
1370
1371
}
@@ -1747,13 +1748,17 @@ private static class RowCallbackHandlerResultSetExtractor implements ResultSetEx
1747
1748
1748
1749
private final RowCallbackHandler rch ;
1749
1750
1750
- public RowCallbackHandlerResultSetExtractor (RowCallbackHandler rch ) {
1751
+ private final int maxRows ;
1752
+
1753
+ public RowCallbackHandlerResultSetExtractor (RowCallbackHandler rch , int maxRows ) {
1751
1754
this .rch = rch ;
1755
+ this .maxRows = maxRows ;
1752
1756
}
1753
1757
1754
1758
@ Override
1755
1759
public @ Nullable Object extractData (ResultSet rs ) throws SQLException {
1756
- while (rs .next ()) {
1760
+ int processed = 0 ;
1761
+ while (rs .next () && (this .maxRows == -1 || (processed ++) < this .maxRows )) {
1757
1762
this .rch .processRow (rs );
1758
1763
}
1759
1764
return null ;
@@ -1771,17 +1776,20 @@ private static class ResultSetSpliterator<T> implements Spliterator<T> {
1771
1776
1772
1777
private final RowMapper <T > rowMapper ;
1773
1778
1779
+ private final int maxRows ;
1780
+
1774
1781
private int rowNum = 0 ;
1775
1782
1776
- public ResultSetSpliterator (ResultSet rs , RowMapper <T > rowMapper ) {
1783
+ public ResultSetSpliterator (ResultSet rs , RowMapper <T > rowMapper , int maxRows ) {
1777
1784
this .rs = rs ;
1778
1785
this .rowMapper = rowMapper ;
1786
+ this .maxRows = maxRows ;
1779
1787
}
1780
1788
1781
1789
@ Override
1782
1790
public boolean tryAdvance (Consumer <? super T > action ) {
1783
1791
try {
1784
- if (this .rs .next ()) {
1792
+ if (this .rs .next () && ( this . maxRows == - 1 || this . rowNum < this . maxRows ) ) {
1785
1793
action .accept (this .rowMapper .mapRow (this .rs , this .rowNum ++));
1786
1794
return true ;
1787
1795
}
0 commit comments