diff options
Diffstat (limited to 'common/src/main/kotlin/DB.kt')
-rw-r--r-- | common/src/main/kotlin/DB.kt | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/common/src/main/kotlin/DB.kt b/common/src/main/kotlin/DB.kt index 2fa25ebc..b44e1fcd 100644 --- a/common/src/main/kotlin/DB.kt +++ b/common/src/main/kotlin/DB.kt @@ -139,11 +139,13 @@ fun <R> PgConnection.transaction(lambda: (PgConnection) -> R): R { fun <T> PreparedStatement.oneOrNull(lambda: (ResultSet) -> T): T? { executeQuery().use { - if (!it.next()) return null - return lambda(it) + return if (it.next()) lambda(it) else null } } +fun <T> PreparedStatement.one(lambda: (ResultSet) -> T): T = + requireNotNull(oneOrNull(lambda)) { "Missing result to database query" } + fun <T> PreparedStatement.all(lambda: (ResultSet) -> T): List<T> { executeQuery().use { val ret = mutableListOf<T>() |