Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#226] Fix batch API usage when binding indexes or nulls #229

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/io/r2dbc/h2/H2Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public H2Statement add() {
@Override
public H2Statement bind(String name, Object value) {
Assert.requireNonNull(name, "name must not be null");
this.bindings.open = false;
return addIndex(getIndex(name), value);
}

Expand All @@ -92,6 +91,7 @@ public H2Statement bindNull(String name, Class<?> type) {

@Override
public H2Statement bindNull(int index, @Nullable Class<?> type) {
this.bindings.open = false;
this.bindings.getCurrent().add(index, this.codecs.encodeNull(type));

return this;
Expand Down Expand Up @@ -130,6 +130,7 @@ Binding getCurrentBinding() {
private H2Statement addIndex(int index, Object value) {
Assert.requireNonNull(value, "value must not be null");

this.bindings.open = false;
this.bindings.getCurrent().add(index, this.codecs.encode(value));

return this;
Expand Down
29 changes: 20 additions & 9 deletions src/test/java/io/r2dbc/h2/H2StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static io.r2dbc.h2.H2ConnectionFactoryProvider.H2_DRIVER;
import static io.r2dbc.h2.H2ConnectionFactoryProvider.URL;
Expand Down Expand Up @@ -162,30 +165,38 @@ void constructorNoSql() {

@Test
void execute() {
CommandInterface command1 = mock(CommandInterface.class);
CommandInterface command2 = mock(CommandInterface.class);
List<CommandInterface> commands = IntStream.range(0, 5).mapToObj(i -> mock(CommandInterface.class)).collect(Collectors.toList());
when(this.client.prepareCommand("select test-query-$1 from my_table", Arrays.asList(
new Binding().add(0, ValueInteger.get(100)),
new Binding().add(0, ValueInteger.get(200))
))).thenReturn(Arrays.asList(command1, command2).iterator());
when(command1.isQuery()).thenReturn(true);
when(command2.isQuery()).thenReturn(true);
when(this.client.query(command1)).thenReturn(new LocalResult());
when(this.client.query(command2)).thenReturn(new LocalResult());
new Binding().add(0, ValueInteger.get(200)),
new Binding().add(0, ValueInteger.get(300)),
new Binding().add(0, ValueNull.INSTANCE),
new Binding().add(0, ValueNull.INSTANCE)
))).thenReturn(commands.iterator());
commands.forEach(c -> when(c.isQuery()).thenReturn(true));
commands.forEach(c -> when(this.client.query(c)).thenReturn(new LocalResult()));

MockCodecs codecs = MockCodecs.builder()
.encoding(100, ValueInteger.get(100))
.encoding(200, ValueInteger.get(200))
.encoding(300, ValueInteger.get(300))
.encoding(Integer.class, ValueNull.INSTANCE)
.build();

new H2Statement(this.client, codecs, "select test-query-$1 from my_table")
.add()
.bind("$1", 100)
.add()
.bind("$1", 200)
.add()
.bind(0,300)
.add()
.bindNull("$1", Integer.class)
.add()
.bindNull(0, Integer.class)
.execute()
.as(StepVerifier::create)
.expectNextCount(2)
.expectNextCount(5)
.verifyComplete();
}

Expand Down