DATAJDBC-464 - Polishing.
Fix generics. Reformat code. Improve method names. Original pull request: #186.
This commit is contained in:
@@ -26,7 +26,7 @@ import org.springframework.data.jdbc.core.convert.JdbcConverter;
|
||||
import org.springframework.data.jdbc.core.convert.JdbcValue;
|
||||
import org.springframework.data.jdbc.support.JdbcUtil;
|
||||
import org.springframework.data.mapping.callback.EntityCallbacks;
|
||||
import org.springframework.data.relational.core.mapping.JdbcCompatibleTypes;
|
||||
import org.springframework.data.relational.core.mapping.JdbcColumnTypes;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.core.mapping.event.AfterLoadCallback;
|
||||
@@ -93,7 +93,7 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
this.queryMethod = queryMethod;
|
||||
this.operations = operations;
|
||||
|
||||
RowMapper rowMapper = determineRowMapper(defaultRowMapper);
|
||||
RowMapper<Object> rowMapper = determineRowMapper(defaultRowMapper);
|
||||
executor = createExecutor( //
|
||||
queryMethod, //
|
||||
determineResultSetExtractor(rowMapper != defaultRowMapper ? rowMapper : null), //
|
||||
@@ -103,8 +103,8 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
private QueryExecutor<Object> createExecutor(JdbcQueryMethod queryMethod, @Nullable ResultSetExtractor extractor,
|
||||
RowMapper rowMapper) {
|
||||
private QueryExecutor<Object> createExecutor(JdbcQueryMethod queryMethod,
|
||||
@Nullable ResultSetExtractor<Object> extractor, RowMapper<Object> rowMapper) {
|
||||
|
||||
String query = determineQuery();
|
||||
|
||||
@@ -132,15 +132,13 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
return executor.execute(bindParameters(objects));
|
||||
}
|
||||
|
||||
private QueryExecutor<Object> createObjectQueryExecutor(QueryExecutor executor) {
|
||||
private QueryExecutor<Object> createObjectQueryExecutor(QueryExecutor<Object> executor) {
|
||||
|
||||
return parameters -> {
|
||||
|
||||
try {
|
||||
|
||||
Object result;
|
||||
|
||||
result = executor.execute(parameters);
|
||||
Object result = executor.execute(parameters);
|
||||
|
||||
publishAfterLoad(result);
|
||||
|
||||
@@ -228,7 +226,7 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
String parameterName = p.getName().orElseThrow(() -> new IllegalStateException(PARAMETER_NEEDS_TO_BE_NAMED));
|
||||
|
||||
Class<?> parameterType = queryMethod.getParameters().getParameter(p.getIndex()).getType();
|
||||
Class conversionTargetType = JdbcCompatibleTypes.INSTANCE.columnTypeForNonEntity(parameterType);
|
||||
Class<?> conversionTargetType = JdbcColumnTypes.INSTANCE.resolvePrimitiveType(parameterType);
|
||||
|
||||
JdbcValue jdbcValue = converter.writeJdbcValue(value, conversionTargetType,
|
||||
JdbcUtil.sqlTypeFor(conversionTargetType));
|
||||
@@ -237,16 +235,16 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
if (jdbcType == null) {
|
||||
|
||||
parameters.addValue(parameterName, jdbcValue.getValue());
|
||||
}else {
|
||||
} else {
|
||||
parameters.addValue(parameterName, jdbcValue.getValue(), jdbcType.getVendorTypeNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ResultSetExtractor determineResultSetExtractor(@Nullable RowMapper<?> rowMapper) {
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private ResultSetExtractor<Object> determineResultSetExtractor(@Nullable RowMapper<Object> rowMapper) {
|
||||
|
||||
Class<? extends ResultSetExtractor> resultSetExtractorClass = (Class<? extends ResultSetExtractor>) queryMethod
|
||||
.getResultSetExtractorClass();
|
||||
Class<? extends ResultSetExtractor> resultSetExtractorClass = queryMethod.getResultSetExtractorClass();
|
||||
|
||||
if (isUnconfigured(resultSetExtractorClass, ResultSetExtractor.class)) {
|
||||
return null;
|
||||
@@ -262,15 +260,16 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
return BeanUtils.instantiateClass(resultSetExtractorClass);
|
||||
}
|
||||
|
||||
private RowMapper determineRowMapper(RowMapper<?> defaultMapper) {
|
||||
@SuppressWarnings("unchecked")
|
||||
private RowMapper<Object> determineRowMapper(RowMapper<?> defaultMapper) {
|
||||
|
||||
Class<?> rowMapperClass = queryMethod.getRowMapperClass();
|
||||
|
||||
if (isUnconfigured(rowMapperClass, RowMapper.class)) {
|
||||
return defaultMapper;
|
||||
return (RowMapper<Object>) defaultMapper;
|
||||
}
|
||||
|
||||
return (RowMapper) BeanUtils.instantiateClass(rowMapperClass);
|
||||
return (RowMapper<Object>) BeanUtils.instantiateClass(rowMapperClass);
|
||||
}
|
||||
|
||||
private static boolean isUnconfigured(@Nullable Class<?> configuredClass, Class<?> defaultClass) {
|
||||
@@ -297,7 +296,6 @@ class JdbcRepositoryQuery implements RepositoryQuery {
|
||||
|
||||
callbacks.callback(AfterLoadCallback.class, entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private interface QueryExecutor<T> {
|
||||
|
||||
@@ -16,11 +16,6 @@
|
||||
package org.springframework.data.relational.core.mapping;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -35,7 +30,6 @@ import org.springframework.data.util.Lazy;
|
||||
import org.springframework.data.util.Optionals;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -161,16 +155,18 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
|
||||
private Class<?> doGetColumnType() {
|
||||
|
||||
if (isReference()) {
|
||||
return columnTypeForReference();
|
||||
return getReferenceColumnType();
|
||||
}
|
||||
|
||||
Class columnType = columnTypeIfEntity(getActualType());
|
||||
if (isEntity()) {
|
||||
Class<?> columnType = getEntityColumnType(getActualType());
|
||||
|
||||
if (columnType != null) {
|
||||
return columnType;
|
||||
if (columnType != null) {
|
||||
return columnType;
|
||||
}
|
||||
}
|
||||
|
||||
Class componentColumnType = JdbcCompatibleTypes.INSTANCE.columnTypeForNonEntity(getActualType());
|
||||
Class<?> componentColumnType = JdbcColumnTypes.INSTANCE.resolvePrimitiveType(getActualType());
|
||||
|
||||
while (componentColumnType.isArray()) {
|
||||
componentColumnType = componentColumnType.getComponentType();
|
||||
@@ -222,7 +218,7 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
|
||||
Assert.isTrue(isQualified(), "The qualifier column type is only defined for properties that are qualified");
|
||||
|
||||
if (isMap()) {
|
||||
return getTypeInformation().getComponentType().getType();
|
||||
return getTypeInformation().getRequiredComponentType().getType();
|
||||
}
|
||||
|
||||
// for lists and arrays
|
||||
@@ -261,7 +257,7 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Class columnTypeIfEntity(Class type) {
|
||||
private Class<?> getEntityColumnType(Class<?> type) {
|
||||
|
||||
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(type);
|
||||
|
||||
@@ -277,7 +273,7 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
|
||||
return idProperty.getColumnType();
|
||||
}
|
||||
|
||||
private Class columnTypeForReference() {
|
||||
private Class<?> getReferenceColumnType() {
|
||||
|
||||
Class<?> componentType = getTypeInformation().getRequiredComponentType().getType();
|
||||
RelationalPersistentEntity<?> referencedEntity = context.getRequiredPersistentEntity(componentType);
|
||||
|
||||
@@ -26,32 +26,33 @@ import org.springframework.util.ClassUtils;
|
||||
/**
|
||||
* Utility that determines the necessary type conversions between Java types used in the domain model and types
|
||||
* compatible with JDBC drivers.
|
||||
*
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
public enum JdbcCompatibleTypes {
|
||||
public enum JdbcColumnTypes {
|
||||
|
||||
INSTANCE {
|
||||
|
||||
private final Map<Class<?>, Class<?>> javaToDbType = new LinkedHashMap<>();
|
||||
|
||||
{
|
||||
|
||||
javaToDbType.put(Enum.class, String.class);
|
||||
javaToDbType.put(ZonedDateTime.class, String.class);
|
||||
javaToDbType.put(Temporal.class, Date.class);
|
||||
}
|
||||
|
||||
public Class columnTypeForNonEntity(Class type) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public Class<?> resolvePrimitiveType(Class<?> type) {
|
||||
|
||||
return javaToDbType.entrySet().stream() //
|
||||
.filter(e -> e.getKey().isAssignableFrom(type)) //
|
||||
.map(e -> (Class) e.getValue()) //
|
||||
.map(e -> (Class<?>) e.getValue()) //
|
||||
.findFirst() //
|
||||
.orElseGet(() -> ClassUtils.resolvePrimitiveIfNecessary(type));
|
||||
.orElseGet(() -> (Class) ClassUtils.resolvePrimitiveIfNecessary(type));
|
||||
}
|
||||
};
|
||||
|
||||
public abstract Class columnTypeForNonEntity(Class type);
|
||||
private static final Map<Class<?>, Class<?>> javaToDbType = new LinkedHashMap<>();
|
||||
|
||||
static {
|
||||
|
||||
javaToDbType.put(Enum.class, String.class);
|
||||
javaToDbType.put(ZonedDateTime.class, String.class);
|
||||
javaToDbType.put(Temporal.class, Date.class);
|
||||
}
|
||||
|
||||
public abstract Class<?> resolvePrimitiveType(Class<?> type);
|
||||
}
|
||||
Reference in New Issue
Block a user