Skip to content

Commit 905efc8

Browse files
committed
Move remaining methods for retrieving SqlParameterSource from DefaultDataAccessStrategy to SqlParametersFactory
1 parent 06b6761 commit 905efc8

File tree

4 files changed

+110
-194
lines changed

4 files changed

+110
-194
lines changed

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

Lines changed: 9 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;
1919

2020
import java.sql.ResultSet;
21-
import java.sql.SQLType;
22-
import java.util.ArrayList;
2321
import java.util.Collections;
2422
import java.util.List;
2523
import java.util.Optional;
@@ -28,8 +26,6 @@
2826
import org.springframework.dao.OptimisticLockingFailureException;
2927
import org.springframework.data.domain.Pageable;
3028
import org.springframework.data.domain.Sort;
31-
import org.springframework.data.jdbc.core.mapping.JdbcValue;
32-
import org.springframework.data.jdbc.support.JdbcUtil;
3329
import org.springframework.data.mapping.PersistentPropertyPath;
3430
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
3531
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -41,7 +37,6 @@
4137
import org.springframework.jdbc.core.RowMapper;
4238
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4339
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
44-
import org.springframework.jdbc.support.JdbcUtils;
4540
import org.springframework.lang.Nullable;
4641
import org.springframework.util.Assert;
4742

@@ -166,7 +161,7 @@ public <S> boolean updateWithVersion(S instance, Class<S> domainType, Number pre
166161
public void delete(Object id, Class<?> domainType) {
167162

168163
String deleteByIdSql = sql(domainType).getDeleteById();
169-
SqlParameterSource parameter = createIdParameterSource(id, domainType);
164+
SqlParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
170165

171166
operations.update(deleteByIdSql, parameter);
172167
}
@@ -182,7 +177,7 @@ public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previou
182177

183178
RelationalPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
184179

185-
SqlIdentifierParameterSource parameterSource = createIdParameterSource(id, domainType);
180+
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
186181
parameterSource.addValue(VERSION_SQL_PARAMETER, previousVersion);
187182
int affectedRows = operations.update(sql(domainType).getDeleteByIdAndVersion(), parameterSource);
188183

@@ -207,13 +202,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
207202

208203
String delete = sql(rootEntity.getType()).createDeleteByPath(propertyPath);
209204

210-
SqlIdentifierParameterSource parameters = new SqlIdentifierParameterSource(getIdentifierProcessing());
211-
addConvertedPropertyValue( //
212-
parameters, //
213-
rootEntity.getRequiredIdProperty(), //
214-
rootId, //
215-
ROOT_ID_PARAMETER //
216-
);
205+
SqlIdentifierParameterSource parameters = sqlParametersFactory.getId(rootId, rootEntity.getType(), ROOT_ID_PARAMETER);
217206
operations.update(delete, parameters);
218207
}
219208

@@ -244,7 +233,7 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
244233
public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType) {
245234

246235
String acquireLockByIdSql = sql(domainType).getAcquireLockById(lockMode);
247-
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
236+
SqlIdentifierParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
248237

249238
operations.query(acquireLockByIdSql, parameter, ResultSet::next);
250239
}
@@ -283,7 +272,7 @@ public long count(Class<?> domainType) {
283272
public <T> T findById(Object id, Class<T> domainType) {
284273

285274
String findOneSql = sql(domainType).getFindOne();
286-
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
275+
SqlIdentifierParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
287276

288277
try {
289278
return operations.queryForObject(findOneSql, parameter, (RowMapper<T>) getEntityRowMapper(domainType));
@@ -314,10 +303,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
314303
return Collections.emptyList();
315304
}
316305

317-
RelationalPersistentProperty idProperty = getRequiredPersistentEntity(domainType).getRequiredIdProperty();
318-
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(getIdentifierProcessing());
319-
320-
addConvertedPropertyValuesAsList(parameterSource, idProperty, ids, IDS_SQL_PARAMETER);
306+
SqlParameterSource parameterSource = sqlParametersFactory.getIds(ids, domainType);
321307

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

@@ -345,18 +331,8 @@ public Iterable<Object> findAllByPath(Identifier identifier,
345331
RowMapper<?> rowMapper = path.isMap() ? this.getMapEntityRowMapper(path, identifier)
346332
: this.getEntityRowMapper(path, identifier);
347333

348-
return operations.query(findAllByProperty, createParameterSource(identifier, getIdentifierProcessing()),
349-
(RowMapper<Object>) rowMapper);
350-
}
351-
352-
private SqlParameterSource createParameterSource(Identifier identifier, IdentifierProcessing identifierProcessing) {
353-
354-
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(identifierProcessing);
355-
356-
identifier.toMap()
357-
.forEach((name, value) -> addConvertedPropertyValue(parameterSource, name, value, value.getClass()));
358-
359-
return parameterSource;
334+
SqlParameterSource parameterSource = sqlParametersFactory.getIdentifier(identifier);
335+
return operations.query(findAllByProperty, parameterSource, (RowMapper<Object>) rowMapper);
360336
}
361337

362338
/*
@@ -367,7 +343,7 @@ private SqlParameterSource createParameterSource(Identifier identifier, Identifi
367343
public <T> boolean existsById(Object id, Class<T> domainType) {
368344

369345
String existsSql = sql(domainType).getExists();
370-
SqlParameterSource parameter = createIdParameterSource(id, domainType);
346+
SqlParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
371347

372348
Boolean result = operations.queryForObject(existsSql, parameter, Boolean.class);
373349
Assert.state(result != null, "The result of an exists query must not be null");
@@ -411,73 +387,10 @@ private RowMapper<?> getMapEntityRowMapper(PersistentPropertyPathExtension path,
411387
return new MapEntityRowMapper<>(path, converter, identifier, keyColumn, getIdentifierProcessing());
412388
}
413389

414-
// TODO: Move to SqlParametersFactory
415-
private <T> SqlIdentifierParameterSource createIdParameterSource(Object id, Class<T> domainType) {
416-
417-
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(getIdentifierProcessing());
418-
419-
addConvertedPropertyValue( //
420-
parameterSource, //
421-
getRequiredPersistentEntity(domainType).getRequiredIdProperty(), //
422-
id, //
423-
ID_SQL_PARAMETER //
424-
);
425-
return parameterSource;
426-
}
427-
428390
private IdentifierProcessing getIdentifierProcessing() {
429391
return sqlGeneratorSource.getDialect().getIdentifierProcessing();
430392
}
431393

432-
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource,
433-
RelationalPersistentProperty property, @Nullable Object value, SqlIdentifier name) {
434-
435-
addConvertedValue(parameterSource, value, name, converter.getColumnType(property), converter.getTargetSqlType(property));
436-
}
437-
438-
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource, SqlIdentifier name, Object value,
439-
Class<?> javaType) {
440-
441-
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.targetSqlTypeFor(javaType));
442-
}
443-
444-
private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nullable Object value,
445-
SqlIdentifier paramName, Class<?> javaType, SQLType sqlType) {
446-
447-
JdbcValue jdbcValue = converter.writeJdbcValue( //
448-
value, //
449-
javaType, //
450-
sqlType //
451-
);
452-
453-
parameterSource.addValue( //
454-
paramName, //
455-
jdbcValue.getValue(), //
456-
jdbcValue.getJdbcType().getVendorTypeNumber());
457-
}
458-
459-
private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource parameterSource,
460-
RelationalPersistentProperty property, Iterable<?> values, SqlIdentifier paramName) {
461-
462-
List<Object> convertedIds = new ArrayList<>();
463-
JdbcValue jdbcValue = null;
464-
for (Object id : values) {
465-
466-
Class<?> columnType = converter.getColumnType(property);
467-
SQLType sqlType = converter.getTargetSqlType(property);
468-
469-
jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
470-
convertedIds.add(jdbcValue.getValue());
471-
}
472-
473-
Assert.state(jdbcValue != null, "JdbcValue must be not null at this point. Please report this as a bug.");
474-
475-
SQLType jdbcType = jdbcValue.getJdbcType();
476-
int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();
477-
478-
parameterSource.addValue(paramName, convertedIds, typeNumber);
479-
}
480-
481394
@SuppressWarnings("unchecked")
482395
private <S> RelationalPersistentEntity<S> getRequiredPersistentEntity(Class<S> domainType) {
483396
return (RelationalPersistentEntity<S>) context.getRequiredPersistentEntity(domainType);

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@
1010
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
1111
import org.springframework.data.relational.core.sql.IdentifierProcessing;
1212
import org.springframework.data.relational.core.sql.SqlIdentifier;
13+
import org.springframework.jdbc.support.JdbcUtils;
1314
import org.springframework.lang.Nullable;
15+
import org.springframework.util.Assert;
1416

1517
import java.sql.SQLType;
18+
import java.util.ArrayList;
19+
import java.util.List;
1620
import java.util.function.Predicate;
1721

22+
import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;
23+
1824
public class SqlParametersFactory {
1925
private final RelationalMappingContext context;
2026
private final JdbcConverter converter;
@@ -46,6 +52,37 @@ <T> SqlIdentifierParameterSource getUpdate(T instance, Class<T> domainType) {
4652
return getParameterSource(instance, getRequiredPersistentEntity(domainType), "", Predicates.includeAll(),
4753
dialect.getIdentifierProcessing());
4854
}
55+
56+
<T> SqlIdentifierParameterSource getId(Object id, Class<T> domainType, SqlIdentifier name) {
57+
58+
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(dialect.getIdentifierProcessing());
59+
60+
addConvertedPropertyValue( //
61+
parameterSource, //
62+
getRequiredPersistentEntity(domainType).getRequiredIdProperty(), //
63+
id, //
64+
name //
65+
);
66+
return parameterSource;
67+
}
68+
69+
<T> SqlIdentifierParameterSource getIds(Iterable<?> ids, Class<T> domainType) {
70+
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(dialect.getIdentifierProcessing());
71+
72+
addConvertedPropertyValuesAsList(parameterSource, getRequiredPersistentEntity(domainType).getRequiredIdProperty(),
73+
ids, IDS_SQL_PARAMETER);
74+
75+
return parameterSource;
76+
}
77+
78+
SqlIdentifierParameterSource getIdentifier(Identifier identifier) {
79+
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource(dialect.getIdentifierProcessing());
80+
81+
identifier.toMap()
82+
.forEach((name, value) -> addConvertedPropertyValue(parameterSource, name, value, value.getClass()));
83+
84+
return parameterSource;
85+
}
4986

5087
/**
5188
* Utility to create {@link Predicate}s.
@@ -112,6 +149,28 @@ private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nu
112149
jdbcValue.getJdbcType().getVendorTypeNumber());
113150
}
114151

152+
private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource parameterSource,
153+
RelationalPersistentProperty property, Iterable<?> values, SqlIdentifier paramName) {
154+
155+
List<Object> convertedIds = new ArrayList<>();
156+
JdbcValue jdbcValue = null;
157+
for (Object id : values) {
158+
159+
Class<?> columnType = converter.getColumnType(property);
160+
SQLType sqlType = converter.getTargetSqlType(property);
161+
162+
jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
163+
convertedIds.add(jdbcValue.getValue());
164+
}
165+
166+
Assert.state(jdbcValue != null, "JdbcValue must be not null at this point. Please report this as a bug.");
167+
168+
SQLType jdbcType = jdbcValue.getJdbcType();
169+
int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();
170+
171+
parameterSource.addValue(paramName, convertedIds, typeNumber);
172+
}
173+
115174
@SuppressWarnings("unchecked")
116175
private <S> RelationalPersistentEntity<S> getRequiredPersistentEntity(Class<S> domainType) {
117176
return (RelationalPersistentEntity<S>) context.getRequiredPersistentEntity(domainType);

0 commit comments

Comments
 (0)