DATAJDBC-235 - Incorporate feedback from review.

Refactor ResultSetParameterValueProvider into Function<Parameter, Object>. Remove unnecessary assertions.
This commit is contained in:
Mark Paluch
2018-07-18 14:33:33 +02:00
committed by Jens Schauder
parent fb858bf1b1
commit 2c5489b070
5 changed files with 15 additions and 44 deletions

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.data.jdbc.core;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
@@ -26,8 +23,6 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -144,33 +139,18 @@ public class EntityRowMapper<T> implements RowMapper<T> {
}
private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, String prefix) {
return converter.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, prefix));
}
@RequiredArgsConstructor
private static class ResultSetParameterValueProvider implements ParameterValueProvider<RelationalPersistentProperty> {
@NonNull private final ResultSet resultSet;
@NonNull private final RelationalPersistentEntity<?> entity;
@NonNull private final String prefix;
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
@SuppressWarnings("unchecked")
@Override
public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parameter) {
return converter.createInstance(entity, parameter -> {
String parameterName = parameter.getName();
Assert.notNull(parameterName, "A constructor parameter name must not be null to be used with Spring Data JDBC");
String column = prefix + entity.getRequiredPersistentProperty(parameterName).getColumnName();
try {
return (T) resultSet.getObject(column);
return rs.getObject(column);
} catch (SQLException o_O) {
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", column), o_O);
}
}
});
}
}

View File

@@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
@@ -130,11 +131,11 @@ public class BasicRelationalConverter implements RelationalConverter {
/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.conversion.RelationalConverter#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider)
* @see org.springframework.data.relational.core.conversion.RelationalConverter#createInstance(org.springframework.data.mapping.PersistentEntity, java.util.function.Function)
*/
@Override
public <T> T createInstance(PersistentEntity<T, RelationalPersistentProperty> entity,
ParameterValueProvider<RelationalPersistentProperty> parameterValueProvider) {
Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider) {
return entityInstantiators.getInstantiatorFor(entity) //
.createInstance(entity, new ConvertingParameterValueProvider<>(parameterValueProvider));
@@ -154,9 +155,9 @@ public class BasicRelationalConverter implements RelationalConverter {
if (conversions.hasCustomReadTarget(value.getClass(), type.getType())) {
return conversionService.convert(value, type.getType());
} else {
return getPotentiallyConvertedSimpleRead(value, type.getType());
}
return getPotentiallyConvertedSimpleRead(value, type.getType());
}
/*
@@ -221,10 +222,6 @@ public class BasicRelationalConverter implements RelationalConverter {
return value;
}
if (conversions.hasCustomReadTarget(value.getClass(), target)) {
return conversionService.convert(value, target);
}
if (Enum.class.isAssignableFrom(target)) {
return Enum.valueOf((Class<Enum>) target, value.toString());
}
@@ -241,7 +238,7 @@ public class BasicRelationalConverter implements RelationalConverter {
@RequiredArgsConstructor
class ConvertingParameterValueProvider<P extends PersistentProperty<P>> implements ParameterValueProvider<P> {
private final ParameterValueProvider<P> delegate;
private final Function<Parameter<?, P>, Object> delegate;
/*
* (non-Javadoc)
@@ -250,7 +247,7 @@ public class BasicRelationalConverter implements RelationalConverter {
@Override
@SuppressWarnings("unchecked")
public <T> T getParameterValue(Parameter<T, P> parameter) {
return (T) readValue(delegate.getParameterValue(parameter), parameter.getType());
return (T) readValue(delegate.apply(parameter), parameter.getType());
}
}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.relational.core.conversion;
import java.util.function.Function;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -57,7 +60,7 @@ public interface RelationalConverter {
* @return
*/
<T> T createInstance(PersistentEntity<T, RelationalPersistentProperty> entity,
ParameterValueProvider<RelationalPersistentProperty> parameterValueProvider);
Function<Parameter<?, RelationalPersistentProperty>, Object> parameterValueProvider);
/**
* Return a {@link PersistentPropertyAccessor} to access property values of the {@code instance}.

View File

@@ -111,8 +111,6 @@ public class DefaultDataAccessStrategyUnitTests {
verify(jdbcOperations).update(sqlCaptor.capture(), paramSourceCaptor.capture(), any(KeyHolder.class));
assertThat(sqlCaptor.getValue()) //
.contains("INSERT INTO entity_with_boolean (flag, id) VALUES (:flag, :id)");
assertThat(paramSourceCaptor.getValue().getValue("id")).isEqualTo(ORIGINAL_ID);
assertThat(paramSourceCaptor.getValue().getValue("flag")).isEqualTo("T");
}

View File

@@ -22,8 +22,6 @@ import lombok.Value;
import org.junit.Test;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
@@ -78,12 +76,7 @@ public class BasicRelationalConverterUnitTests {
RelationalPersistentEntity<MyValue> entity = (RelationalPersistentEntity) context
.getRequiredPersistentEntity(MyValue.class);
MyValue result = converter.createInstance(entity, new ParameterValueProvider<RelationalPersistentProperty>() {
@Override
public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parameter) {
return (T) "bar";
}
});
MyValue result = converter.createInstance(entity, it -> "bar");
assertThat(result.getFoo()).isEqualTo("bar");
}