|
@@ -1,11 +1,17 @@
|
|
|
package benchmark.repository;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
import org.springframework.r2dbc.core.DatabaseClient;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import benchmark.model.Fortune;
|
|
|
import benchmark.model.World;
|
|
|
+import io.r2dbc.spi.Connection;
|
|
|
+import io.r2dbc.spi.ConnectionFactory;
|
|
|
+import io.r2dbc.spi.Result;
|
|
|
+import io.r2dbc.spi.Statement;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
@@ -23,28 +29,25 @@ public class R2dbcDbRepository implements DbRepository {
|
|
|
public Mono<World> getWorld(int id) {
|
|
|
return databaseClient
|
|
|
.sql("SELECT id, randomnumber FROM world WHERE id = $1")
|
|
|
- .bind("$1", id)
|
|
|
+ .bind(0, id)
|
|
|
.mapProperties(World.class)
|
|
|
.first();
|
|
|
}
|
|
|
|
|
|
- private Mono<World> updateWorld(World world) {
|
|
|
- return databaseClient
|
|
|
- .sql("UPDATE world SET randomnumber=$2 WHERE id = $1")
|
|
|
- .bind("$1", world.id)
|
|
|
- .bind("$2", world.randomnumber)
|
|
|
- .fetch()
|
|
|
- .rowsUpdated()
|
|
|
- .map(count -> world);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
@Override
|
|
|
- public Mono<World> findAndUpdateWorld(int id, int randomNumber) {
|
|
|
- return getWorld(id).flatMap(world -> {
|
|
|
- world.randomnumber = randomNumber;
|
|
|
- return updateWorld(world);
|
|
|
- });
|
|
|
+ public Mono<Void> updateWorlds(List<World> worlds) {
|
|
|
+ return databaseClient.inConnectionMany(con -> {
|
|
|
+ Statement statement = con.createStatement("UPDATE world SET randomnumber=$2 WHERE id = $1");
|
|
|
+ for (int i = 0; i < worlds.size(); i++) {
|
|
|
+ World world = worlds.get(i);
|
|
|
+ statement.bind(0, world.randomnumber)
|
|
|
+ .bind(1, world.id);
|
|
|
+ if (i < worlds.size() - 1) {
|
|
|
+ statement.add();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Flux.from(statement.execute());
|
|
|
+ }).flatMap(Result::getRowsUpdated).then();
|
|
|
}
|
|
|
|
|
|
@Override
|