From 039aed27475c363d751c05b5570e4cff9c696296 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 8 Sep 2023 02:59:53 +0600 Subject: [PATCH] improvements in ExposedReadKeyValuesRepo --- CHANGELOG.md | 4 ++++ ...epo.kt => AbstractExposedKeyValuesRepo.kt} | 0 ...kt => AbstractExposedReadKeyValuesRepo.kt} | 24 +++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{AbstractExposedKeyValueRepo.kt => AbstractExposedKeyValuesRepo.kt} (100%) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{AbstractExposedReadKeyValueRepo.kt => AbstractExposedReadKeyValuesRepo.kt} (75%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d9f6b6d1b1..e55282c550d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.20.6 +* `Repos`: + * `Exposed` + * Fixes in exposed key-values repos + ## 0.20.5 * `Coroutines`: diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt similarity index 100% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedKeyValuesRepo.kt diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt similarity index 75% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt index 15fa501e579..52e959f77db 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadKeyValuesRepo.kt @@ -1,12 +1,10 @@ package dev.inmo.micro_utils.repos.exposed.onetomany import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.exposed.* import dev.inmo.micro_utils.repos.exposed.utils.selectPaginated import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.transaction abstract class AbstractExposedReadKeyValuesRepo( @@ -73,4 +71,26 @@ abstract class AbstractExposedReadKeyValuesRepo( override suspend fun contains(k: Key, v: Value): Boolean = transaction(database) { select { selectById(k).and(selectByValue(v)) }.limit(1).any() } + + override suspend fun getAll(reverseLists: Boolean): Map> = transaction(database) { + val query = if (reverseLists) { + selectAll().orderBy(keyColumn, SortOrder.DESC) + } else { + selectAll() + } + query.asSequence().map { it.asKey to it.asObject }.groupBy { it.first }.mapValues { + it.value.map { it.second } + } + } + + override suspend fun getAll(k: Key, reverseLists: Boolean): List = transaction(database) { + val query = if (reverseLists) { + select { selectById(k) }.orderBy(keyColumn, SortOrder.DESC) + } else { + select { selectById(k) } + } + query.map { + it.asObject + } + } }