#85 - Consider BigDecimal and BigInteger as simple types.

We now consider BigDecimal and BigInteger as simple types and no longer as entities.

We also no longer set properties whose value is null.
This commit is contained in:
Mark Paluch
2019-04-02 16:01:41 +02:00
parent 7e3bcceeda
commit 7c9e777d00
7 changed files with 388 additions and 7 deletions

View File

@@ -17,6 +17,8 @@ package org.springframework.data.r2dbc.dialect;
import io.r2dbc.spi.Row;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -36,7 +38,7 @@ public class R2dbcSimpleTypeHolder extends SimpleTypeHolder {
* Set of R2DBC simple types.
*/
public static final Set<Class<?>> R2DBC_SIMPLE_TYPES = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList(OutboundRow.class, Row.class)));
.unmodifiableSet(new HashSet<>(Arrays.asList(OutboundRow.class, Row.class, BigInteger.class, BigDecimal.class)));
public static final SimpleTypeHolder HOLDER = new R2dbcSimpleTypeHolder();

View File

@@ -114,7 +114,11 @@ public class MappingR2dbcConverter extends BasicRelationalConverter implements R
continue;
}
propertyAccessor.setProperty(property, readFrom(row, property, ""));
Object value = readFrom(row, property, "");
if (value != null) {
propertyAccessor.setProperty(property, value);
}
}
return result;
@@ -266,8 +270,7 @@ public class MappingR2dbcConverter extends BasicRelationalConverter implements R
private void writeNullInternal(OutboundRow sink, RelationalPersistentProperty property) {
sink.put(property.getColumnName(),
SettableValue.empty(getPotentiallyConvertedSimpleNullType(property.getType())));
sink.put(property.getColumnName(), SettableValue.empty(getPotentiallyConvertedSimpleNullType(property.getType())));
}
private Class<?> getPotentiallyConvertedSimpleNullType(Class<?> type) {
@@ -419,7 +422,14 @@ public class MappingR2dbcConverter extends BasicRelationalConverter implements R
String column = prefix + property.getColumnName();
try {
return converter.getConversionService().convert(resultSet.get(column), parameter.getType().getType());
Object value = resultSet.get(column);
if (value == null) {
return null;
}
return converter.getConversionService().convert(value, parameter.getType().getType());
} catch (Exception o_O) {
throw new MappingException(String.format("Couldn't read column %s from Row.", column), o_O);
}

View File

@@ -30,6 +30,12 @@ import java.util.UUID;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalDateConverterOverride;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalDateTimeConverterOverride;
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalTimeConverterOverride;
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;
@@ -67,6 +73,22 @@ abstract class R2dbcConverters {
return converters;
}
/**
* @return A list of the registered converters to enforce JSR-310 type usage.
* @see CustomConversions#DEFAULT_CONVERTERS
* @see Jsr310Converters
*/
public static Collection<Object> getOverrideConvertersToRegister() {
List<Object> converters = new ArrayList<>();
converters.add(LocalDateConverterOverride.INSTANCE);
converters.add(LocalDateTimeConverterOverride.INSTANCE);
converters.add(LocalTimeConverterOverride.INSTANCE);
return converters;
}
/**
* Simple singleton to convert {@link Row}s to their {@link Boolean} representation.
*
@@ -229,5 +251,53 @@ abstract class R2dbcConverters {
return row.get(0, ZonedDateTime.class);
}
}
/**
* {@link Converter} override that forces {@link LocalDate} to stay on {@link LocalDate}.
*
* @author Mark Paluch
*/
@WritingConverter
public enum LocalDateConverterOverride implements Converter<LocalDate, LocalDate> {
INSTANCE;
@Override
public LocalDate convert(LocalDate value) {
return value;
}
}
/**
* {@link Converter} override that forces {@link LocalDateTime} to stay on {@link LocalDateTime}.
*
* @author Mark Paluch
*/
@WritingConverter
public enum LocalDateTimeConverterOverride implements Converter<LocalDateTime, LocalDateTime> {
INSTANCE;
@Override
public LocalDateTime convert(LocalDateTime value) {
return value;
}
}
/**
* {@link Converter} override that forces {@link LocalTime} to stay on {@link LocalTime}.
*
* @author Mark Paluch
*/
@WritingConverter
public enum LocalTimeConverterOverride implements Converter<LocalTime, LocalTime> {
INSTANCE;
@Override
public LocalTime convert(LocalTime value) {
return value;
}
}
}
}

View File

@@ -40,7 +40,7 @@ public class R2dbcCustomConversions extends CustomConversions {
* @param converters must not be {@literal null}.
*/
public R2dbcCustomConversions(Collection<?> converters) {
super(STORE_CONVERSIONS, converters);
super(STORE_CONVERSIONS, appendOverriddes(converters));
}
/**
@@ -50,6 +50,14 @@ public class R2dbcCustomConversions extends CustomConversions {
* @param converters must not be {@literal null}.
*/
public R2dbcCustomConversions(StoreConversions storeConversions, Collection<?> converters) {
super(storeConversions, converters);
super(storeConversions, appendOverriddes(converters));
}
private static Collection<?> appendOverriddes(Collection<?> converters) {
List<Object> objects = new ArrayList<>(converters);
objects.addAll(R2dbcConverters.getOverrideConvertersToRegister());
return objects;
}
}