Polishing.

Use FilterFunction instead of nullable fetchSize to avoid unconditional capturing lambdas and improve defaulting.

Add since tag.

See #1652
Original pull request: #1898
This commit is contained in:
Mark Paluch
2024-10-01 10:17:39 +02:00
parent 96a4121058
commit 90b6d8e8a8
5 changed files with 44 additions and 47 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.r2dbc.core;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
import io.r2dbc.spi.Statement;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -320,7 +321,8 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
<T, P extends Publisher<T>> P doSelect(Query query, Class<?> entityClass, SqlIdentifier tableName,
Class<T> returnType, Function<RowsFetchSpec<T>, P> resultHandler, @Nullable Integer fetchSize) {
RowsFetchSpec<T> fetchSpec = doSelect(query, entityClass, tableName, returnType, fetchSize);
RowsFetchSpec<T> fetchSpec = doSelect(query, entityClass, tableName, returnType,
fetchSize != null ? statement -> statement.fetchSize(fetchSize) : Function.identity());
P result = resultHandler.apply(fetchSpec);
@@ -332,7 +334,7 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
}
private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdentifier tableName,
Class<T> returnType, @Nullable Integer fetchSize) {
Class<T> returnType, Function<? super Statement, ? extends Statement> filterFunction) {
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityType);
@@ -360,7 +362,7 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
PreparedOperation<?> operation = statementMapper.getMappedObject(selectSpec);
return getRowsFetchSpec(
databaseClient.sql(operation).filter((statement) -> statement.fetchSize(Optional.ofNullable(fetchSize).orElse(0))),
databaseClient.sql(operation).filter(filterFunction),
entityType,
returnType
);

View File

@@ -118,10 +118,12 @@ public interface ReactiveSelectOperation {
interface SelectWithQuery<T> extends TerminatingSelect<T> {
/**
* Specifies the fetch size for this query
* Specifies the fetch size for this query.
*
* @param fetchSize
* @return
* @return new instance of {@link SelectWithQuery}.
* @since 3.4
* @see io.r2dbc.spi.Statement#fetchSize(int)
*/
SelectWithQuery<T> withFetchSize(int fetchSize);

View File

@@ -54,7 +54,6 @@ class ReactiveSelectOperationSupport implements ReactiveSelectOperation {
private final Class<T> returnType;
private final Query query;
private final @Nullable SqlIdentifier tableName;
private final @Nullable Integer fetchSize;
ReactiveSelectSupport(R2dbcEntityTemplate template, Class<?> domainType, Class<T> returnType, Query query,
@@ -86,9 +85,6 @@ class ReactiveSelectOperationSupport implements ReactiveSelectOperation {
@Override
public SelectWithQuery<T> withFetchSize(int fetchSize) {
Assert.notNull(returnType, "FetchSize must not be null");
return new ReactiveSelectSupport<>(template, domainType, returnType, query, tableName, fetchSize);
}