18
18
import static org .springframework .data .jdbc .core .convert .SqlGenerator .*;
19
19
20
20
import java .sql .ResultSet ;
21
- import java .sql .SQLType ;
22
- import java .util .ArrayList ;
23
21
import java .util .Collections ;
24
22
import java .util .List ;
25
23
import java .util .Optional ;
28
26
import org .springframework .dao .OptimisticLockingFailureException ;
29
27
import org .springframework .data .domain .Pageable ;
30
28
import org .springframework .data .domain .Sort ;
31
- import org .springframework .data .jdbc .core .mapping .JdbcValue ;
32
- import org .springframework .data .jdbc .support .JdbcUtil ;
33
29
import org .springframework .data .mapping .PersistentPropertyPath ;
34
30
import org .springframework .data .relational .core .mapping .PersistentPropertyPathExtension ;
35
31
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
41
37
import org .springframework .jdbc .core .RowMapper ;
42
38
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
43
39
import org .springframework .jdbc .core .namedparam .SqlParameterSource ;
44
- import org .springframework .jdbc .support .JdbcUtils ;
45
40
import org .springframework .lang .Nullable ;
46
41
import org .springframework .util .Assert ;
47
42
@@ -166,7 +161,7 @@ public <S> boolean updateWithVersion(S instance, Class<S> domainType, Number pre
166
161
public void delete (Object id , Class <?> domainType ) {
167
162
168
163
String deleteByIdSql = sql (domainType ).getDeleteById ();
169
- SqlParameterSource parameter = createIdParameterSource (id , domainType );
164
+ SqlParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
170
165
171
166
operations .update (deleteByIdSql , parameter );
172
167
}
@@ -182,7 +177,7 @@ public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previou
182
177
183
178
RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
184
179
185
- SqlIdentifierParameterSource parameterSource = createIdParameterSource (id , domainType );
180
+ SqlIdentifierParameterSource parameterSource = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
186
181
parameterSource .addValue (VERSION_SQL_PARAMETER , previousVersion );
187
182
int affectedRows = operations .update (sql (domainType ).getDeleteByIdAndVersion (), parameterSource );
188
183
@@ -207,13 +202,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
207
202
208
203
String delete = sql (rootEntity .getType ()).createDeleteByPath (propertyPath );
209
204
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 );
217
206
operations .update (delete , parameters );
218
207
}
219
208
@@ -244,7 +233,7 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
244
233
public <T > void acquireLockById (Object id , LockMode lockMode , Class <T > domainType ) {
245
234
246
235
String acquireLockByIdSql = sql (domainType ).getAcquireLockById (lockMode );
247
- SqlIdentifierParameterSource parameter = createIdParameterSource (id , domainType );
236
+ SqlIdentifierParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
248
237
249
238
operations .query (acquireLockByIdSql , parameter , ResultSet ::next );
250
239
}
@@ -283,7 +272,7 @@ public long count(Class<?> domainType) {
283
272
public <T > T findById (Object id , Class <T > domainType ) {
284
273
285
274
String findOneSql = sql (domainType ).getFindOne ();
286
- SqlIdentifierParameterSource parameter = createIdParameterSource (id , domainType );
275
+ SqlIdentifierParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
287
276
288
277
try {
289
278
return operations .queryForObject (findOneSql , parameter , (RowMapper <T >) getEntityRowMapper (domainType ));
@@ -314,10 +303,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
314
303
return Collections .emptyList ();
315
304
}
316
305
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 );
321
307
322
308
String findAllInListSql = sql (domainType ).getFindAllInList ();
323
309
@@ -345,18 +331,8 @@ public Iterable<Object> findAllByPath(Identifier identifier,
345
331
RowMapper <?> rowMapper = path .isMap () ? this .getMapEntityRowMapper (path , identifier )
346
332
: this .getEntityRowMapper (path , identifier );
347
333
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 );
360
336
}
361
337
362
338
/*
@@ -367,7 +343,7 @@ private SqlParameterSource createParameterSource(Identifier identifier, Identifi
367
343
public <T > boolean existsById (Object id , Class <T > domainType ) {
368
344
369
345
String existsSql = sql (domainType ).getExists ();
370
- SqlParameterSource parameter = createIdParameterSource (id , domainType );
346
+ SqlParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
371
347
372
348
Boolean result = operations .queryForObject (existsSql , parameter , Boolean .class );
373
349
Assert .state (result != null , "The result of an exists query must not be null" );
@@ -411,73 +387,10 @@ private RowMapper<?> getMapEntityRowMapper(PersistentPropertyPathExtension path,
411
387
return new MapEntityRowMapper <>(path , converter , identifier , keyColumn , getIdentifierProcessing ());
412
388
}
413
389
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
-
428
390
private IdentifierProcessing getIdentifierProcessing () {
429
391
return sqlGeneratorSource .getDialect ().getIdentifierProcessing ();
430
392
}
431
393
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
-
481
394
@ SuppressWarnings ("unchecked" )
482
395
private <S > RelationalPersistentEntity <S > getRequiredPersistentEntity (Class <S > domainType ) {
483
396
return (RelationalPersistentEntity <S >) context .getRequiredPersistentEntity (domainType );
0 commit comments