SQL batch
Assume you want to batch insert 3 records
AuthorsRecord rec1 = new AuthorsRecord().setName("zero88").setCountry("VN");
AuthorsRecord rec2 = new AuthorsRecord().setName("jooq").setCountry("CH");
AuthorsRecord rec3 = new AuthorsRecord().setName("vertx");
BindBatchValues bindBatchValues = new BindBatchValues().register(Tables.AUTHORS.NAME)
.registerValue(Tables.AUTHORS.COUNTRY, "FR")
.add(rec1, rec2, rec3);
assert bindBatchValues.size() == 3;
Then simply use batch
jooqx.batch(dsl -> dsl.insertInto(Tables.AUTHORS).set(bindBatchValues.getDummyValues()), bindBatchValues)
.onSuccess(result -> {
final BatchResult batchResult = result;
assert batchResult.getTotal() == 3;
assert batchResult.getSuccesses() == 3;
});
If you want to get batch result, use batchResult
jooqx.batchResult(dsl -> dsl.insertInto(Tables.AUTHORS).set(bindBatchValues.getDummyValues()).returning(),
bindBatchValues, DSLAdapter.fetchJsonRecords(Tables.AUTHORS))
.onSuccess(batchReturningResult -> {
BatchReturningResult<JsonRecord<AuthorsRecord>> result = batchReturningResult;
List<JsonRecord<AuthorsRecord>> authorRecords = result.getRecords();
assert result.getTotal() == 3;
assert result.getSuccesses() == 3;
String data = authorRecords.stream()
.map(JsonRecord::toJson)
.map(JsonObject::encode)
.collect(Collectors.joining(",\n", "[", "]"));
assert Objects.equals(data, """
[{"id":1,"name":"zero88","country":"VN"},
{"id":2,"name":"jooq","country":"CH"},
{"id":3,"name":"vertx","country":"FR"}]
""");
})
.onFailure(Throwable::printStackTrace);