Skip to content

Commit 5399ec9

Browse files
committed
Polishing.
Remove code duplicates with method calls. Simplify getReference(…) calls. See #530.
1 parent 457fdb9 commit 5399ec9

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.springframework.data.relational.core.dialect.ArrayColumns;
4848
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
4949
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
50-
import org.springframework.data.relational.core.sql.IdentifierProcessing;
5150
import org.springframework.data.util.ClassTypeInformation;
5251
import org.springframework.data.util.TypeInformation;
5352
import org.springframework.lang.Nullable;
@@ -155,16 +154,18 @@ private <R> R read(RelationalPersistentEntity<R> entity, Row row, @Nullable RowM
155154
* @param prefix to be used for all column names accessed by this method. Must not be {@literal null}.
156155
* @return the value read from the {@link Row}. May be {@literal null}.
157156
*/
157+
@Nullable
158158
private Object readFrom(Row row, @Nullable RowMetadata metadata, RelationalPersistentProperty property,
159159
String prefix) {
160160

161+
String identifier = prefix + property.getColumnName().getReference();
162+
161163
try {
162164

163165
if (property.isEntity()) {
164166
return readEntityFrom(row, metadata, property);
165167
}
166168

167-
String identifier = prefix + property.getColumnName().getReference();
168169
if (metadata != null && !metadata.getColumnNames().contains(identifier)) {
169170
return null;
170171
}
@@ -173,7 +174,8 @@ private Object readFrom(Row row, @Nullable RowMetadata metadata, RelationalPersi
173174
return readValue(value, property.getTypeInformation());
174175

175176
} catch (Exception o_O) {
176-
throw new MappingException(String.format("Could not read property %s from result set!", property), o_O);
177+
throw new MappingException(String.format("Could not read property %s from column %s!", property, identifier),
178+
o_O);
177179
}
178180
}
179181

@@ -274,8 +276,10 @@ private <S> S readEntityFrom(Row row, RowMetadata metadata, PersistentProperty<?
274276

275277
RelationalPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(property.getActualType());
276278

277-
if (readFrom(row, metadata, entity.getRequiredIdProperty(), prefix) == null) {
278-
return null;
279+
if (entity.hasIdProperty()) {
280+
if (readFrom(row, metadata, entity.getRequiredIdProperty(), prefix) == null) {
281+
return null;
282+
}
279283
}
280284

281285
Object instance = createInstance(row, metadata, prefix, entity);
@@ -637,7 +641,7 @@ private boolean potentiallySetId(Row row, RowMetadata metadata, PersistentProper
637641

638642
Collection<String> columns = metadata.getColumnNames();
639643
Object generatedIdValue = null;
640-
String idColumnName = idProperty.getColumnName().getReference(IdentifierProcessing.NONE);
644+
String idColumnName = idProperty.getColumnName().getReference();
641645

642646
if (columns.contains(idColumnName)) {
643647
generatedIdValue = row.get(idColumnName);
@@ -688,7 +692,7 @@ public <T> T getParameterValue(PreferredConstructor.Parameter<T, RelationalPersi
688692
}
689693
}
690694

691-
private static class RowParameterValueProvider implements ParameterValueProvider<RelationalPersistentProperty> {
695+
private class RowParameterValueProvider implements ParameterValueProvider<RelationalPersistentProperty> {
692696

693697
private final Row resultSet;
694698
private final RowMetadata metadata;
@@ -714,30 +718,22 @@ public RowParameterValueProvider(Row resultSet, RowMetadata metadata, Relational
714718
public <T> T getParameterValue(PreferredConstructor.Parameter<T, RelationalPersistentProperty> parameter) {
715719

716720
RelationalPersistentProperty property = this.entity.getRequiredPersistentProperty(parameter.getName());
721+
Object value = readFrom(this.resultSet, this.metadata, property, this.prefix);
717722

718-
String reference = property.getColumnName().getReference(IdentifierProcessing.NONE);
719-
String column = this.prefix.isEmpty() ? reference : this.prefix + reference;
720-
721-
try {
722-
723-
if (this.metadata != null && !this.metadata.getColumnNames().contains(column)) {
724-
return null;
725-
}
726-
727-
Object value = this.resultSet.get(column);
723+
if (value == null) {
724+
return null;
725+
}
728726

729-
if (value == null) {
730-
return null;
731-
}
727+
Class<T> type = parameter.getType().getType();
732728

733-
Class<T> type = parameter.getType().getType();
729+
if (type.isInstance(value)) {
730+
return type.cast(value);
731+
}
734732

735-
if (type.isInstance(value)) {
736-
return type.cast(value);
737-
}
733+
try {
738734
return this.converter.getConversionService().convert(value, type);
739735
} catch (Exception o_O) {
740-
throw new MappingException(String.format("Couldn't read column %s from Row.", column), o_O);
736+
throw new MappingException(String.format("Couldn't read parameter %s.", parameter.getName()), o_O);
741737
}
742738
}
743739
}

0 commit comments

Comments
 (0)