From 5dc03888554a6aef779a6ba468dba068d8fcd2d1 Mon Sep 17 00:00:00 2001 From: MarkMarkyMarkus Date: Thu, 29 Jul 2021 20:01:37 +0300 Subject: [PATCH] Add support for batch operations --- ...bstractDatabaseClientIntegrationTests.java | 47 +++++++++++++++++++ .../core/DefaultDatabaseClientUnitTests.java | 25 ++++++++++ 2 files changed, 72 insertions(+) diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/AbstractDatabaseClientIntegrationTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/AbstractDatabaseClientIntegrationTests.java index d25e21cb8b4c..43a021b95b97 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/AbstractDatabaseClientIntegrationTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/AbstractDatabaseClientIntegrationTests.java @@ -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; @@ -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); diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/DefaultDatabaseClientUnitTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/DefaultDatabaseClientUnitTests.java index 5afcb3670c00..15390d2be6e0 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/DefaultDatabaseClientUnitTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/core/DefaultDatabaseClientUnitTests.java @@ -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");