#41 - Polishing.

Rename RowDataConverter to R2dbcConverters. Introduce R2dbcSimpleTypeHolder. Apply custom conversions check in MappingR2dbcConverter. Extend tests.

Original pull request: #65.
This commit is contained in:
Mark Paluch
2019-03-10 11:29:11 +01:00
parent 455e9a59de
commit 369522231f
13 changed files with 529 additions and 350 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.data.r2dbc.config;
import io.r2dbc.spi.ConnectionFactory;
import java.util.Collections;
import java.util.Optional;
import org.springframework.context.annotation.Bean;
@@ -153,7 +152,7 @@ public abstract class AbstractR2dbcConfiguration {
Dialect dialect = getDialect(connectionFactory());
StoreConversions storeConversions = StoreConversions.of(dialect.getSimpleTypeHolder());
return new R2dbcCustomConversions(storeConversions, Collections.emptyList());
return new R2dbcCustomConversions(storeConversions, R2dbcCustomConversions.STORE_CONVERTERS);
}
/**

View File

@@ -3,6 +3,7 @@ package org.springframework.data.r2dbc.dialect;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.r2dbc.dialect.ArrayColumns.Unsupported;
@@ -40,7 +41,11 @@ public interface Dialect {
* @see #getSimpleTypes()
*/
default SimpleTypeHolder getSimpleTypeHolder() {
return new SimpleTypeHolder(new HashSet<>(getSimpleTypes()), true);
Set<Class<?>> simpleTypes = new HashSet<>(getSimpleTypes());
simpleTypes.addAll(R2dbcSimpleTypeHolder.R2DBC_SIMPLE_TYPES);
return new SimpleTypeHolder(simpleTypes, true);
}
/**

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2019 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.dialect;
import io.r2dbc.spi.Row;
import java.util.Collections;
import java.util.Set;
import org.springframework.data.mapping.model.SimpleTypeHolder;
/**
* Simple constant holder for a {@link SimpleTypeHolder} enriched with R2DBC specific simple types.
*
* @author Mark Paluch
*/
public class R2dbcSimpleTypeHolder extends SimpleTypeHolder {
/**
* Set of R2DBC simple types.
*/
public static final Set<Class<?>> R2DBC_SIMPLE_TYPES = Collections.singleton(Row.class);
public static final SimpleTypeHolder HOLDER = new R2dbcSimpleTypeHolder();
/**
* Create a new {@link R2dbcSimpleTypeHolder} instance.
*/
private R2dbcSimpleTypeHolder() {
super(R2DBC_SIMPLE_TYPES, true);
}
}

View File

@@ -42,6 +42,7 @@ import org.springframework.data.relational.core.conversion.BasicRelationalConver
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.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -61,7 +62,7 @@ public class MappingR2dbcConverter extends BasicRelationalConverter implements R
*/
public MappingR2dbcConverter(
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context) {
super(context, new R2dbcCustomConversions(CustomConversions.StoreConversions.NONE, Collections.emptyList()));
super(context, new R2dbcCustomConversions(Collections.emptyList()));
}
/**
@@ -81,6 +82,19 @@ public class MappingR2dbcConverter extends BasicRelationalConverter implements R
@Override
public <R> R read(Class<R> type, Row row) {
TypeInformation<? extends R> typeInfo = ClassTypeInformation.from(type);
Class<? extends R> rawType = typeInfo.getType();
if (Row.class.isAssignableFrom(rawType)) {
return type.cast(row);
}
if (getConversions().hasCustomReadTarget(Row.class, rawType)
|| getConversionService().canConvert(Row.class, rawType)) {
return getConversionService().convert(row, rawType);
}
return read(getRequiredPersistentEntity(type), row);
}

View File

@@ -0,0 +1,233 @@
/*
* Copyright 2019 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.convert;
import io.r2dbc.spi.Row;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToOffsetDateTimeConverter;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToStringConverter;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToUuidConverter;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToZonedDateTimeConverter;
import org.springframework.util.Assert;
import org.springframework.util.NumberUtils;
/**
* Wrapper class to contain useful converters for the usage with R2DBC.
*
* @author Hebert Coelho
* @author Mark Paluch
*/
abstract class R2dbcConverters {
private R2dbcConverters() {}
/**
* @return A list of the registered converters
*/
public static Collection<Object> getConvertersToRegister() {
List<Object> converters = new ArrayList<>();
converters.add(RowToBooleanConverter.INSTANCE);
converters.add(RowToNumberConverterFactory.INSTANCE);
converters.add(RowToLocalDateConverter.INSTANCE);
converters.add(RowToLocalDateTimeConverter.INSTANCE);
converters.add(RowToLocalTimeConverter.INSTANCE);
converters.add(RowToOffsetDateTimeConverter.INSTANCE);
converters.add(RowToStringConverter.INSTANCE);
converters.add(RowToUuidConverter.INSTANCE);
converters.add(RowToZonedDateTimeConverter.INSTANCE);
return converters;
}
/**
* Simple singleton to convert {@link Row}s to their {@link Boolean} representation.
*
* @author Hebert Coelho
*/
public enum RowToBooleanConverter implements Converter<Row, Boolean> {
INSTANCE;
@Override
public Boolean convert(Row row) {
return row.get(0, Boolean.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalDate} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalDateConverter implements Converter<Row, LocalDate> {
INSTANCE;
@Override
public LocalDate convert(Row row) {
return row.get(0, LocalDate.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalDateTimeConverter implements Converter<Row, LocalDateTime> {
INSTANCE;
@Override
public LocalDateTime convert(Row row) {
return row.get(0, LocalDateTime.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalTimeConverter implements Converter<Row, LocalTime> {
INSTANCE;
@Override
public LocalTime convert(Row row) {
return row.get(0, LocalTime.class);
}
}
/**
* Singleton converter factory to convert the first column of a {@link Row} to a {@link Number}.
* <p>
* Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class
* delegates to {@link NumberUtils#convertNumberToTargetClass(Number, Class)} to perform the conversion.
*
* @see Byte
* @see Short
* @see Integer
* @see Long
* @see java.math.BigInteger
* @see Float
* @see Double
* @see java.math.BigDecimal
* @author Hebert Coelho
*/
public enum RowToNumberConverterFactory implements ConverterFactory<Row, Number> {
INSTANCE;
@Override
public <T extends Number> Converter<Row, T> getConverter(Class<T> targetType) {
Assert.notNull(targetType, "Target type must not be null");
return new RowToNumber<>(targetType);
}
static class RowToNumber<T extends Number> implements Converter<Row, T> {
private final Class<T> targetType;
RowToNumber(Class<T> targetType) {
this.targetType = targetType;
}
@Override
public T convert(Row source) {
Object object = source.get(0, targetType);
return (object != null ? NumberUtils.convertNumberToTargetClass((Number) object, this.targetType) : null);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link OffsetDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToOffsetDateTimeConverter implements Converter<Row, OffsetDateTime> {
INSTANCE;
@Override
public OffsetDateTime convert(Row row) {
return row.get(0, OffsetDateTime.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link String} representation.
*
* @author Hebert Coelho
*/
public enum RowToStringConverter implements Converter<Row, String> {
INSTANCE;
@Override
public String convert(Row row) {
return row.get(0, String.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link UUID} representation.
*
* @author Hebert Coelho
*/
public enum RowToUuidConverter implements Converter<Row, UUID> {
INSTANCE;
@Override
public UUID convert(Row row) {
return row.get(0, UUID.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link ZonedDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToZonedDateTimeConverter implements Converter<Row, ZonedDateTime> {
INSTANCE;
@Override
public ZonedDateTime convert(Row row) {
return row.get(0, ZonedDateTime.class);
}
}
}
}

View File

@@ -1,8 +1,13 @@
package org.springframework.data.r2dbc.function.convert;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.JodaTimeConverters;
import org.springframework.data.r2dbc.dialect.R2dbcSimpleTypeHolder;
/**
* Value object to capture custom conversion. {@link R2dbcCustomConversions} also act as factory for
@@ -14,8 +19,32 @@ import org.springframework.data.convert.CustomConversions;
*/
public class R2dbcCustomConversions extends CustomConversions {
public static final List<Object> STORE_CONVERTERS;
private static final StoreConversions STORE_CONVERSIONS;
static {
List<Object> converters = new ArrayList<>();
converters.addAll(R2dbcConverters.getConvertersToRegister());
converters.addAll(JodaTimeConverters.getConvertersToRegister());
STORE_CONVERTERS = Collections.unmodifiableList(converters);
STORE_CONVERSIONS = StoreConversions.of(R2dbcSimpleTypeHolder.HOLDER, STORE_CONVERTERS);
}
/**
* Creates a new {@link CustomConversions} instance registering the given converters.
* Creates a new {@link R2dbcCustomConversions} instance registering the given converters.
*
* @param converters must not be {@literal null}.
*/
public R2dbcCustomConversions(Collection<?> converters) {
super(STORE_CONVERSIONS, converters);
}
/**
* Creates a new {@link R2dbcCustomConversions} instance registering the given converters.
*
* @param storeConversions must not be {@literal null}.
* @param converters must not be {@literal null}.

View File

@@ -1,220 +0,0 @@
/*
* Copyright 2019 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.query;
import io.r2dbc.spi.Row;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToOffsetDateTimeConverter;
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToStringConverter;
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToUuidConverter;
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToZonedDateTimeConverter;
import org.springframework.util.Assert;
import org.springframework.util.NumberUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
/**
* Base class for row data converter.
*
* @author Hebert Coelho
*/
abstract class RowDataConverter {
private RowDataConverter() {
}
/**
* @return A list of the registered converters
*/
static Collection<Object> getConvertersToRegister() {
List<Object> converters = new ArrayList<>();
converters.add(RowToBooleanConverter.INSTANCE);
converters.add(RowToLocalDateConverter.INSTANCE);
converters.add(RowToLocalDateTimeConverter.INSTANCE);
converters.add(RowToLocalTimeConverter.INSTANCE);
converters.add(RowToOffsetDateTimeConverter.INSTANCE);
converters.add(RowToStringConverter.INSTANCE);
converters.add(RowToUuidConverter.INSTANCE);
converters.add(RowToZonedDateTimeConverter.INSTANCE);
return converters;
}
/**
* Simple singleton to convert {@link Row}s to their {@link Boolean} representation.
*
* @author Hebert Coelho
*/
public enum RowToBooleanConverter implements Converter<Row, Boolean> {
INSTANCE;
@Override
public Boolean convert(Row row) {
return row.get(0, Boolean.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalDate} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalDateConverter implements Converter<Row, LocalDate> {
INSTANCE;
@Override
public LocalDate convert(Row row) {
return row.get(0, LocalDate.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalDateTimeConverter implements Converter<Row, LocalDateTime> {
INSTANCE;
@Override
public LocalDateTime convert(Row row) {
return row.get(0, LocalDateTime.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link LocalTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToLocalTimeConverter implements Converter<Row, LocalTime> {
INSTANCE;
@Override
public LocalTime convert(Row row) {
return row.get(0, LocalTime.class);
}
}
/**
* Singleton converter factory to convert the first column of a {@link Row} to a {@link Number}.
* <p>
* Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class
* delegates to {@link NumberUtils#convertNumberToTargetClass(Number, Class)} to perform the conversion.
*
* @see Byte
* @see Short
* @see Integer
* @see Long
* @see java.math.BigInteger
* @see Float
* @see Double
* @see java.math.BigDecimal
*
* @author Hebert Coelho
*/
public enum RowToNumberConverterFactory implements ConverterFactory<Row, Number> {
INSTANCE;
@Override
public <T extends Number> Converter<Row, T> getConverter(Class<T> targetType) {
Assert.notNull(targetType, "Target type must not be null");
return new RowToNumber<>(targetType);
}
private static final class RowToNumber<T extends Number> implements Converter<Row, T> {
private final Class<T> targetType;
RowToNumber(Class<T> targetType) {
this.targetType = targetType;
}
@Override
public T convert(Row source) {
Object object = source.get(0, targetType);
return (object != null ? NumberUtils.convertNumberToTargetClass((Number) object, this.targetType) : null);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link OffsetDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToOffsetDateTimeConverter implements Converter<Row, OffsetDateTime> {
INSTANCE;
@Override
public OffsetDateTime convert(Row row) {
return row.get(0, OffsetDateTime.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link String} representation.
*
* @author Hebert Coelho
*/
public enum RowToStringConverter implements Converter<Row, String> {
INSTANCE;
@Override
public String convert(Row row) {
return row.get(0, String.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link UUID} representation.
*
* @author Hebert Coelho
*/
public enum RowToUuidConverter implements Converter<Row, UUID> {
INSTANCE;
@Override
public UUID convert(Row row) {
return row.get(0, UUID.class);
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link ZonedDateTime} representation.
*
* @author Hebert Coelho
*/
public enum RowToZonedDateTimeConverter implements Converter<Row, ZonedDateTime> {
INSTANCE;
@Override
public ZonedDateTime convert(Row row) {
return row.get(0, ZonedDateTime.class);
}
}
}
}