Skip to content

Commit 78f26aa

Browse files
committed
DATAJDBC-327 - Polishing.
Extend Javadoc. Reorder constructors in the order of their parameter number. Add missing assertions. Introduce utility to create predicates. Remove unused fields. Add since tags. Reformat. Original pull request: #123.
1 parent 35d9bb6 commit 78f26aa

13 files changed

+155
-81
lines changed

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

+50-14
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,46 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
6565
private final @NonNull NamedParameterJdbcOperations operations;
6666
private final @NonNull DataAccessStrategy accessStrategy;
6767

68-
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
69-
JdbcConverter converter, NamedParameterJdbcOperations operations, @Nullable DataAccessStrategy accessStrategy) {
70-
71-
this.sqlGeneratorSource = sqlGeneratorSource;
72-
this.context = context;
73-
this.converter = converter;
74-
this.operations = operations;
75-
this.accessStrategy = accessStrategy == null ? this : accessStrategy;
76-
}
77-
7868
/**
7969
* Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
8070
* Only suitable if this is the only access strategy in use.
71+
*
72+
* @param sqlGeneratorSource must not be {@literal null}.
73+
* @param context must not be {@literal null}.
74+
* @param converter must not be {@literal null}.
75+
* @param operations must not be {@literal null}.
8176
*/
8277
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
8378
JdbcConverter converter, NamedParameterJdbcOperations operations) {
84-
8579
this(sqlGeneratorSource, context, converter, operations, null);
8680
}
8781

82+
/**
83+
* Creates a {@link DefaultDataAccessStrategy}
84+
*
85+
* @param sqlGeneratorSource must not be {@literal null}.
86+
* @param context must not be {@literal null}.
87+
* @param converter must not be {@literal null}.
88+
* @param operations must not be {@literal null}.
89+
* @param mappingAccessStrategy can be {@literal null}.
90+
* @since 1.1
91+
*/
92+
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
93+
JdbcConverter converter, NamedParameterJdbcOperations operations,
94+
@Nullable DataAccessStrategy mappingAccessStrategy) {
95+
96+
Assert.notNull(sqlGeneratorSource, "SqlGeneratorSource must not be null");
97+
Assert.notNull(context, "RelationalMappingContext must not be null");
98+
Assert.notNull(converter, "JdbcConverter must not be null");
99+
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
100+
101+
this.sqlGeneratorSource = sqlGeneratorSource;
102+
this.context = context;
103+
this.converter = converter;
104+
this.operations = operations;
105+
this.accessStrategy = mappingAccessStrategy == null ? this : mappingAccessStrategy;
106+
}
107+
88108
/*
89109
* (non-Javadoc)
90110
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
@@ -104,7 +124,8 @@ public <T> Object insert(T instance, Class<T> domainType, Identifier identifier)
104124
KeyHolder holder = new GeneratedKeyHolder();
105125
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
106126

107-
MapSqlParameterSource parameterSource = getParameterSource(instance, persistentEntity, "", PersistentProperty::isIdProperty);
127+
MapSqlParameterSource parameterSource = getParameterSource(instance, persistentEntity, "",
128+
PersistentProperty::isIdProperty);
108129

109130
identifier.forEach((name, value, type) -> addConvertedPropertyValue(parameterSource, name, value, type));
110131

@@ -134,7 +155,7 @@ public <S> boolean update(S instance, Class<S> domainType) {
134155
RelationalPersistentEntity<S> persistentEntity = getRequiredPersistentEntity(domainType);
135156

136157
return operations.update(sql(domainType).getUpdate(),
137-
getParameterSource(instance, persistentEntity, "", property -> false)) != 0;
158+
getParameterSource(instance, persistentEntity, "", Predicates.includeAll())) != 0;
138159
}
139160

140161
/*
@@ -289,7 +310,7 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
289310
}
290311

291312
private <S, T> MapSqlParameterSource getParameterSource(S instance, RelationalPersistentEntity<S> persistentEntity,
292-
String prefix, Predicate<RelationalPersistentProperty> skipProperty) {
313+
String prefix, Predicate<RelationalPersistentProperty> skipProperty) {
293314

294315
MapSqlParameterSource parameters = new MapSqlParameterSource();
295316

@@ -445,4 +466,19 @@ private <S> RelationalPersistentEntity<S> getRequiredPersistentEntity(Class<S> d
445466
private SqlGenerator sql(Class<?> domainType) {
446467
return sqlGeneratorSource.getSqlGenerator(domainType);
447468
}
469+
470+
/**
471+
* Utility to create {@link Predicate}s.
472+
*/
473+
static class Predicates {
474+
475+
/**
476+
* Include all {@link Predicate} returning {@literal false} to never skip a property.
477+
*
478+
* @return the include all {@link Predicate}.
479+
*/
480+
static Predicate<RelationalPersistentProperty> includeAll() {
481+
return it -> false;
482+
}
483+
}
448484
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
* A collection of utility methods for dealing with arrays.
2222
*
2323
* @author Jens Schauder
24+
* @since 1.1
2425
*/
2526
@UtilityClass
2627
class ArrayUtil {
2728

2829
/**
29-
* Convertes an {@code Byte[]} into a {@code byte[]}
30-
* @param byteArray the array to be converted. Must not be {@literal null}.
30+
* Converts an {@code Byte[]} into a {@code byte[]}.
3131
*
32-
* @return a {@code byte[]} of same size with the unboxed values of the input array. Guaranteed to be not {@literal null}.
32+
* @param byteArray the array to be converted. Must not be {@literal null}.
33+
* @return a {@code byte[]} of same size with the unboxed values of the input array. Guaranteed to be not
34+
* {@literal null}.
3335
*/
34-
static Object toPrimitiveByteArray(Byte[] byteArray) {
36+
static byte[] toPrimitiveByteArray(Byte[] byteArray) {
3537

3638
byte[] bytes = new byte[byteArray.length];
3739
for (int i = 0; i < byteArray.length; i++) {

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

+38-21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
25+
2526
import org.springframework.core.convert.ConverterNotFoundException;
2627
import org.springframework.data.convert.CustomConversions;
2728
import org.springframework.data.jdbc.core.mapping.AggregateReference;
@@ -35,6 +36,7 @@
3536
import org.springframework.data.util.ClassTypeInformation;
3637
import org.springframework.data.util.TypeInformation;
3738
import org.springframework.lang.Nullable;
39+
import org.springframework.util.Assert;
3840

3941
/**
4042
* {@link RelationalConverter} that uses a {@link MappingContext} to apply basic conversion of relational values to
@@ -54,43 +56,54 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc
5456

5557
private final JdbcTypeFactory typeFactory;
5658

59+
/**
60+
* Creates a new {@link BasicRelationalConverter} given {@link MappingContext} and a
61+
* {@link JdbcTypeFactory#unsupported() no-op type factory} throwing {@link UnsupportedOperationException} on type
62+
* creation. Use {@link #BasicJdbcConverter(MappingContext, JdbcTypeFactory)} to convert arrays and large objects into
63+
* JDBC-specific types.
64+
*
65+
* @param context must not be {@literal null}.
66+
*/
67+
public BasicJdbcConverter(
68+
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context) {
69+
this(context, JdbcTypeFactory.unsupported());
70+
}
71+
5772
/**
5873
* Creates a new {@link BasicRelationalConverter} given {@link MappingContext}.
5974
*
60-
* @param context must not be {@literal null}. org.springframework.data.jdbc.core.DefaultDataAccessStrategyUnitTests
61-
* @param typeFactory
75+
* @param context must not be {@literal null}.
76+
* @param typeFactory must not be {@literal null}
77+
* @since 1.1
6278
*/
6379
public BasicJdbcConverter(
6480
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
6581
JdbcTypeFactory typeFactory) {
82+
6683
super(context);
84+
85+
Assert.notNull(typeFactory, "JdbcTypeFactory must not be null");
86+
6787
this.typeFactory = typeFactory;
6888
}
6989

7090
/**
71-
* Creates a new {@link BasicRelationalConverter} given {@link MappingContext} and {@link CustomConversions}.
72-
*
91+
* Creates a new {@link BasicRelationalConverter} given {@link MappingContext}, {@link CustomConversions}, and
92+
* {@link JdbcTypeFactory}.
93+
*
7394
* @param context must not be {@literal null}.
7495
* @param conversions must not be {@literal null}.
75-
* @param typeFactory
96+
* @param typeFactory must not be {@literal null}
97+
* @since 1.1
7698
*/
7799
public BasicJdbcConverter(
78100
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
79101
CustomConversions conversions, JdbcTypeFactory typeFactory) {
102+
80103
super(context, conversions);
81-
this.typeFactory = typeFactory;
82-
}
83104

84-
/**
85-
* Creates a new {@link BasicRelationalConverter} given {@link MappingContext}.
86-
*
87-
* @param context must not be {@literal null}. org.springframework.data.jdbc.core.DefaultDataAccessStrategyUnitTests
88-
* @deprecated use one of the constructors with {@link JdbcTypeFactory} parameter.
89-
*/
90-
@Deprecated
91-
public BasicJdbcConverter(
92-
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context) {
93-
this(context, JdbcTypeFactory.unsupported());
105+
Assert.notNull(typeFactory, "JdbcTypeFactory must not be null");
106+
this.typeFactory = typeFactory;
94107
}
95108

96109
/**
@@ -186,6 +199,11 @@ private boolean canWriteAsJdbcValue(@Nullable Object value) {
186199
return customWriteTarget.isPresent() && customWriteTarget.get().isAssignableFrom(JdbcValue.class);
187200
}
188201

202+
/*
203+
* (non-Javadoc)
204+
* @see org.springframework.data.jdbc.core.convert.JdbcConverter#writeValue(java.lang.Object, java.lang.Class, int)
205+
*/
206+
@Override
189207
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int sqlType) {
190208

191209
JdbcValue jdbcValue = tryToConvertToJdbcValue(value);
@@ -209,16 +227,15 @@ public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int
209227
}
210228

211229
return JdbcValue.of(convertedValue, JDBCType.BINARY);
212-
213230
}
214231

232+
@Nullable
215233
private JdbcValue tryToConvertToJdbcValue(@Nullable Object value) {
216234

217-
JdbcValue jdbcValue = null;
218235
if (canWriteAsJdbcValue(value)) {
219-
jdbcValue = (JdbcValue) writeValue(value, ClassTypeInformation.from(JdbcValue.class));
236+
return (JdbcValue) writeValue(value, ClassTypeInformation.from(JdbcValue.class));
220237
}
221238

222-
return jdbcValue;
239+
return null;
223240
}
224241
}

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import java.sql.Array;
19+
import java.sql.JDBCType;
20+
1821
import org.springframework.data.jdbc.support.JdbcUtil;
1922
import org.springframework.jdbc.core.ConnectionCallback;
2023
import org.springframework.jdbc.core.JdbcOperations;
2124
import org.springframework.util.Assert;
2225

23-
import java.sql.Array;
24-
import java.sql.JDBCType;
25-
2626
/**
27-
* A {@link JdbcTypeFactory} that performs the conversion by utilizing {@link JdbcOperations#execute(ConnectionCallback)}.
27+
* A {@link JdbcTypeFactory} that performs the conversion by utilizing
28+
* {@link JdbcOperations#execute(ConnectionCallback)}.
2829
*
2930
* @author Jens Schauder
3031
* @since 1.1
@@ -33,7 +34,15 @@ public class DefaultJdbcTypeFactory implements JdbcTypeFactory {
3334

3435
private final JdbcOperations operations;
3536

37+
/**
38+
* Creates a new {@link DefaultJdbcTypeFactory}.
39+
*
40+
* @param operations must not be {@literal null}.
41+
*/
3642
public DefaultJdbcTypeFactory(JdbcOperations operations) {
43+
44+
Assert.notNull(operations, "JdbcOperations must not be null");
45+
3746
this.operations = operations;
3847
}
3948

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* JDBC-specific conversion classes.
3+
*/
4+
@NonNullApi
5+
package org.springframework.data.jdbc.core.convert;
6+
7+
import org.springframework.lang.NonNullApi;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/package-info.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Core JDBC implementation.
3+
*/
14
@NonNullApi
25
package org.springframework.data.jdbc.core;
36

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static int sqlTypeFor(Class<?> type) {
8787

8888
/**
8989
* Converts a {@link JDBCType} to an {@code int} value as defined in {@link Types}.
90-
*
90+
*
9191
* @param jdbcType value to be converted. May be {@literal null}.
9292
* @return One of the values defined in {@link Types} or {@link JdbcUtils#TYPE_UNKNOWN}.
9393
*/
@@ -98,7 +98,7 @@ public static int sqlTypeFor(@Nullable JDBCType jdbcType) {
9898
/**
9999
* Converts a value defined in {@link Types} into a {@link JDBCType} instance or {@literal null} if the value is
100100
* {@link JdbcUtils#TYPE_UNKNOWN}
101-
*
101+
*
102102
* @param sqlType One of the values defined in {@link Types} or {@link JdbcUtils#TYPE_UNKNOWN}.
103103
* @return a matching {@link JDBCType} instance or {@literal null}.
104104
*/
@@ -121,7 +121,6 @@ public static JDBCType jdbcTypeFor(int sqlType) {
121121
*/
122122
@Nullable
123123
public static JDBCType jdbcTypeFor(Class<?> type) {
124-
125124
return jdbcTypeFor(sqlTypeFor(type));
126125
}
127126
}

0 commit comments

Comments
 (0)