Batchlight

batchlight / co.yello.db.batchlight / BatchStatement / execute

execute

fun execute(itemsToInsert: Collection<T>, bindItemFunction: BatchBinder.(T) -> Unit): Unit

Chunks the list into the maximum sized list that can be inserted at a time, then executes the insert with the bindItemFunction passed into the class.

Parameters

itemsToInsert - The collection of items of type T to batch insert into the database.

bindItemFunction -

A function that performs the binds for each field in the T passed to the function. The function is an extension on BatchBinder so all bind functions can be accessed from within.

When binding the order of the bind calls should match the structure of the table inserting.

Example usage:

A simple class would look something like

data class BatchClass(val id: Int, val text: String, val num: Int)

The SQL Create statement for the corresponding table to that object would be:

CREATE TABLE BatchClass (
     id INTEGER,
     text TEXT,
     num INTEGER
)

The Binder generated for this table will will have the params (?,?,?). To correctly bind the three values to their correct ? in the prepared statement the function would be:

{ batchObject - >
     bindLong(batchObject.id)
     bindString(batchObject.text)
     bindLong(batchObject.num)
}