Skip to content

Commit 9e3601a

Browse files
mp911deschauder
authored andcommitted
#8 - Reduce API surface for mapped tabular results.
Move retrieval methods from FetchSpec into RowsFetchSpec and UpdatedRowsFetchSpec. map(…) now returns RowsFetchSpec to not expose updated rows count as mapped results are consumed as objects. Original pull request: #33.
1 parent 6ad31ab commit 9e3601a

File tree

7 files changed

+119
-64
lines changed

7 files changed

+119
-64
lines changed

src/main/java/org/springframework/data/r2dbc/InvalidResultAccessException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* Exception thrown when a {@link io.r2dbc.spi.Result} has been accessed in an invalid fashion. Such exceptions always
2626
* have a {@link io.r2dbc.spi.R2dbcException} root cause.
2727
* <p>
28-
* This typically happens when an invalid {@link org.springframework.data.r2dbc.function.SqlResult} column index or name has been specified.
28+
* This typically happens when an invalid {@link org.springframework.data.r2dbc.function.FetchSpec} column index or name
29+
* has been specified.
2930
*
3031
* @author Mark Paluch
3132
* @see BadSqlGrammarException

src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ interface GenericExecuteSpec extends BindSpec<GenericExecuteSpec> {
166166
* @param <R> result type.
167167
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
168168
*/
169-
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
169+
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
170170

171171
/**
172172
* Perform the SQL call and retrieve the result.
@@ -202,7 +202,7 @@ interface TypedExecuteSpec<T> extends BindSpec<TypedExecuteSpec<T>> {
202202
* @param <R> result type.
203203
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
204204
*/
205-
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
205+
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
206206

207207
/**
208208
* Perform the SQL call and retrieve the result.
@@ -284,7 +284,7 @@ interface GenericSelectSpec extends SelectSpec<GenericSelectSpec> {
284284
* @param <R> result type.
285285
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
286286
*/
287-
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
287+
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
288288

289289
/**
290290
* Perform the SQL call and retrieve the result.
@@ -304,7 +304,7 @@ interface TypedSelectSpec<T> extends SelectSpec<TypedSelectSpec<T>> {
304304
* @param resultType must not be {@literal null}.
305305
* @param <R> result type.
306306
*/
307-
<R> FetchSpec<R> as(Class<R> resultType);
307+
<R> RowsFetchSpec<R> as(Class<R> resultType);
308308

309309
/**
310310
* Configure a result mapping {@link java.util.function.BiFunction function}.
@@ -313,7 +313,7 @@ interface TypedSelectSpec<T> extends SelectSpec<TypedSelectSpec<T>> {
313313
* @param <R> result type.
314314
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
315315
*/
316-
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
316+
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
317317

318318
/**
319319
* Perform the SQL call and retrieve the result.
@@ -416,11 +416,11 @@ interface InsertSpec<T> {
416416
/**
417417
* Configure a result mapping {@link java.util.function.BiFunction function}.
418418
*
419-
* @param mappingFunction must not be {@literal null}.
419+
* @param mappwingFunction must not be {@literal null}.
420420
* @param <R> result type.
421421
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
422422
*/
423-
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
423+
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
424424

425425
/**
426426
* Perform the SQL call and retrieve the result.

src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@
2323
import io.r2dbc.spi.RowMetadata;
2424
import io.r2dbc.spi.Statement;
2525
import lombok.RequiredArgsConstructor;
26-
import org.apache.commons.logging.Log;
27-
import org.apache.commons.logging.LogFactory;
28-
import org.reactivestreams.Publisher;
29-
import org.springframework.dao.DataAccessException;
30-
import org.springframework.data.domain.Pageable;
31-
import org.springframework.data.domain.Sort;
32-
import org.springframework.data.r2dbc.UncategorizedR2dbcException;
33-
import org.springframework.data.r2dbc.function.connectionfactory.ConnectionProxy;
34-
import org.springframework.data.r2dbc.function.convert.ColumnMapRowMapper;
35-
import org.springframework.data.r2dbc.function.convert.SettableValue;
36-
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
37-
import org.springframework.jdbc.core.SqlProvider;
38-
import org.springframework.lang.Nullable;
39-
import org.springframework.util.Assert;
4026
import reactor.core.publisher.Flux;
4127
import reactor.core.publisher.Mono;
4228

@@ -57,6 +43,21 @@
5743
import java.util.function.Supplier;
5844
import java.util.stream.Collectors;
5945

46+
import org.apache.commons.logging.Log;
47+
import org.apache.commons.logging.LogFactory;
48+
import org.reactivestreams.Publisher;
49+
import org.springframework.dao.DataAccessException;
50+
import org.springframework.data.domain.Pageable;
51+
import org.springframework.data.domain.Sort;
52+
import org.springframework.data.r2dbc.UncategorizedR2dbcException;
53+
import org.springframework.data.r2dbc.function.connectionfactory.ConnectionProxy;
54+
import org.springframework.data.r2dbc.function.convert.ColumnMapRowMapper;
55+
import org.springframework.data.r2dbc.function.convert.SettableValue;
56+
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
57+
import org.springframework.jdbc.core.SqlProvider;
58+
import org.springframework.lang.Nullable;
59+
import org.springframework.util.Assert;
60+
6061
/**
6162
* Default implementation of {@link DatabaseClient}.
6263
*
@@ -313,7 +314,7 @@ protected String getSql() {
313314
return sql;
314315
}
315316

316-
<T> SqlResult<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
317+
<T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
317318

318319
Function<Connection, Statement<?>> executeFunction = it -> {
319320

@@ -603,7 +604,7 @@ public DefaultSelectSpecSupport page(Pageable page) {
603604
return createInstance(table, projectedFields, sort, page);
604605
}
605606

606-
<R> SqlResult<R> execute(String sql, BiFunction<Row, RowMetadata, R> mappingFunction) {
607+
<R> FetchSpec<R> execute(String sql, BiFunction<Row, RowMetadata, R> mappingFunction) {
607608

608609
Function<Connection, Statement<?>> selectFunction = it -> {
609610

@@ -674,7 +675,7 @@ public FetchSpec<Map<String, Object>> fetch() {
674675
return exchange(ColumnMapRowMapper.INSTANCE);
675676
}
676677

677-
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
678+
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
678679

679680
Set<String> columns;
680681

@@ -758,11 +759,11 @@ public DefaultTypedSelectSpec<T> page(Pageable page) {
758759
}
759760

760761
@Override
761-
public SqlResult<T> fetch() {
762+
public FetchSpec<T> fetch() {
762763
return exchange(mappingFunction);
763764
}
764765

765-
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
766+
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
766767

767768
List<String> columns;
768769

@@ -851,7 +852,7 @@ public Mono<Void> then() {
851852
return fetch().rowsUpdated().then();
852853
}
853854

854-
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
855+
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
855856

856857
if (byName.isEmpty()) {
857858
throw new IllegalStateException("Insert fields is empty!");
@@ -970,7 +971,7 @@ public Mono<Integer> rowsUpdated() {
970971
};
971972
}
972973

973-
private <MR> SqlResult<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata, MR> mappingFunction) {
974+
private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata, MR> mappingFunction) {
974975

975976
List<SettableValue> insertValues = dataAccessStrategy.getValuesToInsert(toInsert);
976977
Set<String> columns = new LinkedHashSet<>();

src/main/java/org/springframework/data/r2dbc/function/FetchSpec.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,12 @@
1515
*/
1616
package org.springframework.data.r2dbc.function;
1717

18-
import reactor.core.publisher.Flux;
19-
import reactor.core.publisher.Mono;
20-
2118
/**
2219
* Contract for fetching results.
2320
*
21+
* @param <T> row result type.
2422
* @author Mark Paluch
23+
* @see RowsFetchSpec
24+
* @see UpdatedRowsFetchSpec
2525
*/
26-
public interface FetchSpec<T> {
27-
28-
/**
29-
* Get exactly zero or one result.
30-
*
31-
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
32-
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
33-
*/
34-
Mono<T> one();
35-
36-
/**
37-
* Get the first or no result.
38-
*
39-
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
40-
*/
41-
Mono<T> first();
42-
43-
/**
44-
* Get all matching elements.
45-
*
46-
* @return never {@literal null}.
47-
*/
48-
Flux<T> all();
49-
50-
/**
51-
* Get the number of updated rows.
52-
*
53-
* @return {@link Mono} emitting the number of updated rows. Never {@literal null}.
54-
*/
55-
Mono<Integer> rowsUpdated();
56-
}
26+
public interface FetchSpec<T> extends RowsFetchSpec<T>, UpdatedRowsFetchSpec {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.function;
17+
18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
20+
21+
/**
22+
* Contract for fetching tabular results.
23+
*
24+
* @param <T> row result type.
25+
* @author Mark Paluch
26+
*/
27+
public interface RowsFetchSpec<T> {
28+
29+
/**
30+
* Get exactly zero or one result.
31+
*
32+
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
33+
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
34+
*/
35+
Mono<T> one();
36+
37+
/**
38+
* Get the first or no result.
39+
*
40+
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
41+
*/
42+
Mono<T> first();
43+
44+
/**
45+
* Get all matching elements.
46+
*
47+
* @return never {@literal null}.
48+
*/
49+
Flux<T> all();
50+
}

src/main/java/org/springframework/data/r2dbc/function/SqlResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Mark Paluch
2727
*/
28-
public interface SqlResult<T> extends FetchSpec<T> {
28+
interface SqlResult<T> extends FetchSpec<T> {
2929

3030
/**
3131
* Apply a {@link BiFunction mapping function} to the result that emits {@link Row}s.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.function;
17+
18+
import reactor.core.publisher.Mono;
19+
20+
/**
21+
* Contract for fetching the number of affected rows.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
public interface UpdatedRowsFetchSpec {
26+
27+
/**
28+
* Get the number of updated rows.
29+
*
30+
* @return {@link Mono} emitting the number of updated rows. Never {@literal null}.
31+
*/
32+
Mono<Integer> rowsUpdated();
33+
}

0 commit comments

Comments
 (0)