eclipse-vertx / vertx-sql-client

High performance reactive SQL Client written in Java
Apache License 2.0
894 stars 200 forks source link

Pg doc: incorrect snippet for retrieval of batched insert with returning clause #1440

Closed tsegismont closed 5 months ago

tsegismont commented 5 months ago

As pointed out by @salmonb in #1439 , the snippet for retrieval of batched insert with returning clause is incorrect.

Instead of:

    client
      .preparedQuery("INSERT INTO color (color_name) VALUES ($1) RETURNING color_id")
      .executeBatch(Arrays.asList(Tuple.of("white"), Tuple.of("red"), Tuple.of("blue")))
      .onSuccess(res -> {
        for (RowSet<Row> rows = res;rows.next() != null;rows = rows.next()) {
          Integer colorId = rows.iterator().next().getInteger("color_id");
          System.out.println("generated key: " + colorId);
        }
      });

It should be:

    client
      .preparedQuery("INSERT INTO color (color_name) VALUES ($1) RETURNING color_id")
      .executeBatch(Arrays.asList(Tuple.of("white"), Tuple.of("red"), Tuple.of("blue")))
      .onSuccess(res -> {
        for (RowSet<Row> rows = res; rows != null; rows = rows.next()) {
          Integer colorId = rows.iterator().next().getInteger("color_id");
          System.out.println("generated key: " + colorId);
        }
      });

Otherwise, the last generated id will be missed.

tsegismont commented 5 months ago

Fixed by https://github.com/eclipse-vertx/vertx-sql-client/commit/d669e6074996d7a806706e7a228c825f2ec4e654