Skip to content

Commit 6d0bc57

Browse files
committed
#57 - hacking - ExpandedBindings work as well.
1 parent 9e61e06 commit 6d0bc57

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

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

-22
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ public final boolean isNamed() {
5252
}
5353
}
5454

55-
public static class NamedSingleBinding<T> extends SingleBinding<T> {
56-
57-
public NamedSingleBinding(T identifier, SettableValue value) {
58-
super(identifier, value);
59-
}
60-
61-
@Override
62-
public void bindTo(Statement statement) {
63-
64-
if (value.isEmpty()) {
65-
statement.bindNull(identifier, value.getType());
66-
} else {
67-
statement.bind(identifier, value.getValue());
68-
}
69-
70-
}
71-
72-
@Override
73-
public boolean isIndexed() {
74-
return false;
75-
}
76-
}
7755

7856
public static class IndexedSingleBinding extends SingleBinding<Integer> {
7957

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ <T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFun
337337
if (sqlSupplier instanceof PreparedOperation<?>) {
338338
pop = ((PreparedOperation<?>) sqlSupplier);
339339
} else {
340-
pop = new ParameterbindingPreparedOperation(sql, namedParameters, dataAccessStrategy, byName, byIndex);
340+
pop = new ExpandedPreparedOperation(sql, namedParameters, dataAccessStrategy, byName, byIndex);
341341
}
342342

343343
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(pop.createBoundStatement(it).execute());
+30-18
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,61 @@
1818
import io.r2dbc.spi.Connection;
1919
import io.r2dbc.spi.Statement;
2020

21+
import java.util.ArrayList;
22+
import java.util.List;
2123
import java.util.Map;
22-
import java.util.function.Function;
2324

2425
import org.springframework.data.r2dbc.domain.SettableValue;
2526

2627
/**
2728
* @author Jens Schauder
2829
*/
29-
public class ParameterbindingPreparedOperation implements PreparedOperation<BindableOperation> {
30+
public class ExpandedPreparedOperation
31+
extends DefaultStatementFactory.PreparedOperationSupport<BindableOperation> {
3032

3133
private final BindableOperation operation;
3234
private final Map<String, SettableValue> byName;
3335
private final Map<Integer, SettableValue> byIndex;
3436

35-
private ParameterbindingPreparedOperation(BindableOperation operation, Map<String, SettableValue> byName,
36-
Map<Integer, SettableValue> byIndex) {
37+
private ExpandedPreparedOperation(BindableOperation operation, Map<String, SettableValue> byName,
38+
Map<Integer, SettableValue> byIndex) {
39+
40+
super(createBindings(operation, byName, byIndex));
3741

3842
this.operation = operation;
3943
this.byName = byName;
4044
this.byIndex = byIndex;
4145
}
4246

43-
ParameterbindingPreparedOperation(String sql, NamedParameterExpander namedParameters,
44-
ReactiveDataAccessStrategy dataAccessStrategy, Map<String, SettableValue> byName,
47+
private static Bindings createBindings(BindableOperation operation, Map<String, SettableValue> byName,
4548
Map<Integer, SettableValue> byIndex) {
4649

50+
List<Bindings.SingleBinding> bindings = new ArrayList<>();
51+
52+
byName.forEach(
53+
(identifier, settableValue) -> bindings.add(new Bindings.NamedExpandedSingleBinding(identifier, settableValue, operation)));
54+
55+
byIndex.forEach((identifier, settableValue) -> bindings.add(new Bindings.IndexedSingleBinding(identifier, settableValue)));
56+
57+
return new Bindings(bindings);
58+
}
59+
60+
ExpandedPreparedOperation(String sql, NamedParameterExpander namedParameters,
61+
ReactiveDataAccessStrategy dataAccessStrategy, Map<String, SettableValue> byName,
62+
Map<Integer, SettableValue> byIndex) {
63+
4764
this( //
4865
namedParameters.expand(sql, dataAccessStrategy.getBindMarkersFactory(), new MapBindParameterSource(byName)), //
4966
byName, //
5067
byIndex //
5168
);
5269
}
5370

71+
@Override
72+
protected String createBaseSql() {
73+
return toQuery();
74+
}
75+
5476
@Override
5577
public BindableOperation getSource() {
5678
return operation;
@@ -67,28 +89,18 @@ public Statement createBoundStatement(Connection connection) {
6789
return statement;
6890
}
6991

70-
@Override
71-
public void addSqlFilter(Function<String, String> filter) {
72-
73-
}
74-
75-
@Override
76-
public void addBindingFilter(Function<Bindings, Bindings> filter) {
77-
78-
}
79-
8092
@Override
8193
public String toQuery() {
8294
return operation.toQuery();
8395
}
8496

8597
// todo that is a weird assymmetry between bindByName and bindByIndex
86-
private void bindByName(Statement statement, Map<String, SettableValue> byName) {
98+
private void bindByName(Statement statement, Map<String, SettableValue> byName) {
8799

88100
byName.forEach((name, o) -> {
89101

90102
if (o.getValue() != null) {
91-
operation.bind(statement,name, o.getValue());
103+
operation.bind(statement, name, o.getValue());
92104
} else {
93105
operation.bindNull(statement, name, o.getType());
94106
}

0 commit comments

Comments
 (0)