Skip to content

Commit da8a7b3

Browse files
committed
#155 - Add early returns if conversion can be skipped.
1 parent 412e851 commit da8a7b3

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public <R> R read(Class<R> type, Row row, @Nullable RowMetadata metadata) {
107107
}
108108

109109
if (getConversions().hasCustomReadTarget(Row.class, rawType)
110-
|| getConversionService().canConvert(Row.class, rawType)) {
110+
&& getConversionService().canConvert(Row.class, rawType)) {
111111
return getConversionService().convert(row, rawType);
112112
}
113113

@@ -118,19 +118,21 @@ private <R> R read(RelationalPersistentEntity<R> entity, Row row, @Nullable RowM
118118

119119
R result = createInstance(row, metadata, "", entity);
120120

121-
ConvertingPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>(
122-
entity.getPropertyAccessor(result), getConversionService());
121+
if (entity.requiresPropertyPopulation()) {
122+
ConvertingPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>(
123+
entity.getPropertyAccessor(result), getConversionService());
123124

124-
for (RelationalPersistentProperty property : entity) {
125+
for (RelationalPersistentProperty property : entity) {
125126

126-
if (entity.isConstructorArgument(property)) {
127-
continue;
128-
}
127+
if (entity.isConstructorArgument(property)) {
128+
continue;
129+
}
129130

130-
Object value = readFrom(row, metadata, property, "");
131+
Object value = readFrom(row, metadata, property, "");
131132

132-
if (value != null) {
133-
propertyAccessor.setProperty(property, value);
133+
if (value != null) {
134+
propertyAccessor.setProperty(property, value);
135+
}
134136
}
135137
}
136138

@@ -209,12 +211,15 @@ private <S> S readEntityFrom(Row row, RowMetadata metadata, PersistentProperty<?
209211

210212
Object instance = createInstance(row, metadata, prefix, entity);
211213

212-
PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(instance);
213-
ConvertingPropertyAccessor<?> propertyAccessor = new ConvertingPropertyAccessor<>(accessor, getConversionService());
214+
if (entity.requiresPropertyPopulation()) {
215+
PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(instance);
216+
ConvertingPropertyAccessor<?> propertyAccessor = new ConvertingPropertyAccessor<>(accessor,
217+
getConversionService());
214218

215-
for (RelationalPersistentProperty p : entity) {
216-
if (!entity.isConstructorArgument(property)) {
217-
propertyAccessor.setProperty(p, readFrom(row, metadata, p, prefix));
219+
for (RelationalPersistentProperty p : entity) {
220+
if (!entity.isConstructorArgument(property)) {
221+
propertyAccessor.setProperty(p, readFrom(row, metadata, p, prefix));
222+
}
218223
}
219224
}
220225

@@ -435,7 +440,7 @@ private static Map<String, ColumnMetadata> createMetadataMap(RowMetadata metadat
435440
private static class RowParameterValueProvider implements ParameterValueProvider<RelationalPersistentProperty> {
436441

437442
private final Row resultSet;
438-
private final @Nullable RowMetadata metadata;
443+
private final RowMetadata metadata;
439444
private final RelationalPersistentEntity<?> entity;
440445
private final RelationalConverter converter;
441446
private final String prefix;
@@ -458,7 +463,7 @@ public RowParameterValueProvider(Row resultSet, RowMetadata metadata, Relational
458463
public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parameter) {
459464

460465
RelationalPersistentProperty property = this.entity.getRequiredPersistentProperty(parameter.getName());
461-
String column = this.prefix + property.getColumnName();
466+
String column = this.prefix.isEmpty() ? property.getColumnName() : this.prefix + property.getColumnName();
462467

463468
try {
464469

@@ -472,7 +477,12 @@ public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parame
472477
return null;
473478
}
474479

475-
return this.converter.getConversionService().convert(value, parameter.getType().getType());
480+
Class<T> type = parameter.getType().getType();
481+
482+
if (type.isInstance(value)) {
483+
return type.cast(value);
484+
}
485+
return this.converter.getConversionService().convert(value, type);
476486
} catch (Exception o_O) {
477487
throw new MappingException(String.format("Couldn't read column %s from Row.", column), o_O);
478488
}

0 commit comments

Comments
 (0)