Browse Source

Use `singleOrEmpty()` instead of `buffer()`

This commit avoids fetching and buffering results in temporary `List` buffers.

See gh-31282
pull/31316/head
Ronny Perinke 3 years ago committed by Stéphane Nicoll
parent
commit
cbfb99fef2
  1. 17
      spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultFetchSpec.java

17
spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultFetchSpec.java

@ -59,18 +59,11 @@ class DefaultFetchSpec<T> implements FetchSpec<T> { @@ -59,18 +59,11 @@ class DefaultFetchSpec<T> implements FetchSpec<T> {
@Override
public Mono<T> one() {
return all().buffer(2)
.flatMap(list -> {
if (list.isEmpty()) {
return Mono.empty();
}
if (list.size() > 1) {
return Mono.error(new IncorrectResultSizeDataAccessException(
String.format("Query [%s] returned non unique result.", this.resultFunction.getSql()),
1));
}
return Mono.just(list.get(0));
}).next();
return all().singleOrEmpty()
.onErrorMap(IndexOutOfBoundsException.class, ex -> {
String message = String.format("Query [%s] returned non unique result.", resultFunction.getSql());
return new IncorrectResultSizeDataAccessException(message, 1);
});
}
@Override

Loading…
Cancel
Save