Skip to content

Commit 02e0cb5

Browse files
schaudermp911de
authored andcommitted
#73 - Polishing.
Original pull request: #82.
1 parent ef2d885 commit 02e0cb5

File tree

8 files changed

+505
-111
lines changed

8 files changed

+505
-111
lines changed

src/main/java/org/springframework/data/r2dbc/dialect/IndexedBindMarker.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
/**
2121
* A single indexed bind marker.
2222
*/
23-
class IndexedBindMarker implements BindMarker {
23+
public class IndexedBindMarker implements BindMarker {
2424

2525
private final String placeholder;
2626

27-
private int index;
27+
private final int index;
2828

2929
IndexedBindMarker(String placeholder, int index) {
3030
this.placeholder = placeholder;
@@ -57,4 +57,10 @@ public void bind(Statement statement, Object value) {
5757
public void bindNull(Statement statement, Class<?> valueType) {
5858
statement.bindNull(this.index, valueType);
5959
}
60+
61+
62+
public int getIndex() {
63+
return index;
64+
}
65+
6066
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.Statement;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.List;
22+
23+
import org.springframework.data.r2dbc.domain.SettableValue;
24+
25+
/**
26+
* @author Jens Schauder
27+
*/
28+
public class Bindings {
29+
30+
private final List<SingleBinding> bindings;
31+
32+
public Bindings(List<SingleBinding> bindings) {
33+
this.bindings = bindings;
34+
}
35+
36+
public void apply(Statement statement) {
37+
bindings.forEach(sb -> sb.bindTo(statement));
38+
}
39+
40+
@RequiredArgsConstructor
41+
public static abstract class SingleBinding<T> {
42+
43+
final T identifier;
44+
final SettableValue value;
45+
46+
public abstract void bindTo(Statement statement);
47+
48+
public abstract boolean isIndexed();
49+
50+
public final boolean isNamed() {
51+
return !isIndexed();
52+
}
53+
}
54+
55+
56+
public static class IndexedSingleBinding extends SingleBinding<Integer> {
57+
58+
public IndexedSingleBinding(Integer identifier, SettableValue value) {
59+
super(identifier, value);
60+
}
61+
62+
@Override
63+
public void bindTo(Statement statement) {
64+
65+
if (value.isEmpty()) {
66+
statement.bindNull((int) identifier, value.getType());
67+
} else {
68+
statement.bind((int) identifier, value.getValue());
69+
}
70+
}
71+
72+
@Override
73+
public boolean isIndexed() {
74+
return true;
75+
}
76+
}
77+
78+
public static class NamedExpandedSingleBinding extends SingleBinding<String> {
79+
80+
private final BindableOperation operation;
81+
82+
public NamedExpandedSingleBinding(String identifier, SettableValue value, BindableOperation operation) {
83+
84+
super(identifier, value);
85+
86+
this.operation = operation;
87+
}
88+
89+
@Override
90+
public void bindTo(Statement statement) {
91+
92+
if (value != null) {
93+
operation.bind(statement, identifier, value);
94+
} else {
95+
operation.bindNull(statement, identifier, value.getType());
96+
}
97+
}
98+
99+
@Override
100+
public boolean isIndexed() {
101+
return false;
102+
}
103+
}
104+
}

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

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -331,40 +331,15 @@ protected String getSql() {
331331

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

334-
Function<Connection, Statement> executeFunction = it -> {
334+
PreparedOperation pop;
335335

336-
if (logger.isDebugEnabled()) {
337-
logger.debug("Executing SQL statement [" + sql + "]");
338-
}
339-
340-
if (sqlSupplier instanceof PreparedOperation<?>) {
341-
return ((PreparedOperation<?>) sqlSupplier).bind(it.createStatement(sql));
342-
}
343-
344-
BindableOperation operation = namedParameters.expand(sql, dataAccessStrategy.getBindMarkersFactory(),
345-
new MapBindParameterSource(byName));
346-
347-
if (logger.isTraceEnabled()) {
348-
logger.trace("Expanded SQL [" + operation.toQuery() + "]");
349-
}
350-
351-
Statement statement = it.createStatement(operation.toQuery());
352-
353-
byName.forEach((name, o) -> {
354-
355-
if (o.getValue() != null) {
356-
operation.bind(statement, name, o.getValue());
357-
} else {
358-
operation.bindNull(statement, name, o.getType());
359-
}
360-
});
361-
362-
bindByIndex(statement, byIndex);
363-
364-
return statement;
365-
};
336+
if (sqlSupplier instanceof PreparedOperation<?>) {
337+
pop = ((PreparedOperation<?>) sqlSupplier);
338+
} else {
339+
pop = new ExpandedPreparedOperation(sql, namedParameters, dataAccessStrategy, byName, byIndex);
340+
}
366341

367-
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(executeFunction.apply(it).execute());
342+
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(pop.createBoundStatement(it).execute());
368343

369344
return new DefaultSqlResult<>(DefaultDatabaseClient.this, //
370345
sql, //
@@ -907,20 +882,10 @@ private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunctio
907882
byName.forEach(it::bind);
908883
});
909884

910-
String sql = operation.toQuery();
911-
Function<Connection, Statement> insertFunction = it -> {
912-
913-
if (logger.isDebugEnabled()) {
914-
logger.debug("Executing SQL statement [" + sql + "]");
915-
}
916-
917-
return operation.bind(it.createStatement(sql));
918-
};
919-
920-
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(insertFunction.apply(it).execute());
885+
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(operation.createBoundStatement(it).execute());
921886

922887
return new DefaultSqlResult<>(DefaultDatabaseClient.this, //
923-
sql, //
888+
operation.toQuery(), //
924889
resultFunction, //
925890
it -> resultFunction.apply(it).flatMap(Result::getRowsUpdated).next(), //
926891
mappingFunction);
@@ -1028,21 +993,10 @@ private <MR> FetchSpec<MR> exchange(Object toInsert, BiFunction<Row, RowMetadata
1028993
});
1029994
});
1030995

1031-
String sql = operation.toQuery();
1032-
1033-
Function<Connection, Statement> insertFunction = it -> {
1034-
1035-
if (logger.isDebugEnabled()) {
1036-
logger.debug("Executing SQL statement [" + sql + "]");
1037-
}
1038-
1039-
return operation.bind(it.createStatement(sql));
1040-
};
1041-
1042-
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(insertFunction.apply(it).execute());
996+
Function<Connection, Flux<Result>> resultFunction = it -> Flux.from(operation.createBoundStatement(it).execute());
1043997

1044998
return new DefaultSqlResult<>(DefaultDatabaseClient.this, //
1045-
sql, //
999+
operation.toQuery(), //
10461000
resultFunction, //
10471001
it -> resultFunction //
10481002
.apply(it) //

0 commit comments

Comments
 (0)