Skip to content

Commit

Permalink
Add support for batch operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMarkyMarkus committed Sep 17, 2023
1 parent 7bf520f commit 5dc0388
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.Map;

import java.util.Arrays;

import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.Parameters;
import io.r2dbc.spi.Result;
Expand Down Expand Up @@ -136,6 +138,51 @@ public void executeInsertWithRecords() {
.verifyComplete();
}

@Test
public void executeBatchInsert() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);

databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)")
.bind("id", 42055)
.bind("name", "SCHAUFELRADBAGGER")
.bindNull("manual", Integer.class)
.add()
.bind("id", 2021)
.bind("name", "TOM")
.bindNull("manual", Integer.class)
.fetch().rowsUpdated()
.as(StepVerifier::create)
.expectNextMatches(updatedRows -> updatedRows.equals(2))
.verifyComplete();

databaseClient.sql("SELECT id FROM legoset")
.map(row -> row.get("id"))
.all()
.as(StepVerifier::create)
.assertNext(actual -> {
assertThat(actual).isInstanceOf(Number.class);
assertThat(((Number) actual).intValue()).isEqualTo(2021);
})
.expectNextCount(1)
.verifyComplete();
}

@Test
public void shouldThrowIllegalArgumentException() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);

databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:my_list)")
.bind("my_list", Arrays.asList(1, "Bob", 1))
.add()
.bind("my_list", Arrays.asList(2, "Alice", 1, "next"))
.fetch().rowsUpdated()
.as(StepVerifier::create)
.expectErrorSatisfies(exception -> assertThat(exception)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("not the same"))
.verify();
}

@Test
public void shouldTranslateDuplicateKeyException() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ void executeShouldBindValues() {
verify(statement).bind("$1", Parameters.in("foo"));
}

@Test
void executeBatchShouldBindValues() {
Statement statement = mockStatementFor("INSERT INTO table VALUES ($1)");

DatabaseClient databaseClient = databaseClientBuilder.build();

databaseClient.sql("INSERT INTO table VALUES ($1)")
.bind(0, Parameter.from("foo"))
.add()
.bind(0, Parameter.from("bar"))
.then().as(StepVerifier::create).verifyComplete();

verify(statement).bind(0, "foo");
verify(statement).bind(0, "bar");

databaseClient.sql("INSERT INTO table VALUES ($1)")
.bind("$1", "foo")
.add()
.bind("$1", "bar")
.then().as(StepVerifier::create).verifyComplete();

verify(statement).bind("$1", "foo");
verify(statement).bind("$1", "bar");
}

@Test
void executeShouldBindNamedValuesByIndex() {
Statement statement = mockStatementFor("SELECT * FROM table WHERE key = $1");
Expand Down

0 comments on commit 5dc0388

Please sign in to comment.