diff --git a/src/main/java/org/springframework/data/r2dbc/InvalidResultAccessException.java b/src/main/java/org/springframework/data/r2dbc/InvalidResultAccessException.java index 82d5fe1..6effd60 100644 --- a/src/main/java/org/springframework/data/r2dbc/InvalidResultAccessException.java +++ b/src/main/java/org/springframework/data/r2dbc/InvalidResultAccessException.java @@ -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. *

- * 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 diff --git a/src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java b/src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java index 2c108e7..094e650 100644 --- a/src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java +++ b/src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java @@ -166,7 +166,7 @@ public interface DatabaseClient { * @param result type. * @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}. */ - FetchSpec map(BiFunction mappingFunction); + RowsFetchSpec map(BiFunction mappingFunction); /** * Perform the SQL call and retrieve the result. @@ -202,7 +202,7 @@ public interface DatabaseClient { * @param result type. * @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}. */ - FetchSpec map(BiFunction mappingFunction); + RowsFetchSpec map(BiFunction mappingFunction); /** * Perform the SQL call and retrieve the result. @@ -284,7 +284,7 @@ public interface DatabaseClient { * @param result type. * @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}. */ - FetchSpec map(BiFunction mappingFunction); + RowsFetchSpec map(BiFunction mappingFunction); /** * Perform the SQL call and retrieve the result. @@ -304,7 +304,7 @@ public interface DatabaseClient { * @param resultType must not be {@literal null}. * @param result type. */ - FetchSpec as(Class resultType); + RowsFetchSpec as(Class resultType); /** * Configure a result mapping {@link java.util.function.BiFunction function}. @@ -313,7 +313,7 @@ public interface DatabaseClient { * @param result type. * @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}. */ - FetchSpec map(BiFunction mappingFunction); + RowsFetchSpec map(BiFunction 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 result type. * @return a {@link FetchSpec} for configuration what to fetch. Guaranteed to be not {@literal null}. */ - FetchSpec map(BiFunction mappingFunction); + RowsFetchSpec map(BiFunction mappingFunction); /** * Perform the SQL call and retrieve the result. diff --git a/src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java b/src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java index c8b8dac..d96f57f 100644 --- a/src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java +++ b/src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java @@ -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; } - SqlResult exchange(String sql, BiFunction mappingFunction) { + FetchSpec exchange(String sql, BiFunction mappingFunction) { Function> executeFunction = it -> { @@ -603,7 +604,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { return createInstance(table, projectedFields, sort, page); } - SqlResult execute(String sql, BiFunction mappingFunction) { + FetchSpec execute(String sql, BiFunction mappingFunction) { Function> selectFunction = it -> { @@ -674,7 +675,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { return exchange(ColumnMapRowMapper.INSTANCE); } - private SqlResult exchange(BiFunction mappingFunction) { + private FetchSpec exchange(BiFunction mappingFunction) { Set columns; @@ -758,11 +759,11 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { } @Override - public SqlResult fetch() { + public FetchSpec fetch() { return exchange(mappingFunction); } - private SqlResult exchange(BiFunction mappingFunction) { + private FetchSpec exchange(BiFunction mappingFunction) { List columns; @@ -851,7 +852,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { return fetch().rowsUpdated().then(); } - private SqlResult exchange(BiFunction mappingFunction) { + private FetchSpec exchange(BiFunction mappingFunction) { if (byName.isEmpty()) { throw new IllegalStateException("Insert fields is empty!"); @@ -970,7 +971,7 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { }; } - private SqlResult exchange(Object toInsert, BiFunction mappingFunction) { + private FetchSpec exchange(Object toInsert, BiFunction mappingFunction) { List insertValues = dataAccessStrategy.getValuesToInsert(toInsert); Set columns = new LinkedHashSet<>(); diff --git a/src/main/java/org/springframework/data/r2dbc/function/FetchSpec.java b/src/main/java/org/springframework/data/r2dbc/function/FetchSpec.java index f7e8f2d..da748b5 100644 --- a/src/main/java/org/springframework/data/r2dbc/function/FetchSpec.java +++ b/src/main/java/org/springframework/data/r2dbc/function/FetchSpec.java @@ -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 row result type. * @author Mark Paluch + * @see RowsFetchSpec + * @see UpdatedRowsFetchSpec */ -public interface FetchSpec { - - /** - * 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 one(); - - /** - * Get the first or no result. - * - * @return {@link Mono#empty()} if no match found. Never {@literal null}. - */ - Mono first(); - - /** - * Get all matching elements. - * - * @return never {@literal null}. - */ - Flux all(); - - /** - * Get the number of updated rows. - * - * @return {@link Mono} emitting the number of updated rows. Never {@literal null}. - */ - Mono rowsUpdated(); -} +public interface FetchSpec extends RowsFetchSpec, UpdatedRowsFetchSpec {} diff --git a/src/main/java/org/springframework/data/r2dbc/function/RowsFetchSpec.java b/src/main/java/org/springframework/data/r2dbc/function/RowsFetchSpec.java new file mode 100644 index 0000000..5399bff --- /dev/null +++ b/src/main/java/org/springframework/data/r2dbc/function/RowsFetchSpec.java @@ -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 row result type. + * @author Mark Paluch + */ +public interface RowsFetchSpec { + + /** + * 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 one(); + + /** + * Get the first or no result. + * + * @return {@link Mono#empty()} if no match found. Never {@literal null}. + */ + Mono first(); + + /** + * Get all matching elements. + * + * @return never {@literal null}. + */ + Flux all(); +} diff --git a/src/main/java/org/springframework/data/r2dbc/function/SqlResult.java b/src/main/java/org/springframework/data/r2dbc/function/SqlResult.java index 8356fa8..ea41820 100644 --- a/src/main/java/org/springframework/data/r2dbc/function/SqlResult.java +++ b/src/main/java/org/springframework/data/r2dbc/function/SqlResult.java @@ -25,7 +25,7 @@ import java.util.function.BiFunction; * * @author Mark Paluch */ -public interface SqlResult extends FetchSpec { +interface SqlResult extends FetchSpec { /** * Apply a {@link BiFunction mapping function} to the result that emits {@link Row}s. diff --git a/src/main/java/org/springframework/data/r2dbc/function/UpdatedRowsFetchSpec.java b/src/main/java/org/springframework/data/r2dbc/function/UpdatedRowsFetchSpec.java new file mode 100644 index 0000000..e207735 --- /dev/null +++ b/src/main/java/org/springframework/data/r2dbc/function/UpdatedRowsFetchSpec.java @@ -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 rowsUpdated(); +}