Skip to content

Commit 5f6ced5

Browse files
committed
#57 - hacking - extracted ParameterbindingPreparedOperation.
1 parent 78d5ceb commit 5f6ced5

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ protected String getSql() {
332332

333333
<T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
334334

335+
PreparedOperation pop;
336+
337+
if (sqlSupplier instanceof PreparedOperation<?>) {
338+
pop = ((PreparedOperation<?>) sqlSupplier);
339+
} else {
340+
pop = new PrameterbindingPreparedOperation(namedParameters.expand(sql, dataAccessStrategy.getBindMarkersFactory(),
341+
new MapBindParameterSource(byName)), byName, byIndex);
342+
}
343+
344+
345+
335346
Function<Connection, Statement> executeFunction = it -> {
336347

337348
if (logger.isDebugEnabled()) {
@@ -365,7 +376,7 @@ <T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFun
365376
return statement;
366377
};
367378

368-
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(executeFunction.apply(it).execute());
379+
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(pop.bind(it).execute());
369380

370381
return new DefaultSqlResult<>(DefaultDatabaseClient.this, //
371382
sql, //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.function;
17+
18+
import io.r2dbc.spi.Connection;
19+
import io.r2dbc.spi.Statement;
20+
import org.springframework.data.r2dbc.domain.SettableValue;
21+
22+
import java.util.Map;
23+
24+
/**
25+
* @author Jens Schauder
26+
*/
27+
public class PrameterbindingPreparedOperation implements PreparedOperation<BindableOperation> {
28+
29+
private final BindableOperation operation;
30+
private final Map<String, SettableValue> byName;
31+
private final Map<Integer, SettableValue> byIndex;
32+
33+
public PrameterbindingPreparedOperation(BindableOperation operation, Map<String, SettableValue> byName, Map<Integer, SettableValue> byIndex) {
34+
35+
this.operation = operation;
36+
this.byName = byName;
37+
this.byIndex = byIndex;
38+
}
39+
40+
@Override
41+
public BindableOperation getSource() {
42+
return operation;
43+
}
44+
45+
@Override
46+
public Statement bind(Statement to) {
47+
return null;
48+
}
49+
50+
@Override
51+
public Statement bind(Connection connection) {
52+
53+
Statement statement = connection.createStatement(operation.toQuery());
54+
55+
byName.forEach((name, o) -> {
56+
57+
if (o.getValue() != null) {
58+
operation.bind(statement, name, o.getValue());
59+
} else {
60+
operation.bindNull(statement, name, o.getType());
61+
}
62+
});
63+
64+
bindByIndex(statement, byIndex);
65+
66+
return statement; }
67+
68+
@Override
69+
public String toQuery() {
70+
return null;
71+
}
72+
73+
74+
75+
private static void bindByName(Statement statement, Map<String, SettableValue> byName) {
76+
77+
byName.forEach((name, o) -> {
78+
79+
if (o.getValue() != null) {
80+
statement.bind(name, o.getValue());
81+
} else {
82+
statement.bindNull(name, o.getType());
83+
}
84+
});
85+
}
86+
87+
private static void bindByIndex(Statement statement, Map<Integer, SettableValue> byIndex) {
88+
89+
byIndex.forEach((i, o) -> {
90+
91+
if (o.getValue() != null) {
92+
statement.bind(i.intValue(), o.getValue());
93+
} else {
94+
statement.bindNull(i.intValue(), o.getType());
95+
}
96+
});
97+
}
98+
}

0 commit comments

Comments
 (0)