singleUpdate

@ExperimentalCoroutinesApi
fun Jdbc.singleUpdate(sql: String, prepare: suspend PreparedStatement.() -> Unit = {}): Flow<Int>

Executes a single SQL update using a JDBC connection.

This function takes an SQL statement sql and an optional prepare function, which prepares the statement before execution.

Return

A Flow of the number of rows affected by the update.

Example:

val jdbc: Jdbc = ...
val sql = "UPDATE users SET active = 1 WHERE id = 42"

jdbc.singleUpdate(sql)
.collect { rowsAffected ->
println("Rows affected: $rowsAffected")
}

Parameters

sql

The SQL statement to execute.

prepare

A suspend function that prepares the statement before execution.


@ExperimentalCoroutinesApi
fun <T> Jdbc.singleUpdate(sql: String, upstream: Flow<T>, concurrency: Int = 1, prepare: suspend PreparedStatement.(T) -> Unit = {}): Flow<Int>

Executes a single SQL update using a JDBC connection for each item in the upstream flow.

This function takes an SQL statement sql, an upstream flow, concurrency, and an optional prepare function, which prepares the statement before execution.

Return

A Flow of the number of rows affected by the update for each item.

Example:

val jdbc: Jdbc = ...
val sql = "UPDATE users SET active = 1 WHERE id = ?"
val userIds = flowOf(1, 2, 3, 4, 5)

jdbc.singleUpdate(sql, userIds, prepare = { id ->
setInt(1, id)
}).collect { rowsAffected ->
println("Rows affected: $rowsAffected")
}

Parameters

sql

The SQL statement to execute.

upstream

A Flow of items to process.

concurrency

The level of concurrency for executing the updates.

prepare

A suspend function that prepares the statement for each item before execution.