[history] Extensions on prepared statements to execute query and obtain result

This commit is contained in:
Andrew Golovashevich 2024-08-19 20:13:00 +03:00
parent b75b9ef415
commit 2cee34c427

View File

@ -4,12 +4,15 @@
package ru.landgrafhomyak.db.jdbc_kotlin_extensions package ru.landgrafhomyak.db.jdbc_kotlin_extensions
import org.intellij.lang.annotations.Language
import java.sql.Connection import java.sql.Connection
import java.sql.PreparedStatement import java.sql.PreparedStatement
import java.sql.ResultSet
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
public fun <R> Connection.mapQuery(sql: String, action: PreparedStatementPrepareAndFetch<R>, to: MutableList<R>) { public fun <R, C : MutableCollection<R>> Connection.mapQuery(sql: String, action: PreparedStatementPrepareAndFetch<R>, to: C): C {
this.prepareStatement(sql).use { ps -> this.prepareStatement(sql).use { ps ->
action.prepare(ps) action.prepare(ps)
ps.executeQuery().use { rs -> ps.executeQuery().use { rs ->
@ -18,6 +21,7 @@ public fun <R> Connection.mapQuery(sql: String, action: PreparedStatementPrepare
} }
} }
} }
return to
} }
public fun <R> Connection.mapQuery(sql: String, action: PreparedStatementPrepareAndFetch<R>): List<R> { public fun <R> Connection.mapQuery(sql: String, action: PreparedStatementPrepareAndFetch<R>): List<R> {
@ -28,7 +32,7 @@ public fun <R> Connection.mapQuery(sql: String, action: PreparedStatementPrepare
public inline fun <R> Connection.transaction(t: (Connection) -> R): R { public inline fun <R> Connection.transaction(t: (Connection) -> R): R {
contract { contract {
callsInPlace(t) callsInPlace(t, InvocationKind.EXACTLY_ONCE)
} }
this.prepareStatement("BEGIN TRANSACTION").use { ps -> ps.executeUpdate() } this.prepareStatement("BEGIN TRANSACTION").use { ps -> ps.executeUpdate() }
@ -48,9 +52,32 @@ public inline fun <R> Connection.transaction(t: (Connection) -> R): R {
} }
public inline fun <R> Connection.prepareStatement(sql: String, action: (PreparedStatement) -> R): R { public inline fun <R> Connection.prepareStatement(@Language("SQL") sql: String, action: (PreparedStatement) -> R): R {
contract { contract {
callsInPlace(action) callsInPlace(action, InvocationKind.EXACTLY_ONCE)
} }
this.prepareStatement(sql).use { ps -> return action(ps) } this.prepareStatement(sql/*.trim(' ', '\n', '\t')*/).use { ps -> return action(ps) }
}
public inline fun <R, C : MutableCollection<R>> ResultSet.mapTo(to: C, transform: (rs: ResultSet) -> R): C {
while (this.next()) {
to.add(transform(this))
}
return to
}
public inline fun <R> ResultSet.map(transform: (rs: ResultSet) -> R): List<R> =
this.mapTo(ArrayList(), transform)
public inline fun <R, C : MutableCollection<R>> ResultSet.mapToThenClose(to: C, transform: (rs: ResultSet) -> R): C =
this.use { rs -> rs.mapTo(to, transform) }
public inline fun <R> ResultSet.mapThenClose(transform: (rs: ResultSet) -> R): List<R> =
this.use { rs -> rs.map(transform) }
public inline fun <R> PreparedStatement.executeQuery(action: (ResultSet) -> R): R {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}
return this.executeQuery().use(action)
} }