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
@@ -165,7 +160,7 @@ public <S> boolean updateWithVersion(S instance, Class<S> domainType, Number pre
165
160
public void delete (Object id , Class <?> domainType ) {
166
161
167
162
String deleteByIdSql = sql (domainType ).getDeleteById ();
168
- SqlParameterSource parameter = createIdParameterSource (id , domainType );
163
+ SqlParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
169
164
170
165
operations .update (deleteByIdSql , parameter );
171
166
}
@@ -181,7 +176,7 @@ public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previou
181
176
182
177
RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
183
178
184
- SqlIdentifierParameterSource parameterSource = createIdParameterSource (id , domainType );
179
+ SqlIdentifierParameterSource parameterSource = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
185
180
parameterSource .addValue (VERSION_SQL_PARAMETER , previousVersion );
186
181
int affectedRows = operations .update (sql (domainType ).getDeleteByIdAndVersion (), parameterSource );
187
182
@@ -206,13 +201,7 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
206
201
207
202
String delete = sql (rootEntity .getType ()).createDeleteByPath (propertyPath );
208
203
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 );
216
205
operations .update (delete , parameters );
217
206
}
218
207
@@ -243,7 +232,7 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
243
232
public <T > void acquireLockById (Object id , LockMode lockMode , Class <T > domainType ) {
244
233
245
234
String acquireLockByIdSql = sql (domainType ).getAcquireLockById (lockMode );
246
- SqlIdentifierParameterSource parameter = createIdParameterSource (id , domainType );
235
+ SqlIdentifierParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
247
236
248
237
operations .query (acquireLockByIdSql , parameter , ResultSet ::next );
249
238
}
@@ -282,7 +271,7 @@ public long count(Class<?> domainType) {
282
271
public <T > T findById (Object id , Class <T > domainType ) {
283
272
284
273
String findOneSql = sql (domainType ).getFindOne ();
285
- SqlIdentifierParameterSource parameter = createIdParameterSource (id , domainType );
274
+ SqlIdentifierParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
286
275
287
276
try {
288
277
return operations .queryForObject (findOneSql , parameter , (RowMapper <T >) getEntityRowMapper (domainType ));
@@ -313,10 +302,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
313
302
return Collections .emptyList ();
314
303
}
315
304
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 );
320
306
321
307
String findAllInListSql = sql (domainType ).getFindAllInList ();
322
308
@@ -344,18 +330,8 @@ public Iterable<Object> findAllByPath(Identifier identifier,
344
330
RowMapper <?> rowMapper = path .isMap () ? this .getMapEntityRowMapper (path , identifier )
345
331
: this .getEntityRowMapper (path , identifier );
346
332
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 );
359
335
}
360
336
361
337
/*
@@ -366,7 +342,7 @@ private SqlParameterSource createParameterSource(Identifier identifier, Identifi
366
342
public <T > boolean existsById (Object id , Class <T > domainType ) {
367
343
368
344
String existsSql = sql (domainType ).getExists ();
369
- SqlParameterSource parameter = createIdParameterSource (id , domainType );
345
+ SqlParameterSource parameter = sqlParametersFactory . getId (id , domainType , ID_SQL_PARAMETER );
370
346
371
347
Boolean result = operations .queryForObject (existsSql , parameter , Boolean .class );
372
348
Assert .state (result != null , "The result of an exists query must not be null" );
@@ -410,73 +386,10 @@ private RowMapper<?> getMapEntityRowMapper(PersistentPropertyPathExtension path,
410
386
return new MapEntityRowMapper <>(path , converter , identifier , keyColumn , getIdentifierProcessing ());
411
387
}
412
388
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
-
427
389
private IdentifierProcessing getIdentifierProcessing () {
428
390
return sqlGeneratorSource .getDialect ().getIdentifierProcessing ();
429
391
}
430
392
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
-
480
393
@ SuppressWarnings ("unchecked" )
481
394
private <S > RelationalPersistentEntity <S > getRequiredPersistentEntity (Class <S > domainType ) {
482
395
return (RelationalPersistentEntity <S >) context .getRequiredPersistentEntity (domainType );
0 commit comments