Skip to content

Commit 9091987

Browse files
committed
Vastly refactored property --> jdbc value mapping api
1 parent b3040a1 commit 9091987

File tree

10 files changed

+69
-42
lines changed

10 files changed

+69
-42
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.sql.Array;
1919
import java.sql.SQLType;
2020

21+
import org.springframework.data.jdbc.support.JdbcUtil;
2122
import org.springframework.jdbc.core.ConnectionCallback;
2223
import org.springframework.jdbc.core.JdbcOperations;
2324
import org.springframework.util.Assert;
@@ -28,6 +29,8 @@
2829
*
2930
* @author Jens Schauder
3031
* @author Mark Paluch
32+
* @author Mikhail Polivakha
33+
*
3134
* @since 1.1
3235
*/
3336
public class DefaultJdbcTypeFactory implements JdbcTypeFactory {
@@ -68,6 +71,13 @@ public Array createArray(Object[] value) {
6871
SQLType jdbcType = arrayColumns.getSqlType(componentType);
6972

7073
Assert.notNull(jdbcType, () -> String.format("Couldn't determine SQLType for %s", componentType));
74+
return this.createArray(value, componentType);
75+
}
76+
77+
@Override
78+
public Array createArray(Object[] value, Class<?> componentsGenericType) {
79+
SQLType jdbcType = JdbcUtil.targetSqlTypeFor(componentsGenericType);
80+
Assert.notNull(jdbcType, () -> String.format("Couldn't determine JDBCType for %s", componentsGenericType));
7181
String typeName = arrayColumns.getArrayTypeName(jdbcType);
7282

7383
return operations.execute((ConnectionCallback<Array>) c -> c.createArrayOf(typeName, value));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2424
import org.springframework.data.relational.domain.RowDocument;
2525
import org.springframework.data.util.TypeInformation;
26+
import org.springframework.lang.NonNull;
2627
import org.springframework.lang.Nullable;
2728

2829
/**
2930
* A {@link JdbcConverter} is responsible for converting for values to the native relational representation and vice
3031
* versa.
3132
*
3233
* @author Jens Schauder
33-
* @author Mark Paluch
34+
* @author Mikhail Polivakha
35+
*
3436
* @since 1.1
3537
*/
3638
public interface JdbcConverter extends RelationalConverter {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Allows the creation of instances of database dependent types, e.g. {@link Array}.
2222
*
2323
* @author Jens Schauder
24+
* @author Mikhail Polivakha
25+
*
2426
* @since 1.1
2527
*/
2628
public interface JdbcTypeFactory {
@@ -32,8 +34,16 @@ public interface JdbcTypeFactory {
3234
*/
3335
static JdbcTypeFactory unsupported() {
3436

35-
return value -> {
36-
throw new UnsupportedOperationException("This JdbcTypeFactory does not support Array creation");
37+
return new JdbcTypeFactory() {
38+
@Override
39+
public Array createArray(Object[] value) {
40+
throw new UnsupportedOperationException("This JdbcTypeFactory does not support Array creation");
41+
}
42+
43+
@Override
44+
public Array createArray(Object[] value, Class<?> componentsGenericType) {
45+
throw new UnsupportedOperationException("This JdbcTypeFactory does not support Array creation");
46+
}
3747
};
3848
}
3949

@@ -44,4 +54,6 @@ static JdbcTypeFactory unsupported() {
4454
* @return an {@link Array}. Guaranteed to be not {@literal null}.
4555
*/
4656
Array createArray(Object[] value);
57+
58+
Array createArray(Object[] value, Class<?> componentsGenericType);
4759
}

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.function.Predicate;
2323

2424
import org.springframework.data.jdbc.core.mapping.JdbcValue;
25-
import org.springframework.data.jdbc.support.JdbcUtil;
2625
import org.springframework.data.mapping.PersistentProperty;
2726
import org.springframework.data.mapping.PersistentPropertyAccessor;
2827
import org.springframework.data.relational.core.conversion.IdValueSource;
@@ -77,7 +76,6 @@ <T> SqlIdentifierParameterSource forInsert(T instance, Class<T> domainType, Iden
7776
identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));
7877

7978
if (IdValueSource.PROVIDED.equals(idValueSource)) {
80-
8179
RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
8280
Object idValue = persistentEntity.getIdentifierAccessor(instance).getRequiredIdentifier();
8381
addConvertedPropertyValue(parameterSource, idProperty, idValue, idProperty.getColumnName());
@@ -156,41 +154,25 @@ SqlIdentifierParameterSource forQueryByIdentifier(Identifier identifier) {
156154
return parameterSource;
157155
}
158156

159-
/**
160-
* Utility to create {@link Predicate}s.
161-
*/
162-
static class Predicates {
163-
164-
/**
165-
* Include all {@link Predicate} returning {@literal false} to never skip a property.
166-
*
167-
* @return the include all {@link Predicate}.
168-
*/
169-
static Predicate<RelationalPersistentProperty> includeAll() {
170-
return it -> false;
171-
}
172-
}
173-
174157
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource,
175158
RelationalPersistentProperty property, @Nullable Object value, SqlIdentifier name) {
176159

177-
addConvertedValue(parameterSource, value, name, converter.getColumnType(property),
178-
converter.getTargetSqlType(property));
160+
addConvertedValue(parameterSource, value, name, converter.getColumnType(property));
179161
}
180162

181163
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource, SqlIdentifier name, Object value,
182164
Class<?> javaType) {
183165

184-
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.targetSqlTypeFor(javaType));
166+
addConvertedValue(parameterSource, value, name, javaType);
185167
}
186168

187169
private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nullable Object value,
188-
SqlIdentifier paramName, Class<?> javaType, SQLType sqlType) {
170+
SqlIdentifier paramName, Class<?> javaType) {
189171

190-
JdbcValue jdbcValue = converter.writeJdbcValue( //
191-
value, //
192-
javaType, //
193-
sqlType //
172+
JdbcValue jdbcValue = converter.createJdbcValue(
173+
value,
174+
javaType.isArray() ? javaType.getComponentType() : null,
175+
javaType
194176
);
195177

196178
parameterSource.addValue( //
@@ -232,14 +214,16 @@ private <S, T> SqlIdentifierParameterSource getParameterSource(@Nullable S insta
232214

233215
SqlIdentifierParameterSource parameters = new SqlIdentifierParameterSource();
234216

235-
PersistentPropertyAccessor<S> propertyAccessor = instance != null ? persistentEntity.getPropertyAccessor(instance)
217+
PersistentPropertyAccessor<S> propertyAccessor = instance != null
218+
? persistentEntity.getPropertyAccessor(instance)
236219
: NoValuePropertyAccessor.instance();
237220

238221
persistentEntity.doWithAll(property -> {
239222

240223
if (skipProperty.test(property) || !property.isWritable()) {
241224
return;
242225
}
226+
243227
if (property.isEntity() && !property.isEmbedded()) {
244228
return;
245229
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/JdbcUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ public static SQLType targetSqlTypeFor(Class<?> type) {
106106
.map(sqlTypeMappings::get) //
107107
.orElse(JdbcUtil.TYPE_UNKNOWN);
108108
}
109-
}
109+
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverterUnitTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
*
5656
* @author Mark Paluch
5757
* @author Jens Schauder
58+
* @author Mikhail Polivakha
5859
*/
5960
class MappingJdbcConverterUnitTests {
6061

@@ -345,6 +346,11 @@ public Array createArray(Object[] value) {
345346
arraySource = value;
346347
return mock(Array.class);
347348
}
349+
350+
@Override
351+
public Array createArray(Object[] value, Class<?> componentsGenericType) {
352+
return this.createArray(value);
353+
}
348354
}
349355

350356
private record WithOneToOne(@Id String id, @MappedCollection(idColumn = "renamed") Referenced referenced) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package org.springframework.data.jdbc.repository;
1717

18-
import static java.util.Arrays.*;
19-
import static org.assertj.core.api.Assertions.*;
20-
import static org.assertj.core.api.SoftAssertions.*;
18+
import static java.util.Arrays.asList;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
2121

2222
import java.math.BigDecimal;
2323
import java.sql.JDBCType;
@@ -48,6 +48,7 @@
4848
* @author Jens Schauder
4949
* @author Sanghyuk Jung
5050
* @author Chirag Tailor
51+
* @author Mikhail Polivakha
5152
*/
5253
@IntegrationTest
5354
public class JdbcRepositoryCustomConversionIntegrationTests {
@@ -253,12 +254,11 @@ enum DirectionToIntegerConverter implements Converter<Direction, JdbcValue> {
253254
@Override
254255
public JdbcValue convert(Direction source) {
255256

256-
int integer = switch (source) {
257-
case LEFT -> -1;
258-
case CENTER -> 0;
259-
case RIGHT -> 1;
257+
return switch (source) {
258+
case LEFT -> JdbcValue.of(-1, JDBCType.INTEGER);
259+
case CENTER -> JdbcValue.of(0, JDBCType.INTEGER);
260+
case RIGHT -> JdbcValue.of(1, JDBCType.INTEGER);
260261
};
261-
return JdbcValue.of(integer, JDBCType.INTEGER);
262262
}
263263
}
264264

Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
CREATE TABLE aggregate_one ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100), two INTEGER);
1+
CREATE TABLE aggregate_one (
2+
id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY,
3+
NAME VARCHAR(100),
4+
two INTEGER
5+
);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL ( id SERIAL PRIMARY KEY, Stringy_number DECIMAL(20,10), DIRECTION INTEGER);
2-
CREATE TABLE OTHER_ENTITY ( ID SERIAL PRIMARY KEY, CREATED DATE, ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER);
1+
CREATE TABLE ENTITY_WITH_STRINGY_BIG_DECIMAL (
2+
id SERIAL PRIMARY KEY,
3+
Stringy_number DECIMAL(20,10),
4+
DIRECTION INTEGER
5+
);
6+
7+
CREATE TABLE OTHER_ENTITY (
8+
ID SERIAL PRIMARY KEY,
9+
CREATED DATE,
10+
ENTITY_WITH_STRINGY_BIG_DECIMAL INTEGER
11+
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private Class<?> getPotentiallyConvertedSimpleNullType(Class<?> type) {
328328
* @return
329329
*/
330330
@Nullable
331-
private Object getPotentiallyConvertedSimpleWrite(@Nullable Object value) {
331+
protected Object getPotentiallyConvertedSimpleWrite(@Nullable Object value) {
332332
return getPotentiallyConvertedSimpleWrite(value, Object.class);
333333
}
334334

0 commit comments

Comments
 (0)