diff options
Diffstat (limited to 'common/src')
-rw-r--r-- | common/src/main/kotlin/DB.kt | 6 | ||||
-rw-r--r-- | common/src/main/kotlin/helpers.kt | 2 |
2 files changed, 5 insertions, 3 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>() diff --git a/common/src/main/kotlin/helpers.kt b/common/src/main/kotlin/helpers.kt index cd803f64..2eba2d16 100644 --- a/common/src/main/kotlin/helpers.kt +++ b/common/src/main/kotlin/helpers.kt @@ -63,7 +63,7 @@ fun ByteArray.encodeBase64(): String = Base64.getEncoder().encodeToString(this) /* ----- InputStream ----- */ /** Unzip an input stream and run [lambda] over each entry */ -fun InputStream.unzipEach(lambda: (String, InputStream) -> Unit) { +inline fun InputStream.unzipEach(lambda: (String, InputStream) -> Unit) { ZipInputStream(this).use { zip -> while (true) { val entry = zip.getNextEntry() |