Browse Source

Polishing.

Tweak method names. Rebase onto main. Convert issue references to GH- from hash prefixed ones. Remove final keyword from variable and parameter declarations.

Original pull request: #999.
See #987
pull/1018/head
Mark Paluch 4 years ago
parent
commit
7d67f30bbd
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 19
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java
  2. 23
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/QueryMapper.java
  3. 6
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java
  4. 12
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java
  5. 15
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/BasicRelationalConverter.java

19
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java

@ -19,7 +19,6 @@ import java.sql.Array;
import java.sql.JDBCType; import java.sql.JDBCType;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -28,7 +27,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
@ -228,12 +226,10 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc
TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(value.getClass()); TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(value.getClass());
TypeDescriptor targetDescriptor = createTypeDescriptor(type); TypeDescriptor targetDescriptor = createTypeDescriptor(type);
return getConversionService().convert(value, sourceDescriptor, return getConversionService().convert(value, sourceDescriptor, targetDescriptor);
targetDescriptor);
} }
if ( !getConversions().hasCustomReadTarget(value.getClass(), type.getType()) && if (value instanceof Array) {
value instanceof Array) {
try { try {
return readValue(((Array) value).getArray(), type); return readValue(((Array) value).getArray(), type);
} catch (SQLException | ConverterNotFoundException e) { } catch (SQLException | ConverterNotFoundException e) {
@ -244,17 +240,6 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc
return super.readValue(value, type); return super.readValue(value, type);
} }
private static TypeDescriptor createTypeDescriptor(TypeInformation<?> type) {
List<TypeInformation<?>> typeArguments = type.getTypeArguments();
Class<?>[] generics = new Class[typeArguments.size()];
for (int i = 0; i < typeArguments.size(); i++) {
generics[i] = typeArguments.get(i).getType();
}
return new TypeDescriptor(ResolvableType.forClassWithGenerics(type.getType(), generics), type.getType(), null);
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.relational.core.conversion.RelationalConverter#writeValue(java.lang.Object, org.springframework.data.util.TypeInformation) * @see org.springframework.data.relational.core.conversion.RelationalConverter#writeValue(java.lang.Object, org.springframework.data.util.TypeInformation)

23
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/QueryMapper.java

@ -55,6 +55,7 @@ import org.springframework.util.ClassUtils;
* conversion. * conversion.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Jens Schauder
* @since 2.0 * @since 2.0
*/ */
class QueryMapper { class QueryMapper {
@ -299,7 +300,7 @@ class QueryMapper {
&& (criteria.getValue() == null || !criteria.getValue().getClass().isArray())) { && (criteria.getValue() == null || !criteria.getValue().getClass().isArray())) {
RelationalPersistentProperty property = ((MetadataBackedField) propertyField).property; RelationalPersistentProperty property = ((MetadataBackedField) propertyField).property;
JdbcValue jdbcValue = convertSpecial(property, criteria.getValue()); JdbcValue jdbcValue = convertToJdbcValue(property, criteria.getValue());
mappedValue = jdbcValue.getValue(); mappedValue = jdbcValue.getValue();
sqlType = jdbcValue.getJdbcType() != null ? jdbcValue.getJdbcType().getVendorTypeNumber() sqlType = jdbcValue.getJdbcType() != null ? jdbcValue.getJdbcType().getVendorTypeNumber()
: propertyField.getSqlType(); : propertyField.getSqlType();
@ -315,14 +316,14 @@ class QueryMapper {
} }
/** /**
* Converts values while taking special value types like arrays, {@link Iterable}, or {@link Pair}. * Converts values while taking specific value types like arrays, {@link Iterable}, or {@link Pair}.
* *
* @param property the property to which the value relates. It determines the type to convert to. Must not be * @param property the property to which the value relates. It determines the type to convert to. Must not be
* {@literal null}. * {@literal null}.
* @param value the value to be converted. * @param value the value to be converted.
* @return a non null {@link JdbcValue} holding the converted value and the appropriate JDBC type information. * @return a non null {@link JdbcValue} holding the converted value and the appropriate JDBC type information.
*/ */
private JdbcValue convertSpecial(RelationalPersistentProperty property, @Nullable Object value) { private JdbcValue convertToJdbcValue(RelationalPersistentProperty property, @Nullable Object value) {
if (value == null) { if (value == null) {
return JdbcValue.of(null, JDBCType.NULL); return JdbcValue.of(null, JDBCType.NULL);
@ -330,8 +331,8 @@ class QueryMapper {
if (value instanceof Pair) { if (value instanceof Pair) {
final JdbcValue first = convertSimple(property, ((Pair<?, ?>) value).getFirst()); JdbcValue first = getWriteValue(property, ((Pair<?, ?>) value).getFirst());
final JdbcValue second = convertSimple(property, ((Pair<?, ?>) value).getSecond()); JdbcValue second = getWriteValue(property, ((Pair<?, ?>) value).getSecond());
return JdbcValue.of(Pair.of(first.getValue(), second.getValue()), first.getJdbcType()); return JdbcValue.of(Pair.of(first.getValue(), second.getValue()), first.getJdbcType());
} }
@ -342,7 +343,7 @@ class QueryMapper {
for (Object o : (Iterable<?>) value) { for (Object o : (Iterable<?>) value) {
final JdbcValue jdbcValue = convertSimple(property, o); JdbcValue jdbcValue = getWriteValue(property, o);
if (jdbcType == null) { if (jdbcType == null) {
jdbcType = jdbcValue.getJdbcType(); jdbcType = jdbcValue.getJdbcType();
} }
@ -355,13 +356,13 @@ class QueryMapper {
if (value.getClass().isArray()) { if (value.getClass().isArray()) {
final Object[] valueAsArray = (Object[]) value; Object[] valueAsArray = (Object[]) value;
final Object[] mappedValueArray = new Object[valueAsArray.length]; Object[] mappedValueArray = new Object[valueAsArray.length];
JDBCType jdbcType = null; JDBCType jdbcType = null;
for (int i = 0; i < valueAsArray.length; i++) { for (int i = 0; i < valueAsArray.length; i++) {
final JdbcValue jdbcValue = convertSimple(property, valueAsArray[i]); JdbcValue jdbcValue = getWriteValue(property, valueAsArray[i]);
if (jdbcType == null) { if (jdbcType == null) {
jdbcType = jdbcValue.getJdbcType(); jdbcType = jdbcValue.getJdbcType();
} }
@ -372,7 +373,7 @@ class QueryMapper {
return JdbcValue.of(mappedValueArray, jdbcType); return JdbcValue.of(mappedValueArray, jdbcType);
} }
return convertSimple(property, value); return getWriteValue(property, value);
} }
/** /**
@ -383,7 +384,7 @@ class QueryMapper {
* @param value the value to be converted. * @param value the value to be converted.
* @return a non null {@link JdbcValue} holding the converted value and the appropriate JDBC type information. * @return a non null {@link JdbcValue} holding the converted value and the appropriate JDBC type information.
*/ */
private JdbcValue convertSimple(RelationalPersistentProperty property, Object value) { private JdbcValue getWriteValue(RelationalPersistentProperty property, Object value) {
return converter.writeJdbcValue( // return converter.writeJdbcValue( //
value, // value, //

6
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -32,12 +32,12 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -515,7 +515,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat(result).extracting(e -> e.idProp).containsExactly(entity.idProp); assertThat(result).extracting(e -> e.idProp).containsExactly(entity.idProp);
} }
@Test // #987 @Test // GH-987
void queryBySimpleReference() { void queryBySimpleReference() {
final DummyEntity one = repository.save(createDummyEntity()); final DummyEntity one = repository.save(createDummyEntity());
@ -528,7 +528,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat(result).extracting(e -> e.idProp).containsExactly(two.idProp); assertThat(result).extracting(e -> e.idProp).containsExactly(two.idProp);
} }
@Test // #987 @Test // GH-987
void queryByAggregateReference() { void queryByAggregateReference() {
final DummyEntity one = repository.save(createDummyEntity()); final DummyEntity one = repository.save(createDummyEntity());

12
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java

@ -78,12 +78,12 @@ public class PartTreeJdbcQueryUnitTests {
assertThatIllegalArgumentException().isThrownBy(() -> createQuery(queryMethod)); assertThatIllegalArgumentException().isThrownBy(() -> createQuery(queryMethod));
} }
@Test // #922 @Test // GH-922
public void createQueryByAggregateReference() throws Exception { public void createQueryByAggregateReference() throws Exception {
JdbcQueryMethod queryMethod = getQueryMethod("findAllByHobbyReference", Hobby.class); JdbcQueryMethod queryMethod = getQueryMethod("findAllByHobbyReference", Hobby.class);
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod); PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
final Hobby hobby = new Hobby(); Hobby hobby = new Hobby();
hobby.name = "twentythree"; hobby.name = "twentythree";
ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType); ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType);
@ -110,12 +110,12 @@ public class PartTreeJdbcQueryUnitTests {
assertThatIllegalArgumentException().isThrownBy(() -> createQuery(queryMethod)); assertThatIllegalArgumentException().isThrownBy(() -> createQuery(queryMethod));
} }
@Test // #922 @Test // GH-922
public void createQueryForQueryByAggregateReference() throws Exception { public void createQueryForQueryByAggregateReference() throws Exception {
JdbcQueryMethod queryMethod = getQueryMethod("findViaReferenceByHobbyReference", AggregateReference.class); JdbcQueryMethod queryMethod = getQueryMethod("findViaReferenceByHobbyReference", AggregateReference.class);
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod); PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
final AggregateReference<Object, String> hobby = AggregateReference.to("twentythree"); AggregateReference<Object, String> hobby = AggregateReference.to("twentythree");
ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType); ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType);
assertSoftly(softly -> { assertSoftly(softly -> {
@ -127,12 +127,12 @@ public class PartTreeJdbcQueryUnitTests {
}); });
} }
@Test // #922 @Test // GH-922
public void createQueryForQueryByAggregateReferenceId() throws Exception { public void createQueryForQueryByAggregateReferenceId() throws Exception {
JdbcQueryMethod queryMethod = getQueryMethod("findViaIdByHobbyReference", String.class); JdbcQueryMethod queryMethod = getQueryMethod("findViaIdByHobbyReference", String.class);
PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod); PartTreeJdbcQuery jdbcQuery = createQuery(queryMethod);
final String hobby = "twentythree"; String hobby = "twentythree";
ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType); ParametrizedQuery query = jdbcQuery.createQuery(getAccessor(queryMethod, new Object[] {hobby}), returnedType);
assertSoftly(softly -> { assertSoftly(softly -> {

15
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/BasicRelationalConverter.java

@ -16,10 +16,13 @@
package org.springframework.data.relational.core.conversion; package org.springframework.data.relational.core.conversion;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.convert.CustomConversions; import org.springframework.data.convert.CustomConversions;
@ -160,7 +163,7 @@ public class BasicRelationalConverter implements RelationalConverter {
if (getConversions().hasCustomReadTarget(value.getClass(), type.getType())) { if (getConversions().hasCustomReadTarget(value.getClass(), type.getType())) {
TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(value.getClass()); TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(value.getClass());
TypeDescriptor targetDescriptor = typeInformationToTypeDescriptor(type); TypeDescriptor targetDescriptor = createTypeDescriptor(type);
return getConversionService().convert(value, sourceDescriptor, targetDescriptor); return getConversionService().convert(value, sourceDescriptor, targetDescriptor);
} }
@ -250,11 +253,15 @@ public class BasicRelationalConverter implements RelationalConverter {
return conversionService.convert(value, target); return conversionService.convert(value, target);
} }
protected static TypeDescriptor typeInformationToTypeDescriptor(TypeInformation<?> type) { protected static TypeDescriptor createTypeDescriptor(TypeInformation<?> type) {
Class<?>[] generics = type.getTypeArguments().stream().map(TypeInformation::getType).toArray(Class[]::new); List<TypeInformation<?>> typeArguments = type.getTypeArguments();
Class<?>[] generics = new Class[typeArguments.size()];
for (int i = 0; i < typeArguments.size(); i++) {
generics[i] = typeArguments.get(i).getType();
}
return new TypeDescriptor(ResolvableType.forClassWithGenerics(type.getType(), generics), null, null); return new TypeDescriptor(ResolvableType.forClassWithGenerics(type.getType(), generics), type.getType(), null);
} }
/** /**

Loading…
Cancel
Save