#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.
This commit is contained in:
Mark Paluch
2019-07-12 15:38:59 +02:00
parent 1d42866231
commit 776044de2b
3 changed files with 96 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ import reactor.core.publisher.Mono;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
@@ -229,6 +230,15 @@ public interface DatabaseClient {
*/
<R> TypedExecuteSpec<R> as(Class<R> resultType);
/**
* Configure a result mapping {@link java.util.function.Function function}.
*
* @param mappingFunction must not be {@literal null}.
* @param <R> result type.
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
*/
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
/**
* Configure a result mapping {@link java.util.function.BiFunction function}.
*
@@ -265,6 +275,15 @@ public interface DatabaseClient {
*/
<R> TypedExecuteSpec<R> as(Class<R> resultType);
/**
* Configure a result mapping {@link java.util.function.Function function}.
*
* @param mappingFunction must not be {@literal null}.
* @param <R> result type.
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
*/
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
/**
* Configure a result mapping {@link java.util.function.BiFunction function}.
*
@@ -393,6 +412,15 @@ public interface DatabaseClient {
*/
<R> TypedSelectSpec<R> as(Class<R> resultType);
/**
* Configure a result mapping {@link java.util.function.Function function}.
*
* @param mappingFunction must not be {@literal null}.
* @param <R> result type.
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
*/
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
/**
* Configure a result mapping {@link java.util.function.BiFunction function}.
*
@@ -422,6 +450,15 @@ public interface DatabaseClient {
*/
<R> RowsFetchSpec<R> as(Class<R> resultType);
/**
* Configure a result mapping {@link java.util.function.Function function}.
*
* @param mappingFunction must not be {@literal null}.
* @param <R> result type.
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
*/
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
/**
* Configure a result mapping {@link java.util.function.BiFunction function}.
*
@@ -556,6 +593,15 @@ public interface DatabaseClient {
*/
interface InsertSpec<T> {
/**
* Configure a result mapping {@link java.util.function.Function function}.
*
* @param mappingFunction must not be {@literal null}.
* @param <R> result type.
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
*/
<R> RowsFetchSpec<R> map(Function<Row, R> mappingFunction);
/**
* Configure a result mapping {@link java.util.function.BiFunction function}.
*

View File

@@ -486,6 +486,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
}
@Override
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange(this.sqlSupplier, (row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
@@ -571,6 +579,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
}
@Override
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange(this.sqlSupplier, (row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
@@ -727,6 +743,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
resultType, dataAccessStrategy.getRowMapper(resultType));
}
@Override
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
@@ -816,6 +840,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
return exchange(dataAccessStrategy.getRowMapper(resultType));
}
@Override
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
@@ -935,6 +967,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
return new DefaultGenericInsertSpec<>(this.table, byName, this.mappingFunction);
}
@Override
public <R> FetchSpec<R> map(Function<Row, R> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction) {
@@ -1017,6 +1057,14 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
return new DefaultTypedInsertSpec<>(this.typeToInsert, this.table, objectToInsert, this.mappingFunction);
}
@Override
public <MR> FetchSpec<MR> map(Function<Row, MR> mappingFunction) {
Assert.notNull(mappingFunction, "Mapping function must not be null!");
return exchange((row, rowMetadata) -> mappingFunction.apply(row));
}
@Override
public <MR> FetchSpec<MR> map(BiFunction<Row, RowMetadata, MR> mappingFunction) {

View File

@@ -332,7 +332,7 @@ public abstract class AbstractDatabaseClientIntegrationTests extends R2dbcIntegr
databaseClient.select().from("legoset") //
.project("id", "name", "manual") //
.orderBy(Sort.by("id")) //
.map((r, md) -> r.get("id", Integer.class)) //
.map((r) -> r.get("id", Integer.class)) //
.all() //
.as(StepVerifier::create) //
.expectNext(42055) //
@@ -374,7 +374,7 @@ public abstract class AbstractDatabaseClientIntegrationTests extends R2dbcIntegr
.project("id", "name", "manual") //
.orderBy(Sort.by("id")) //
.matching(where("id").greaterThanOrEquals(42055).and("id").lessThanOrEquals(42055))
.map((r, md) -> r.get("id", Integer.class)) //
.map((r) -> r.get("id", Integer.class)) //
.all() //
.as(StepVerifier::create) //
.expectNext(42055) //