Skip to content

Commit 776044d

Browse files
committed
#140 - Accept simple mapping function for Row in DatabaseClient.
We now accept a Function<Row, T> in DatabaseClient's map(…) operator to simplify mapping by not requiring RowMetadata.
1 parent 1d42866 commit 776044d

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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

+46
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.function.BiFunction;
2525
import java.util.function.Consumer;
26+
import java.util.function.Function;
2627
import java.util.function.Supplier;
2728

2829
import org.reactivestreams.Publisher;
@@ -229,6 +230,15 @@ interface GenericExecuteSpec extends BindSpec<GenericExecuteSpec> {
229230
*/
230231
<R> TypedExecuteSpec<R> as(Class<R> resultType);
231232

233+
/**
234+
* Configure a result mapping {@link java.util.function.Function function}.
235+
*
236+
* @param mappingFunction must not be {@literal null}.
237+
* @param <R> result type.
238+
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
239+
*/
240+
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
241+
232242
/**
233243
* Configure a result mapping {@link java.util.function.BiFunction function}.
234244
*
@@ -265,6 +275,15 @@ interface TypedExecuteSpec<T> extends BindSpec<TypedExecuteSpec<T>> {
265275
*/
266276
<R> TypedExecuteSpec<R> as(Class<R> resultType);
267277

278+
/**
279+
* Configure a result mapping {@link java.util.function.Function function}.
280+
*
281+
* @param mappingFunction must not be {@literal null}.
282+
* @param <R> result type.
283+
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
284+
*/
285+
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
286+
268287
/**
269288
* Configure a result mapping {@link java.util.function.BiFunction function}.
270289
*
@@ -393,6 +412,15 @@ interface GenericSelectSpec extends SelectSpec<GenericSelectSpec> {
393412
*/
394413
<R> TypedSelectSpec<R> as(Class<R> resultType);
395414

415+
/**
416+
* Configure a result mapping {@link java.util.function.Function function}.
417+
*
418+
* @param mappingFunction must not be {@literal null}.
419+
* @param <R> result type.
420+
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
421+
*/
422+
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
423+
396424
/**
397425
* Configure a result mapping {@link java.util.function.BiFunction function}.
398426
*
@@ -422,6 +450,15 @@ interface TypedSelectSpec<T> extends SelectSpec<TypedSelectSpec<T>> {
422450
*/
423451
<R> RowsFetchSpec<R> as(Class<R> resultType);
424452

453+
/**
454+
* Configure a result mapping {@link java.util.function.Function function}.
455+
*
456+
* @param mappingFunction must not be {@literal null}.
457+
* @param <R> result type.
458+
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
459+
*/
460+
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
461+
425462
/**
426463
* Configure a result mapping {@link java.util.function.BiFunction function}.
427464
*
@@ -556,6 +593,15 @@ interface TypedInsertSpec<T> {
556593
*/
557594
interface InsertSpec<T> {
558595

596+
/**
597+
* Configure a result mapping {@link java.util.function.Function function}.
598+
*
599+
* @param mappingFunction must not be {@literal null}.
600+
* @param <R> result type.
601+
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
602+
*/
603+
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
604+
559605
/**
560606
* Configure a result mapping {@link java.util.function.BiFunction function}.
561607
*

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

+48
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
486486
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
487487
}
488488

489+
@Override
490+
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
491+
492+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
493+
494+
return exchange(this.sqlSupplier, (row, rowMetadata) -> mappingFunction.apply(row));
495+
}
496+
489497
@Override
490498
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
491499

@@ -571,6 +579,14 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
571579
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
572580
}
573581

582+
@Override
583+
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
584+
585+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
586+
587+
return exchange(this.sqlSupplier, (row, rowMetadata) -> mappingFunction.apply(row));
588+
}
589+
574590
@Override
575591
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
576592

@@ -727,6 +743,14 @@ public <R> TypedSelectSpec<R> as(Class<R> resultType) {
727743
resultType, dataAccessStrategy.getRowMapper(resultType));
728744
}
729745

746+
@Override
747+
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
748+
749+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
750+
751+
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
752+
}
753+
730754
@Override
731755
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
732756

@@ -816,6 +840,14 @@ public <R> FetchSpec<R> as(Class<R> resultType) {
816840
return exchange(dataAccessStrategy.getRowMapper(resultType));
817841
}
818842

843+
@Override
844+
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
845+
846+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
847+
848+
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
849+
}
850+
819851
@Override
820852
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
821853

@@ -935,6 +967,14 @@ public GenericInsertSpec<T> nullValue(String field) {
935967
return new DefaultGenericInsertSpec<>(this.table, byName, this.mappingFunction);
936968
}
937969

970+
@Override
971+
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
972+
973+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
974+
975+
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
976+
}
977+
938978
@Override
939979
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
940980

@@ -1017,6 +1057,14 @@ public InsertSpec using(Publisher<T> objectToInsert) {
10171057
return new DefaultTypedInsertSpec<>(this.typeToInsert, this.table, objectToInsert, this.mappingFunction);
10181058
}
10191059

1060+
@Override
1061+
public <MR> FetchSpec<MR> map(Function<Row, MR> mappingFunction) {
1062+
1063+
Assert.notNull(mappingFunction, "Mapping function must not be null!");
1064+
1065+
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
1066+
}
1067+
10201068
@Override
10211069
public <MR> FetchSpec<MR> map(BiFunction<Row, RowMetadata, MR> mappingFunction) {
10221070

src/test/java/org/springframework/data/r2dbc/core/AbstractDatabaseClientIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public void selectExtracting() {
332332
databaseClient.select().from("legoset") //
333333
.project("id", "name", "manual") //
334334
.orderBy(Sort.by("id")) //
335-
.map((r, md) -> r.get("id", Integer.class)) //
335+
.map((r) -> r.get("id", Integer.class)) //
336336
.all() //
337337
.as(StepVerifier::create) //
338338
.expectNext(42055) //
@@ -374,7 +374,7 @@ public void selectWithCriteria() {
374374
.project("id", "name", "manual") //
375375
.orderBy(Sort.by("id")) //
376376
.matching(where("id").greaterThanOrEquals(42055).and("id").lessThanOrEquals(42055))
377-
.map((r, md) -> r.get("id", Integer.class)) //
377+
.map((r) -> r.get("id", Integer.class)) //
378378
.all() //
379379
.as(StepVerifier::create) //
380380
.expectNext(42055) //

0 commit comments

Comments
 (0)