Skip to content

Commit 2af0bf6

Browse files
committed
#82 - 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.
1 parent 7e3bcce commit 2af0bf6

File tree

7 files changed

+388
-7
lines changed

7 files changed

+388
-7
lines changed

src/main/java/org/springframework/data/r2dbc/dialect/R2dbcSimpleTypeHolder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import io.r2dbc.spi.Row;
1919

20+
import java.math.BigDecimal;
21+
import java.math.BigInteger;
2022
import java.util.Arrays;
2123
import java.util.Collections;
2224
import java.util.HashSet;
@@ -36,7 +38,7 @@ public class R2dbcSimpleTypeHolder extends SimpleTypeHolder {
3638
* Set of R2DBC simple types.
3739
*/
3840
public static final Set<Class<?>> R2DBC_SIMPLE_TYPES = Collections
39-
.unmodifiableSet(new HashSet<>(Arrays.asList(OutboundRow.class, Row.class)));
41+
.unmodifiableSet(new HashSet<>(Arrays.asList(OutboundRow.class, Row.class, BigInteger.class, BigDecimal.class)));
4042

4143
public static final SimpleTypeHolder HOLDER = new R2dbcSimpleTypeHolder();
4244

src/main/java/org/springframework/data/r2dbc/function/convert/MappingR2dbcConverter.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ private <R> R read(RelationalPersistentEntity<R> entity, Row row) {
114114
continue;
115115
}
116116

117-
propertyAccessor.setProperty(property, readFrom(row, property, ""));
117+
Object value = readFrom(row, property, "");
118+
119+
if (value != null) {
120+
propertyAccessor.setProperty(property, value);
121+
}
118122
}
119123

120124
return result;
@@ -266,8 +270,7 @@ private void writeSimpleInternal(OutboundRow sink, Object value, RelationalPersi
266270

267271
private void writeNullInternal(OutboundRow sink, RelationalPersistentProperty property) {
268272

269-
sink.put(property.getColumnName(),
270-
SettableValue.empty(getPotentiallyConvertedSimpleNullType(property.getType())));
273+
sink.put(property.getColumnName(), SettableValue.empty(getPotentiallyConvertedSimpleNullType(property.getType())));
271274
}
272275

273276
private Class<?> getPotentiallyConvertedSimpleNullType(Class<?> type) {
@@ -419,7 +422,14 @@ public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parame
419422
String column = prefix + property.getColumnName();
420423

421424
try {
422-
return converter.getConversionService().convert(resultSet.get(column), parameter.getType().getType());
425+
426+
Object value = resultSet.get(column);
427+
428+
if (value == null) {
429+
return null;
430+
}
431+
432+
return converter.getConversionService().convert(value, parameter.getType().getType());
423433
} catch (Exception o_O) {
424434
throw new MappingException(String.format("Couldn't read column %s from Row.", column), o_O);
425435
}

src/main/java/org/springframework/data/r2dbc/function/convert/R2dbcConverters.java

+70
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
import org.springframework.core.convert.converter.Converter;
3232
import org.springframework.core.convert.converter.ConverterFactory;
33+
import org.springframework.data.convert.CustomConversions;
34+
import org.springframework.data.convert.Jsr310Converters;
35+
import org.springframework.data.convert.WritingConverter;
36+
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalDateConverterOverride;
37+
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalDateTimeConverterOverride;
38+
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.LocalTimeConverterOverride;
3339
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToOffsetDateTimeConverter;
3440
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToStringConverter;
3541
import org.springframework.data.r2dbc.function.convert.R2dbcConverters.RowToNumberConverterFactory.RowToUuidConverter;
@@ -67,6 +73,22 @@ public static Collection<Object> getConvertersToRegister() {
6773
return converters;
6874
}
6975

76+
/**
77+
* @return A list of the registered converters to enforce JSR-310 type usage.
78+
* @see CustomConversions#DEFAULT_CONVERTERS
79+
* @see Jsr310Converters
80+
*/
81+
public static Collection<Object> getOverrideConvertersToRegister() {
82+
83+
List<Object> converters = new ArrayList<>();
84+
85+
converters.add(LocalDateConverterOverride.INSTANCE);
86+
converters.add(LocalDateTimeConverterOverride.INSTANCE);
87+
converters.add(LocalTimeConverterOverride.INSTANCE);
88+
89+
return converters;
90+
}
91+
7092
/**
7193
* Simple singleton to convert {@link Row}s to their {@link Boolean} representation.
7294
*
@@ -229,5 +251,53 @@ public ZonedDateTime convert(Row row) {
229251
return row.get(0, ZonedDateTime.class);
230252
}
231253
}
254+
255+
/**
256+
* {@link Converter} override that forces {@link LocalDate} to stay on {@link LocalDate}.
257+
*
258+
* @author Mark Paluch
259+
*/
260+
@WritingConverter
261+
public enum LocalDateConverterOverride implements Converter<LocalDate, LocalDate> {
262+
263+
INSTANCE;
264+
265+
@Override
266+
public LocalDate convert(LocalDate value) {
267+
return value;
268+
}
269+
}
270+
271+
/**
272+
* {@link Converter} override that forces {@link LocalDateTime} to stay on {@link LocalDateTime}.
273+
*
274+
* @author Mark Paluch
275+
*/
276+
@WritingConverter
277+
public enum LocalDateTimeConverterOverride implements Converter<LocalDateTime, LocalDateTime> {
278+
279+
INSTANCE;
280+
281+
@Override
282+
public LocalDateTime convert(LocalDateTime value) {
283+
return value;
284+
}
285+
}
286+
287+
/**
288+
* {@link Converter} override that forces {@link LocalTime} to stay on {@link LocalTime}.
289+
*
290+
* @author Mark Paluch
291+
*/
292+
@WritingConverter
293+
public enum LocalTimeConverterOverride implements Converter<LocalTime, LocalTime> {
294+
295+
INSTANCE;
296+
297+
@Override
298+
public LocalTime convert(LocalTime value) {
299+
return value;
300+
}
301+
}
232302
}
233303
}

src/main/java/org/springframework/data/r2dbc/function/convert/R2dbcCustomConversions.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class R2dbcCustomConversions extends CustomConversions {
4040
* @param converters must not be {@literal null}.
4141
*/
4242
public R2dbcCustomConversions(Collection<?> converters) {
43-
super(STORE_CONVERSIONS, converters);
43+
super(STORE_CONVERSIONS, appendOverriddes(converters));
4444
}
4545

4646
/**
@@ -50,6 +50,14 @@ public R2dbcCustomConversions(Collection<?> converters) {
5050
* @param converters must not be {@literal null}.
5151
*/
5252
public R2dbcCustomConversions(StoreConversions storeConversions, Collection<?> converters) {
53-
super(storeConversions, converters);
53+
super(storeConversions, appendOverriddes(converters));
54+
}
55+
56+
private static Collection<?> appendOverriddes(Collection<?> converters) {
57+
58+
List<Object> objects = new ArrayList<>(converters);
59+
objects.addAll(R2dbcConverters.getOverrideConvertersToRegister());
60+
61+
return objects;
5462
}
5563
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.function;
17+
18+
import org.springframework.data.r2dbc.dialect.PostgresDialect;
19+
20+
/**
21+
* {@link PostgresDialect} specific tests for {@link ReactiveDataAccessStrategy}.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessStrategyTestSupport {
26+
27+
private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
28+
29+
@Override
30+
protected ReactiveDataAccessStrategy getStrategy() {
31+
return strategy;
32+
}
33+
}

0 commit comments

Comments
 (0)