Skip to content

DATAJDBC-327 - Adds support for conversion to JdbcTypeAware and storing byte[] as binary. #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-327-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-327-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-327-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-327-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,35 @@
package org.springframework.data.jdbc.core;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.sql.Connection;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import java.util.function.Predicate;

import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
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.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -55,27 +57,32 @@
* @author Thomas Lang
* @author Bastian Wilhelm
*/
@RequiredArgsConstructor
public class DefaultDataAccessStrategy implements DataAccessStrategy {

private final @NonNull SqlGeneratorSource sqlGeneratorSource;
private final @NonNull RelationalMappingContext context;
private final @NonNull RelationalConverter converter;
private final @NonNull JdbcConverter converter;
private final @NonNull NamedParameterJdbcOperations operations;
private final @NonNull DataAccessStrategy accessStrategy;

public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
JdbcConverter converter, NamedParameterJdbcOperations operations, @Nullable DataAccessStrategy accessStrategy) {

this.sqlGeneratorSource = sqlGeneratorSource;
this.context = context;
this.converter = converter;
this.operations = operations;
this.accessStrategy = accessStrategy == null ? this : accessStrategy;
}

/**
* Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
* Only suitable if this is the only access strategy in use.
*/
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
RelationalConverter converter, NamedParameterJdbcOperations operations) {
JdbcConverter converter, NamedParameterJdbcOperations operations) {

this.sqlGeneratorSource = sqlGeneratorSource;
this.operations = operations;
this.context = context;
this.converter = converter;
this.accessStrategy = this;
this(sqlGeneratorSource, context, converter, operations, null);
}

/*
Expand All @@ -97,28 +104,19 @@ public <T> Object insert(T instance, Class<T> domainType, Identifier identifier)
KeyHolder holder = new GeneratedKeyHolder();
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);

Map<String, Object> parameters = new LinkedHashMap<>(identifier.size());
identifier.forEach((name, value, type) -> {
parameters.put(name, converter.writeValue(value, ClassTypeInformation.from(type)));
});
MapSqlParameterSource parameterSource = getParameterSource(instance, persistentEntity, "", PersistentProperty::isIdProperty);

MapSqlParameterSource parameterSource = getPropertyMap(instance, persistentEntity, "");
identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));

Object idValue = getIdValueOrNull(instance, persistentEntity);
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();

if (idValue != null) {

Assert.notNull(idProperty, "Since we have a non-null idValue, we must have an idProperty as well.");

parameters.put(idProperty.getColumnName(),
converter.writeValue(idValue, ClassTypeInformation.from(idProperty.getColumnType())));
RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
addConvertedPropertyValue(parameterSource, idProperty, idValue, idProperty.getColumnName());
}

parameters.forEach(parameterSource::addValue);

operations.update( //
sql(domainType).getInsert(parameters.keySet()), //
sql(domainType).getInsert(new HashSet<>(Arrays.asList(parameterSource.getParameterNames()))), //
parameterSource, //
holder //
);
Expand All @@ -135,7 +133,8 @@ public <S> boolean update(S instance, Class<S> domainType) {

RelationalPersistentEntity<S> persistentEntity = getRequiredPersistentEntity(domainType);

return operations.update(sql(domainType).getUpdate(), getPropertyMap(instance, persistentEntity, "")) != 0;
return operations.update(sql(domainType).getUpdate(),
getParameterSource(instance, persistentEntity, "", property -> false)) != 0;
}

/*
Expand Down Expand Up @@ -240,17 +239,14 @@ public <T> Iterable<T> findAll(Class<T> domainType) {
@Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {

String findAllInListSql = sql(domainType).getFindAllInList();
Class<?> targetType = getRequiredPersistentEntity(domainType).getRequiredIdProperty().getColumnType();
RelationalPersistentProperty idProperty = getRequiredPersistentEntity(domainType).getRequiredIdProperty();
MapSqlParameterSource parameterSource = new MapSqlParameterSource();

MapSqlParameterSource parameter = new MapSqlParameterSource( //
"ids", //
StreamSupport.stream(ids.spliterator(), false) //
.map(id -> converter.writeValue(id, ClassTypeInformation.from(targetType))) //
.collect(Collectors.toList()) //
);
addConvertedPropertyValuesAsList(parameterSource, idProperty, ids, "ids");

String findAllInListSql = sql(domainType).getFindAllInList();

return operations.query(findAllInListSql, parameter, (RowMapper<T>) getEntityRowMapper(domainType));
return operations.query(findAllInListSql, parameterSource, (RowMapper<T>) getEntityRowMapper(domainType));
}

/*
Expand Down Expand Up @@ -292,15 +288,18 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return result;
}

private <S, T> MapSqlParameterSource getPropertyMap(S instance, RelationalPersistentEntity<S> persistentEntity,
String prefix) {
private <S, T> MapSqlParameterSource getParameterSource(S instance, RelationalPersistentEntity<S> persistentEntity,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: How about Predicate< RelationalPersistentProperty> instead of skipId?

String prefix, Predicate<RelationalPersistentProperty> skipProperty) {

MapSqlParameterSource parameters = new MapSqlParameterSource();

PersistentPropertyAccessor<S> propertyAccessor = persistentEntity.getPropertyAccessor(instance);

persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {

if (skipProperty.test(property)) {
return;
}
if (property.isEntity() && !property.isEmbedded()) {
return;
}
Expand All @@ -309,42 +308,21 @@ private <S, T> MapSqlParameterSource getPropertyMap(S instance, RelationalPersis

Object value = propertyAccessor.getProperty(property);
RelationalPersistentEntity<?> embeddedEntity = context.getPersistentEntity(property.getType());
MapSqlParameterSource additionalParameters = getPropertyMap((T) value,
(RelationalPersistentEntity<T>) embeddedEntity, prefix + property.getEmbeddedPrefix());
MapSqlParameterSource additionalParameters = getParameterSource((T) value,
(RelationalPersistentEntity<T>) embeddedEntity, prefix + property.getEmbeddedPrefix(), skipProperty);
parameters.addValues(additionalParameters.getValues());
} else {

Object value = propertyAccessor.getProperty(property);
Object convertedValue = convertForWrite(property, value);
String paramName = prefix + property.getColumnName();

parameters.addValue(prefix + property.getColumnName(), convertedValue,
JdbcUtil.sqlTypeFor(property.getColumnType()));
addConvertedPropertyValue(parameters, property, value, paramName);
}
});

return parameters;
}

@Nullable
private Object convertForWrite(RelationalPersistentProperty property, @Nullable Object value) {

Object convertedValue = converter.writeValue(value, ClassTypeInformation.from(property.getColumnType()));

if (convertedValue == null || !convertedValue.getClass().isArray()) {
return convertedValue;
}

Class<?> componentType = convertedValue.getClass();
while (componentType.isArray()) {
componentType = componentType.getComponentType();
}

String typeName = JDBCType.valueOf(JdbcUtil.sqlTypeFor(componentType)).getName();

return operations.getJdbcOperations()
.execute((Connection c) -> c.createArrayOf(typeName, (Object[]) convertedValue));
}

@SuppressWarnings("unchecked")
@Nullable
private <S, ID> ID getIdValueOrNull(S instance, RelationalPersistentEntity<S> persistentEntity) {
Expand Down Expand Up @@ -398,8 +376,65 @@ private RowMapper<?> getMapEntityRowMapper(RelationalPersistentProperty property

private <T> MapSqlParameterSource createIdParameterSource(Object id, Class<T> domainType) {

Class<?> columnType = getRequiredPersistentEntity(domainType).getRequiredIdProperty().getColumnType();
return new MapSqlParameterSource("id", converter.writeValue(id, ClassTypeInformation.from(columnType)));
MapSqlParameterSource parameterSource = new MapSqlParameterSource();

addConvertedPropertyValue( //
parameterSource, //
getRequiredPersistentEntity(domainType).getRequiredIdProperty(), //
id, //
"id" //
);
return parameterSource;
}

private void addConvertedPropertyValue(MapSqlParameterSource parameterSource, RelationalPersistentProperty property,
Object value, String paramName) {

JdbcValue jdbcValue = converter.writeJdbcValue( //
value, //
property.getColumnType(), //
property.getSqlType() //
);

parameterSource.addValue(paramName, jdbcValue.getValue(), JdbcUtil.sqlTypeFor(jdbcValue.getJdbcType()));
}

private void addConvertedPropertyValue(MapSqlParameterSource parameterSource, String name, Object value,
Class<?> type) {

JdbcValue jdbcValue = converter.writeJdbcValue( //
value, //
type, //
JdbcUtil.sqlTypeFor(type) //
);

parameterSource.addValue( //
name, //
jdbcValue.getValue(), //
JdbcUtil.sqlTypeFor(jdbcValue.getJdbcType()) //
);
}

private void addConvertedPropertyValuesAsList(MapSqlParameterSource parameterSource,
RelationalPersistentProperty property, Iterable<?> values, String paramName) {

List<Object> convertedIds = new ArrayList<>();
JdbcValue jdbcValue = null;
for (Object id : values) {

Class<?> columnType = property.getColumnType();
int sqlType = property.getSqlType();

jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
convertedIds.add(jdbcValue.getValue());
}

Assert.notNull(jdbcValue, "JdbcValue must be not null at this point. Please report this as a bug.");

JDBCType jdbcType = jdbcValue.getJdbcType();
int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();

parameterSource.addValue(paramName, convertedIds, typeNumber);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.convert;

import lombok.experimental.UtilityClass;

/**
* A collection of utility methods for dealing with arrays.
*
* @author Jens Schauder
*/
@UtilityClass
class ArrayUtil {

/**
* Convertes an {@code Byte[]} into a {@code byte[]}
* @param byteArray the array to be converted. Must not be {@literal null}.
*
* @return a {@code byte[]} of same size with the unboxed values of the input array. Guaranteed to be not {@literal null}.
*/
static Object toPrimitiveByteArray(Byte[] byteArray) {

byte[] bytes = new byte[byteArray.length];
for (int i = 0; i < byteArray.length; i++) {
bytes[i] = byteArray[i];
}
return bytes;
}
}
Loading