Skip to content

Commit c37bb7d

Browse files
committed
Move remaining methods for retrieving SqlParameterSource from DefaultDataAccessStrategy to SqlParametersFactory
1 parent 582ac9e commit c37bb7d

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

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

167162
String deleteByIdSql = sql(domainType).getDeleteById();
168-
SqlParameterSource parameter = createIdParameterSource(id, domainType);
163+
SqlParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
169164

170165
operations.update(deleteByIdSql, parameter);
171166
}
@@ -181,7 +176,7 @@ public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previou
181176

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

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

@@ -206,13 +201,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
206201

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

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

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

245234
String acquireLockByIdSql = sql(domainType).getAcquireLockById(lockMode);
246-
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
235+
SqlIdentifierParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
247236

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

284273
String findOneSql = sql(domainType).getFindOne();
285-
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
274+
SqlIdentifierParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
286275

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

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

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

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

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

361337
/*
@@ -366,7 +342,7 @@ private SqlParameterSource createParameterSource(Identifier identifier, Identifi
366342
public <T> boolean existsById(Object id, Class<T> domainType) {
367343

368344
String existsSql = sql(domainType).getExists();
369-
SqlParameterSource parameter = createIdParameterSource(id, domainType);
345+
SqlParameterSource parameter = sqlParametersFactory.getId(id, domainType, ID_SQL_PARAMETER);
370346

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

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

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