47
47
import org .apache .commons .logging .Log ;
48
48
import org .apache .commons .logging .LogFactory ;
49
49
import org .reactivestreams .Publisher ;
50
+
50
51
import org .springframework .dao .DataAccessException ;
51
52
import org .springframework .data .domain .Pageable ;
52
53
import org .springframework .data .domain .Sort ;
53
54
import org .springframework .data .r2dbc .UncategorizedR2dbcException ;
54
55
import org .springframework .data .r2dbc .function .connectionfactory .ConnectionProxy ;
55
56
import org .springframework .data .r2dbc .function .convert .ColumnMapRowMapper ;
57
+ import org .springframework .data .r2dbc .function .convert .OutboundRow ;
56
58
import org .springframework .data .r2dbc .function .convert .SettableValue ;
57
59
import org .springframework .data .r2dbc .support .R2dbcExceptionTranslator ;
58
60
import org .springframework .jdbc .core .SqlProvider ;
@@ -365,26 +367,30 @@ <T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFun
365
367
366
368
public ExecuteSpecSupport bind (int index , Object value ) {
367
369
370
+ Assert .notNull (value , () -> String .format ("Value at index %d must not be null. Use bindNull(…) instead." , index ));
371
+
368
372
Map <Integer , SettableValue > byIndex = new LinkedHashMap <>(this .byIndex );
369
- byIndex .put (index , new SettableValue (index , value , null ));
373
+ byIndex .put (index , new SettableValue (value , value . getClass () ));
370
374
371
375
return createInstance (byIndex , this .byName , this .sqlSupplier );
372
376
}
373
377
374
378
public ExecuteSpecSupport bindNull (int index , Class <?> type ) {
375
379
376
380
Map <Integer , SettableValue > byIndex = new LinkedHashMap <>(this .byIndex );
377
- byIndex .put (index , new SettableValue (index , null , type ));
381
+ byIndex .put (index , new SettableValue (null , type ));
378
382
379
383
return createInstance (byIndex , this .byName , this .sqlSupplier );
380
384
}
381
385
382
386
public ExecuteSpecSupport bind (String name , Object value ) {
383
387
384
388
Assert .hasText (name , "Parameter name must not be null or empty!" );
389
+ Assert .notNull (value ,
390
+ () -> String .format ("Value for parameter %s must not be null. Use bindNull(…) instead." , name ));
385
391
386
392
Map <String , SettableValue > byName = new LinkedHashMap <>(this .byName );
387
- byName .put (name , new SettableValue (name , value , null ));
393
+ byName .put (name , new SettableValue (value , value . getClass () ));
388
394
389
395
return createInstance (this .byIndex , byName , this .sqlSupplier );
390
396
}
@@ -394,7 +400,7 @@ public ExecuteSpecSupport bindNull(String name, Class<?> type) {
394
400
Assert .hasText (name , "Parameter name must not be null or empty!" );
395
401
396
402
Map <String , SettableValue > byName = new LinkedHashMap <>(this .byName );
397
- byName .put (name , new SettableValue (name , null , type ));
403
+ byName .put (name , new SettableValue (null , type ));
398
404
399
405
return createInstance (this .byIndex , byName , this .sqlSupplier );
400
406
}
@@ -841,9 +847,11 @@ class DefaultGenericInsertSpec<T> implements GenericInsertSpec<T> {
841
847
public GenericInsertSpec value (String field , Object value ) {
842
848
843
849
Assert .notNull (field , "Field must not be null!" );
850
+ Assert .notNull (value ,
851
+ () -> String .format ("Value for field %s must not be null. Use nullValue(…) instead." , field ));
844
852
845
853
Map <String , SettableValue > byName = new LinkedHashMap <>(this .byName );
846
- byName .put (field , new SettableValue (field , value , null ));
854
+ byName .put (field , new SettableValue (value , value . getClass () ));
847
855
848
856
return new DefaultGenericInsertSpec <>(this .table , byName , this .mappingFunction );
849
857
}
@@ -854,7 +862,7 @@ public GenericInsertSpec nullValue(String field, Class<?> type) {
854
862
Assert .notNull (field , "Field must not be null!" );
855
863
856
864
Map <String , SettableValue > byName = new LinkedHashMap <>(this .byName );
857
- byName .put (field , new SettableValue (field , null , type ));
865
+ byName .put (field , new SettableValue (null , type ));
858
866
859
867
return new DefaultGenericInsertSpec <>(this .table , byName , this .mappingFunction );
860
868
}
@@ -894,7 +902,7 @@ private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunctio
894
902
895
903
Statement statement = it .createStatement (sql ).returnGeneratedValues ();
896
904
897
- byName .forEach ((k , v ) -> bindableInsert .bind (statement , v ));
905
+ byName .forEach ((k , v ) -> bindableInsert .bind (statement , k , v ));
898
906
899
907
return statement ;
900
908
};
@@ -998,12 +1006,16 @@ public Mono<Integer> rowsUpdated() {
998
1006
999
1007
private <MR > FetchSpec <MR > exchange (Object toInsert , BiFunction <Row , RowMetadata , MR > mappingFunction ) {
1000
1008
1001
- List <SettableValue > insertValues = dataAccessStrategy .getValuesToInsert (toInsert );
1009
+ OutboundRow outboundRow = dataAccessStrategy .getOutboundRow (toInsert );
1010
+
1002
1011
Set <String > columns = new LinkedHashSet <>();
1003
1012
1004
- for (SettableValue insertValue : insertValues ) {
1005
- columns .add (insertValue .getIdentifier ().toString ());
1006
- }
1013
+ outboundRow .forEach ((k , v ) -> {
1014
+
1015
+ if (v .hasValue ()) {
1016
+ columns .add (k );
1017
+ }
1018
+ });
1007
1019
1008
1020
BindableOperation bindableInsert = dataAccessStrategy .insertAndReturnGeneratedKeys (table , columns );
1009
1021
@@ -1017,9 +1029,11 @@ private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata
1017
1029
1018
1030
Statement statement = it .createStatement (sql ).returnGeneratedValues ();
1019
1031
1020
- for (SettableValue settable : insertValues ) {
1021
- bindableInsert .bind (statement , settable );
1022
- }
1032
+ outboundRow .forEach ((k , v ) -> {
1033
+ if (v .hasValue ()) {
1034
+ bindableInsert .bind (statement , k , v );
1035
+ }
1036
+ });
1023
1037
1024
1038
return statement ;
1025
1039
};
0 commit comments