From f6eb6941b9c9ba2317b4e24b8caff2285d845934 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 28 Apr 2022 10:33:52 +0200 Subject: [PATCH] [#226] Fix batch API usage when binding indexes or nulls Signed-off-by: Lukas Eder --- src/main/java/io/r2dbc/h2/H2Statement.java | 3 +- .../java/io/r2dbc/h2/H2StatementTest.java | 29 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/r2dbc/h2/H2Statement.java b/src/main/java/io/r2dbc/h2/H2Statement.java index ab9080c..10efae2 100644 --- a/src/main/java/io/r2dbc/h2/H2Statement.java +++ b/src/main/java/io/r2dbc/h2/H2Statement.java @@ -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); } @@ -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; @@ -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; diff --git a/src/test/java/io/r2dbc/h2/H2StatementTest.java b/src/test/java/io/r2dbc/h2/H2StatementTest.java index 83293b2..7e92799 100644 --- a/src/test/java/io/r2dbc/h2/H2StatementTest.java +++ b/src/test/java/io/r2dbc/h2/H2StatementTest.java @@ -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; @@ -162,20 +165,22 @@ void constructorNoSql() { @Test void execute() { - CommandInterface command1 = mock(CommandInterface.class); - CommandInterface command2 = mock(CommandInterface.class); + List 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") @@ -183,9 +188,15 @@ void execute() { .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(); }