#13 - Polishing.

Add missing header & package-info. Update nullability and add tests.

Original pull request: #17.
This commit is contained in:
Christoph Strobl
2018-11-20 09:59:04 +01:00
committed by Mark Paluch
parent 6267047f17
commit 9f7d3863e5
20 changed files with 164 additions and 30 deletions

View File

@@ -25,7 +25,7 @@ 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 Result} column index or name has been specified.
* This typically happens when an invalid {@link org.springframework.data.r2dbc.function.SqlResult} column index or name has been specified.
*
* @author Mark Paluch
* @see BadSqlGrammarException
@@ -42,7 +42,7 @@ public class InvalidResultAccessException extends InvalidDataAccessResourceUsage
* @param sql the offending SQL statement.
* @param ex the root cause.
*/
public InvalidResultAccessException(String task, String sql, R2dbcException ex) {
public InvalidResultAccessException(String task, @Nullable String sql, R2dbcException ex) {
super(task + "; invalid Result access for SQL [" + sql + "]", ex);

View File

@@ -0,0 +1,6 @@
/**
* Connection and ConnectionFactory specifics for R2DBC.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.function.connectionfactory;

View File

@@ -33,6 +33,7 @@ import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.lang.Nullable;
/**
* Maps a {@link io.r2dbc.spi.Row} to an entity of type {@code T}, including entities referenced.
@@ -150,6 +151,7 @@ public class EntityRowMapper<T> implements BiFunction<Row, RowMetadata, T> {
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
@Override
@Nullable
public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parameter) {
String column = prefix + entity.getRequiredPersistentProperty(parameter.getName()).getColumnName();

View File

@@ -0,0 +1,6 @@
/**
* R2DBC-specific conversion and converter implementations.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.function.convert;

View File

@@ -0,0 +1,6 @@
/**
* Core domain types around DatabaseClient.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.function;

View File

@@ -0,0 +1,6 @@
/**
* Support infrastructure for the configuration of R2DBC-specific repositories.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc;

View File

@@ -1,9 +1,23 @@
/*
* 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.repository.config;
import io.r2dbc.spi.ConnectionFactory;
import java.util.Optional;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.function.DatabaseClient;
@@ -14,6 +28,7 @@ import org.springframework.data.r2dbc.support.SqlErrorCodeR2dbcExceptionTranslat
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.util.Assert;
/**
* Base class for Spring Data R2DBC configuration containing bean declarations that must be registered for Spring Data
@@ -39,13 +54,20 @@ public abstract class AbstractR2dbcConfiguration {
* Register a {@link DatabaseClient} using {@link #connectionFactory()} and {@link RelationalMappingContext}.
*
* @return must not be {@literal null}.
* @throws IllegalArgumentException if any of the required args is {@literal null}.
*/
@Bean
public DatabaseClient databaseClient(ReactiveDataAccessStrategy dataAccessStrategy,
R2dbcExceptionTranslator exceptionTranslator) {
return DatabaseClient.builder().connectionFactory(connectionFactory()).dataAccessStrategy(dataAccessStrategy)
.exceptionTranslator(exceptionTranslator).build();
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null!");
Assert.notNull(exceptionTranslator, "ExceptionTranslator must not be null!");
return DatabaseClient.builder() //
.connectionFactory(connectionFactory()) //
.dataAccessStrategy(dataAccessStrategy) //
.exceptionTranslator(exceptionTranslator) //
.build();
}
/**
@@ -53,26 +75,33 @@ public abstract class AbstractR2dbcConfiguration {
*
* @param namingStrategy optional {@link NamingStrategy}. Use {@link NamingStrategy#INSTANCE} as fallback.
* @return must not be {@literal null}.
* @throws IllegalArgumentException if any of the required args is {@literal null}.
*/
@Bean
public RelationalMappingContext r2dbcMappingContext(Optional<NamingStrategy> namingStrategy) {
Assert.notNull(namingStrategy, "NamingStrategy must not be null!");
return new RelationalMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE));
}
/**
* Creates a {@link ReactiveDataAccessStrategy} using the configured {@link #r2dbcMappingContext(Optional)}.
* Creates a {@link ReactiveDataAccessStrategy} using the configured {@link #r2dbcMappingContext(Optional) RelationalMappingContext}.
*
* @param mappingContext the configured {@link RelationalMappingContext}.
* @return must not be {@literal null}.
* @see #r2dbcMappingContext(Optional)
* @throws IllegalArgumentException if any of the {@literal mappingContext} is {@literal null}.
*/
@Bean
public ReactiveDataAccessStrategy reactiveDataAccessStrategy(RelationalMappingContext mappingContext) {
Assert.notNull(mappingContext, "MappingContext must not be null!");
return new DefaultReactiveDataAccessStrategy(new BasicRelationalConverter(mappingContext));
}
/**
* Creates a {@link R2dbcExceptionTranslator} using the configured {@link #connectionFactory()}.
* Creates a {@link R2dbcExceptionTranslator} using the configured {@link #connectionFactory() ConnectionFactory}.
*
* @return must not be {@literal null}.
* @see #connectionFactory()

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* 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.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.repository.config;
import java.lang.annotation.Documented;
@@ -37,6 +36,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
* annotated class.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@@ -87,7 +87,7 @@ public @interface EnableR2dbcRepositories {
/**
* Configures the location of where to find the Spring Data named queries properties file. Will default to
* {@code META-INF/r2dbc-named-queries.properties}.
* {@code META-INF/r2dbc-named-queries.properties} if not configured otherwise.
*
* @return
*/

View File

@@ -1,3 +1,18 @@
/*
* 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.repository.config;
import java.lang.annotation.Annotation;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* 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.

View File

@@ -0,0 +1,6 @@
/**
* Support infrastructure for the configuration of R2DBC-specific repositories.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.repository.config;

View File

@@ -1,7 +1,6 @@
/**
* R2DBC-specific repository implementation.
*/
@NonNullApi
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.repository;
import org.springframework.lang.NonNullApi;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* 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.
@@ -33,13 +33,15 @@ import org.springframework.util.Assert;
* {@link org.springframework.data.r2dbc.repository.R2dbcRepository} instances.
*
* @author Mark Paluch
* @author Christoph Strobl
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository
*/
public class R2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> {
private @Nullable DatabaseClient client;
private @Nullable MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
private @Nullable
MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
private boolean mappingContextConfigured = false;
@@ -67,13 +69,21 @@ public class R2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
*/
@Override
@SuppressWarnings("unchecked")
protected void setMappingContext(MappingContext<?, ?> mappingContext) {
protected void setMappingContext(@Nullable MappingContext<?, ?> mappingContext) {
super.setMappingContext(mappingContext);
this.mappingContext = (MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty>) mappingContext;
this.mappingContextConfigured = true;
if (mappingContext != null) {
this.mappingContext = (MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty>) mappingContext;
this.mappingContextConfigured = true;
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createRepositoryFactory()
*/
@Override
protected final RepositoryFactorySupport createRepositoryFactory() {
return getFactoryInstance(client, this.mappingContext);
@@ -82,15 +92,19 @@ public class R2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
/**
* Creates and initializes a {@link RepositoryFactorySupport} instance.
*
* @param client
* @param mappingContext
* @return
* @param client must not be {@literal null}.
* @param mappingContext must not be {@literal null}.
* @return new instance of {@link RepositoryFactorySupport}.
*/
protected RepositoryFactorySupport getFactoryInstance(DatabaseClient client,
MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext) {
return new R2dbcRepositoryFactory(client, mappingContext);
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {

View File

@@ -1,7 +1,6 @@
/**
* Support infrastructure for query derivation of R2DBC-specific repositories.
*/
@NonNullApi
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package org.springframework.data.r2dbc.repository.support;
import org.springframework.lang.NonNullApi;

View File

@@ -104,7 +104,7 @@ public class SqlErrorCodeR2dbcExceptionTranslator extends AbstractFallbackR2dbcE
*
* @param sec error codes
*/
public SqlErrorCodeR2dbcExceptionTranslator(SQLErrorCodes sec) {
public SqlErrorCodeR2dbcExceptionTranslator(@Nullable SQLErrorCodes sec) {
this();
this.sqlErrorCodes = sec;
}

View File

@@ -0,0 +1,5 @@
/**
* Support infrastructure for the configuration of R2DBC-specific repositories.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.r2dbc.support;