Batch Operations
Batch insert, update, and delete operations for improved performance
Batch queries are supported (only JDBC for now) for insert, update, and delete actions as well as all of the features they support (e.g. returning, excluding, etc.).
val people = listOf(
Person(id = 0, name = "Joe", age = 33, companyId = 123),
Person(id = 0, name = "Jim", age = 44, companyId = 456),
Person(id = 0, name = "Jack", age = 55, companyId = 789)
)
val q =
sql { p ->
sql.batch(people) { p ->
insert<Person> {
set(
name to param(p.name),
age to param(p.age),
companyId to (p.companyId)
)
}
}
}
q.buildFor.Postgres().runOn(myDatabase)
//> INSERT INTO Person (name, age, companyId) VALUES (?, ?, ?)
This will tell the JDBC driver to use a PreparedStatement with .addBatch() and executeBatch() between which every insert will be executed. Batch queries for update and delete work the same way.