Skip to content

Commit 65634ee

Browse files
committed
#61 - Move Conversion-related functionality to MappingR2dbcConverter.
All conversion functionality is now pulled together into MappingR2dbcConverter. Introduce OutboundRow to provide mapping between column names and settable values. Remove identifier from SettableValue.
1 parent 29e8373 commit 65634ee

24 files changed

+743
-339
lines changed

src/main/java/org/springframework/data/r2dbc/config/AbstractR2dbcConfiguration.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.r2dbc.function.DatabaseClient;
3131
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
3232
import org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy;
33+
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;
3334
import org.springframework.data.r2dbc.function.convert.R2dbcCustomConversions;
3435
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
3536
import org.springframework.data.r2dbc.support.SqlErrorCodeR2dbcExceptionTranslator;
@@ -118,8 +119,8 @@ public RelationalMappingContext r2dbcMappingContext(Optional<NamingStrategy> nam
118119
}
119120

120121
/**
121-
* Creates a {@link ReactiveDataAccessStrategy} using the configured {@link #r2dbcMappingContext(Optional, R2dbcCustomConversions)}
122-
* RelationalMappingContext}.
122+
* Creates a {@link ReactiveDataAccessStrategy} using the configured
123+
* {@link #r2dbcMappingContext(Optional, R2dbcCustomConversions)} RelationalMappingContext}.
123124
*
124125
* @param mappingContext the configured {@link RelationalMappingContext}.
125126
* @param r2dbcCustomConversions customized R2DBC conversions.
@@ -134,7 +135,7 @@ public ReactiveDataAccessStrategy reactiveDataAccessStrategy(RelationalMappingCo
134135

135136
Assert.notNull(mappingContext, "MappingContext must not be null!");
136137

137-
BasicRelationalConverter converter = new BasicRelationalConverter(mappingContext, r2dbcCustomConversions);
138+
MappingR2dbcConverter converter = new MappingR2dbcConverter(mappingContext, r2dbcCustomConversions);
138139

139140
return new DefaultReactiveDataAccessStrategy(getDialect(connectionFactory()), converter);
140141
}

src/main/java/org/springframework/data/r2dbc/function/BindableOperation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public interface BindableOperation extends QueryOperation {
4545
* @see Statement#bind
4646
* @see Statement#bindNull
4747
*/
48-
default void bind(Statement statement, SettableValue value) {
48+
default void bind(Statement statement, String identifier, SettableValue value) {
4949

5050
if (value.getValue() == null) {
51-
bindNull(statement, value.getIdentifier().toString(), value.getType());
51+
bindNull(statement, identifier, value.getType());
5252
} else {
53-
bind(statement, value.getIdentifier().toString(), value.getValue());
53+
bind(statement, identifier, value.getValue());
5454
}
5555
}
5656

src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java

+28-14
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
import org.apache.commons.logging.Log;
4848
import org.apache.commons.logging.LogFactory;
4949
import org.reactivestreams.Publisher;
50+
5051
import org.springframework.dao.DataAccessException;
5152
import org.springframework.data.domain.Pageable;
5253
import org.springframework.data.domain.Sort;
5354
import org.springframework.data.r2dbc.UncategorizedR2dbcException;
5455
import org.springframework.data.r2dbc.function.connectionfactory.ConnectionProxy;
5556
import org.springframework.data.r2dbc.function.convert.ColumnMapRowMapper;
57+
import org.springframework.data.r2dbc.function.convert.OutboundRow;
5658
import org.springframework.data.r2dbc.function.convert.SettableValue;
5759
import org.springframework.data.r2dbc.support.R2dbcExceptionTranslator;
5860
import org.springframework.jdbc.core.SqlProvider;
@@ -365,26 +367,30 @@ <T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFun
365367

366368
public ExecuteSpecSupport bind(int index, Object value) {
367369

370+
Assert.notNull(value, () -> String.format("Value at index %d must not be null. Use bindNull(…) instead.", index));
371+
368372
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()));
370374

371375
return createInstance(byIndex, this.byName, this.sqlSupplier);
372376
}
373377

374378
public ExecuteSpecSupport bindNull(int index, Class<?> type) {
375379

376380
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));
378382

379383
return createInstance(byIndex, this.byName, this.sqlSupplier);
380384
}
381385

382386
public ExecuteSpecSupport bind(String name, Object value) {
383387

384388
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));
385391

386392
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()));
388394

389395
return createInstance(this.byIndex, byName, this.sqlSupplier);
390396
}
@@ -394,7 +400,7 @@ public ExecuteSpecSupport bindNull(String name, Class<?> type) {
394400
Assert.hasText(name, "Parameter name must not be null or empty!");
395401

396402
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));
398404

399405
return createInstance(this.byIndex, byName, this.sqlSupplier);
400406
}
@@ -841,9 +847,11 @@ class DefaultGenericInsertSpec<T> implements GenericInsertSpec<T> {
841847
public GenericInsertSpec value(String field, Object value) {
842848

843849
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));
844852

845853
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()));
847855

848856
return new DefaultGenericInsertSpec<>(this.table, byName, this.mappingFunction);
849857
}
@@ -854,7 +862,7 @@ public GenericInsertSpec nullValue(String field, Class<?> type) {
854862
Assert.notNull(field, "Field must not be null!");
855863

856864
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));
858866

859867
return new DefaultGenericInsertSpec<>(this.table, byName, this.mappingFunction);
860868
}
@@ -894,7 +902,7 @@ private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunctio
894902

895903
Statement statement = it.createStatement(sql).returnGeneratedValues();
896904

897-
byName.forEach((k, v) -> bindableInsert.bind(statement, v));
905+
byName.forEach((k, v) -> bindableInsert.bind(statement, k, v));
898906

899907
return statement;
900908
};
@@ -998,12 +1006,16 @@ public Mono<Integer> rowsUpdated() {
9981006

9991007
private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata, MR> mappingFunction) {
10001008

1001-
List<SettableValue> insertValues = dataAccessStrategy.getValuesToInsert(toInsert);
1009+
OutboundRow outboundRow = dataAccessStrategy.getOutboundRow(toInsert);
1010+
10021011
Set<String> columns = new LinkedHashSet<>();
10031012

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+
});
10071019

10081020
BindableOperation bindableInsert = dataAccessStrategy.insertAndReturnGeneratedKeys(table, columns);
10091021

@@ -1017,9 +1029,11 @@ private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata
10171029

10181030
Statement statement = it.createStatement(sql).returnGeneratedValues();
10191031

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+
});
10231037

10241038
return statement;
10251039
};

0 commit comments

Comments
 (0)