#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.
This commit is contained in:
committed by
Jens Schauder
parent
6ad31abd79
commit
9e3601a06d
@@ -25,7 +25,8 @@ import org.springframework.lang.Nullable;
|
||||
* Exception thrown when a {@link io.r2dbc.spi.Result} has been accessed in an invalid fashion. Such exceptions always
|
||||
* have a {@link io.r2dbc.spi.R2dbcException} root cause.
|
||||
* <p>
|
||||
* This typically happens when an invalid {@link org.springframework.data.r2dbc.function.SqlResult} column index or name has been specified.
|
||||
* This typically happens when an invalid {@link org.springframework.data.r2dbc.function.FetchSpec} column index or name
|
||||
* has been specified.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @see BadSqlGrammarException
|
||||
|
||||
@@ -166,7 +166,7 @@ public interface DatabaseClient {
|
||||
* @param <R> result type.
|
||||
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
|
||||
*/
|
||||
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
|
||||
/**
|
||||
* Perform the SQL call and retrieve the result.
|
||||
@@ -202,7 +202,7 @@ public interface DatabaseClient {
|
||||
* @param <R> result type.
|
||||
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
|
||||
*/
|
||||
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
|
||||
/**
|
||||
* Perform the SQL call and retrieve the result.
|
||||
@@ -284,7 +284,7 @@ public interface DatabaseClient {
|
||||
* @param <R> result type.
|
||||
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
|
||||
*/
|
||||
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
|
||||
/**
|
||||
* Perform the SQL call and retrieve the result.
|
||||
@@ -304,7 +304,7 @@ public interface DatabaseClient {
|
||||
* @param resultType must not be {@literal null}.
|
||||
* @param <R> result type.
|
||||
*/
|
||||
<R> FetchSpec<R> as(Class<R> resultType);
|
||||
<R> RowsFetchSpec<R> as(Class<R> resultType);
|
||||
|
||||
/**
|
||||
* Configure a result mapping {@link java.util.function.BiFunction function}.
|
||||
@@ -313,7 +313,7 @@ public interface DatabaseClient {
|
||||
* @param <R> result type.
|
||||
* @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}.
|
||||
*/
|
||||
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
|
||||
/**
|
||||
* Perform the SQL call and retrieve the result.
|
||||
@@ -416,11 +416,11 @@ public interface DatabaseClient {
|
||||
/**
|
||||
* Configure a result mapping {@link java.util.function.BiFunction function}.
|
||||
*
|
||||
* @param mappingFunction must not be {@literal null}.
|
||||
* @param mappwingFunction 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> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
|
||||
|
||||
/**
|
||||
* Perform the SQL call and retrieve the result.
|
||||
|
||||
@@ -23,20 +23,6 @@ import io.r2dbc.spi.Row;
|
||||
import io.r2dbc.spi.RowMetadata;
|
||||
import io.r2dbc.spi.Statement;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.r2dbc.UncategorizedR2dbcException;
|
||||
import org.springframework.data.r2dbc.function.connectionfactory.ConnectionProxy;
|
||||
import org.springframework.data.r2dbc.function.convert.ColumnMapRowMapper;
|
||||
import org.springframework.data.r2dbc.function.convert.SettableValue;
|
||||
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
|
||||
import org.springframework.jdbc.core.SqlProvider;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -57,6 +43,21 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.r2dbc.UncategorizedR2dbcException;
|
||||
import org.springframework.data.r2dbc.function.connectionfactory.ConnectionProxy;
|
||||
import org.springframework.data.r2dbc.function.convert.ColumnMapRowMapper;
|
||||
import org.springframework.data.r2dbc.function.convert.SettableValue;
|
||||
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
|
||||
import org.springframework.jdbc.core.SqlProvider;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link DatabaseClient}.
|
||||
*
|
||||
@@ -313,7 +314,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
return sql;
|
||||
}
|
||||
|
||||
<T> SqlResult<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
|
||||
<T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
|
||||
|
||||
Function<Connection, Statement<?>> executeFunction = it -> {
|
||||
|
||||
@@ -603,7 +604,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
return createInstance(table, projectedFields, sort, page);
|
||||
}
|
||||
|
||||
<R> SqlResult<R> execute(String sql, BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
<R> FetchSpec<R> execute(String sql, BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
|
||||
Function<Connection, Statement<?>> selectFunction = it -> {
|
||||
|
||||
@@ -674,7 +675,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
return exchange(ColumnMapRowMapper.INSTANCE);
|
||||
}
|
||||
|
||||
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
|
||||
Set<String> columns;
|
||||
|
||||
@@ -758,11 +759,11 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlResult<T> fetch() {
|
||||
public FetchSpec<T> fetch() {
|
||||
return exchange(mappingFunction);
|
||||
}
|
||||
|
||||
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
|
||||
List<String> columns;
|
||||
|
||||
@@ -851,7 +852,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
return fetch().rowsUpdated().then();
|
||||
}
|
||||
|
||||
private <R> SqlResult<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
|
||||
if (byName.isEmpty()) {
|
||||
throw new IllegalStateException("Insert fields is empty!");
|
||||
@@ -970,7 +971,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
};
|
||||
}
|
||||
|
||||
private <MR> SqlResult<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata, MR> mappingFunction) {
|
||||
private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata, MR> mappingFunction) {
|
||||
|
||||
List<SettableValue> insertValues = dataAccessStrategy.getValuesToInsert(toInsert);
|
||||
Set<String> columns = new LinkedHashSet<>();
|
||||
|
||||
@@ -15,42 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.r2dbc.function;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Contract for fetching results.
|
||||
*
|
||||
* @param <T> row result type.
|
||||
* @author Mark Paluch
|
||||
* @see RowsFetchSpec
|
||||
* @see UpdatedRowsFetchSpec
|
||||
*/
|
||||
public interface FetchSpec<T> {
|
||||
|
||||
/**
|
||||
* Get exactly zero or one result.
|
||||
*
|
||||
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
|
||||
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
|
||||
*/
|
||||
Mono<T> one();
|
||||
|
||||
/**
|
||||
* Get the first or no result.
|
||||
*
|
||||
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
|
||||
*/
|
||||
Mono<T> first();
|
||||
|
||||
/**
|
||||
* Get all matching elements.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
Flux<T> all();
|
||||
|
||||
/**
|
||||
* Get the number of updated rows.
|
||||
*
|
||||
* @return {@link Mono} emitting the number of updated rows. Never {@literal null}.
|
||||
*/
|
||||
Mono<Integer> rowsUpdated();
|
||||
}
|
||||
public interface FetchSpec<T> extends RowsFetchSpec<T>, UpdatedRowsFetchSpec {}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.function;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Contract for fetching tabular results.
|
||||
*
|
||||
* @param <T> row result type.
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface RowsFetchSpec<T> {
|
||||
|
||||
/**
|
||||
* Get exactly zero or one result.
|
||||
*
|
||||
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
|
||||
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
|
||||
*/
|
||||
Mono<T> one();
|
||||
|
||||
/**
|
||||
* Get the first or no result.
|
||||
*
|
||||
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
|
||||
*/
|
||||
Mono<T> first();
|
||||
|
||||
/**
|
||||
* Get all matching elements.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
Flux<T> all();
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import java.util.function.BiFunction;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface SqlResult<T> extends FetchSpec<T> {
|
||||
interface SqlResult<T> extends FetchSpec<T> {
|
||||
|
||||
/**
|
||||
* Apply a {@link BiFunction mapping function} to the result that emits {@link Row}s.
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.function;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Contract for fetching the number of affected rows.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface UpdatedRowsFetchSpec {
|
||||
|
||||
/**
|
||||
* Get the number of updated rows.
|
||||
*
|
||||
* @return {@link Mono} emitting the number of updated rows. Never {@literal null}.
|
||||
*/
|
||||
Mono<Integer> rowsUpdated();
|
||||
}
|
||||
Reference in New Issue
Block a user