Skip to content

Commit af0b39a

Browse files
committed
Merge pull request #31282 from sephiroth-j
* pr/31282: Polish "Use `singleOrEmpty()` instead of `buffer()`" Use `singleOrEmpty()` instead of `buffer()` Closes gh-31282
2 parents 2e2a62a + f929121 commit af0b39a

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,11 @@ class DefaultFetchSpec<T> implements FetchSpec<T> {
5959

6060
@Override
6161
public Mono<T> one() {
62-
return all().buffer(2)
63-
.flatMap(list -> {
64-
if (list.isEmpty()) {
65-
return Mono.empty();
66-
}
67-
if (list.size() > 1) {
68-
return Mono.error(new IncorrectResultSizeDataAccessException(
69-
String.format("Query [%s] returned non unique result.", this.resultFunction.getSql()),
70-
1));
71-
}
72-
return Mono.just(list.get(0));
73-
}).next();
62+
return all().singleOrEmpty()
63+
.onErrorMap(IndexOutOfBoundsException.class, ex -> {
64+
String message = String.format("Query [%s] returned non unique result.", this.resultFunction.getSql());
65+
return new IncorrectResultSizeDataAccessException(message, 1);
66+
});
7467
}
7568

7669
@Override

0 commit comments

Comments
 (0)