Skip to content

Commit fbbad6b

Browse files
committed
queryLazily() overload without fetchSize
1 parent edae892 commit fbbad6b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

mug-guava/src/.DS_Store

0 Bytes
Binary file not shown.

mug-guava/src/main/java/com/google/mu/safesql/SafeSql.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,34 @@ public <T> List<T> query(
728728
}
729729
}
730730

731+
/**
732+
* Executes the encapsulated SQL as a query against {@code connection},
733+
* and then fetches the results lazily in a stream.
734+
*
735+
* <p>The returned {@code Stream} includes results transformed by {@code rowMapper}.
736+
* The caller must close it using try-with-resources idiom, which will close the associated
737+
* {@link Statement} and {@link ResultSet}.
738+
*
739+
* <p>For example: <pre>{@code
740+
* SafeSql sql = SafeSql.of("SELECT name FROM Users WHERE name LIKE '%{name}%'", name);
741+
* try (Stream<String> names = sql.queryLazily(connection, row -> row.getLong("id"))) {
742+
* return names.findFirst();
743+
* }
744+
* }</pre>
745+
*
746+
* <p>Internally it delegates to {@link PreparedStatement#executeQuery} or {@link
747+
* Statement#executeQuery} if this sql contains no JDBC binding parameters.
748+
*
749+
* @throws UncheckedSqlException wraps {@link SQLException} if failed
750+
* @since 8.4
751+
*/
752+
@MustBeClosed
753+
@SuppressWarnings("MustBeClosedChecker")
754+
public <T> Stream<T> queryLazily(
755+
Connection connection, SqlFunction<? super ResultSet, ? extends T> rowMapper) {
756+
return queryLazily(connection, 0, rowMapper);
757+
}
758+
731759
/**
732760
* Executes the encapsulated SQL as a query against {@code connection}, sets {@code fetchSize}
733761
* using {@link Statement#setFetchSize}, and then fetches the results lazily in a stream.

mug-guava/src/test/java/com/google/mu/safesql/SafeSqlDbTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private List<?> queryColumn(SafeSql sql, String column) throws Exception {
334334
}
335335

336336
private List<?> queryColumnStream(SafeSql sql, String column) throws Exception {
337-
try (Stream<?> stream = sql.queryLazily(connection(), 1, resultSet -> resultSet.getObject(column))) {
337+
try (Stream<?> stream = sql.queryLazily(connection(), resultSet -> resultSet.getObject(column))) {
338338
return stream.collect(Collectors.toList());
339339
}
340340
}

0 commit comments

Comments
 (0)