This commit is contained in:
InsanusMokrassar 2022-11-08 12:47:59 +06:00
parent 9cbca864e3
commit d56eb6c867
10 changed files with 21 additions and 20 deletions

View File

@ -137,9 +137,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun deleteById(ids: List<IdType>) { override suspend fun deleteById(ids: List<IdType>) {
onBeforeDelete(ids) onBeforeDelete(ids)
transaction(db = database) { transaction(db = database) {
val deleted = deleteWhere(null, null) { val deleted = deleteWhere(null, null) { selectByIds(it, ids) }
selectByIds(ids)
}
if (deleted == ids.size) { if (deleted == ids.size) {
ids ids
} else { } else {

View File

@ -4,8 +4,8 @@ import org.jetbrains.exposed.sql.*
interface CommonExposedRepo<IdType, ObjectType> : ExposedRepo { interface CommonExposedRepo<IdType, ObjectType> : ExposedRepo {
val ResultRow.asObject: ObjectType val ResultRow.asObject: ObjectType
val selectById: SqlExpressionBuilder.(IdType) -> Op<Boolean> val selectById: ISqlExpressionBuilder.(IdType) -> Op<Boolean>
val selectByIds: SqlExpressionBuilder.(List<IdType>) -> Op<Boolean> val selectByIds: ISqlExpressionBuilder.(List<IdType>) -> Op<Boolean>
get() = { list -> get() = { list ->
if (list.isEmpty()) { if (list.isEmpty()) {
Op.FALSE Op.FALSE

View File

@ -59,9 +59,9 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
override suspend fun unset(toUnset: List<Key>) { override suspend fun unset(toUnset: List<Key>) {
transaction(database) { transaction(database) {
toUnset.mapNotNull { toUnset.mapNotNull { item ->
if (deleteWhere { selectById(it) } > 0) { if (deleteWhere { selectById(it, item) } > 0) {
it item
} else { } else {
null null
} }
@ -75,7 +75,7 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
transaction(database) { transaction(database) {
toUnset.flatMap { toUnset.flatMap {
val keys = select { selectByValue(it) }.mapNotNull { it.asKey } val keys = select { selectByValue(it) }.mapNotNull { it.asKey }
deleteWhere { selectByIds(keys) } deleteWhere { selectByIds(it, keys) }
keys keys
} }
}.distinct().forEach { }.distinct().forEach {

View File

@ -16,7 +16,7 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
Table(tableName ?: "") { Table(tableName ?: "") {
abstract val keyColumn: Column<*> abstract val keyColumn: Column<*>
abstract val ResultRow.asKey: Key abstract val ResultRow.asKey: Key
abstract val selectByValue: SqlExpressionBuilder.(Value) -> Op<Boolean> abstract val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean>
override suspend fun get(k: Key): Value? = transaction(database) { override suspend fun get(k: Key): Value? = transaction(database) {
select { selectById(k) }.limit(1).firstOrNull() ?.asObject select { selectById(k) }.limit(1).firstOrNull() ?.asObject

View File

@ -4,6 +4,8 @@ import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
open class ExposedKeyValueRepo<Key, Value>( open class ExposedKeyValueRepo<Key, Value>(
@ -47,9 +49,9 @@ open class ExposedKeyValueRepo<Key, Value>(
override suspend fun unset(toUnset: List<Key>) { override suspend fun unset(toUnset: List<Key>) {
transaction(database) { transaction(database) {
toUnset.mapNotNull { toUnset.mapNotNull { item ->
if (deleteWhere { keyColumn.eq(it) } > 0) { if (deleteWhere { keyColumn.eq(item) } > 0) {
it item
} else { } else {
null null
} }

View File

@ -20,10 +20,10 @@ open class ExposedReadKeyValueRepo<Key, Value>(
val valueColumn: Column<Value> = valueColumnAllocator() val valueColumn: Column<Value> = valueColumnAllocator()
override val ResultRow.asKey: Key override val ResultRow.asKey: Key
get() = get(keyColumn) get() = get(keyColumn)
override val selectByValue: SqlExpressionBuilder.(Value) -> Op<Boolean> = { valueColumn.eq(it) } override val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean> = { valueColumn.eq(it) }
override val ResultRow.asObject: Value override val ResultRow.asObject: Value
get() = get(valueColumn) get() = get(valueColumn)
override val selectById: SqlExpressionBuilder.(Key) -> Op<Boolean> = { keyColumn.eq(it) } override val selectById: ISqlExpressionBuilder.(Key) -> Op<Boolean> = { keyColumn.eq(it) }
override val primaryKey: Table.PrimaryKey override val primaryKey: Table.PrimaryKey
get() = PrimaryKey(keyColumn, valueColumn) get() = PrimaryKey(keyColumn, valueColumn)

View File

@ -49,7 +49,7 @@ abstract class AbstractExposedKeyValuesRepo<Key, Value>(
transaction(database) { transaction(database) {
toRemove.keys.flatMap { k -> toRemove.keys.flatMap { k ->
toRemove[k] ?.mapNotNull { v -> toRemove[k] ?.mapNotNull { v ->
if (deleteWhere { selectById(k).and(selectByValue(v)) } > 0 ) { if (deleteWhere { selectById(it, k).and(SqlExpressionBuilder.selectByValue(v)) } > 0 ) {
k to v k to v
} else { } else {
null null
@ -63,7 +63,7 @@ abstract class AbstractExposedKeyValuesRepo<Key, Value>(
override suspend fun clear(k: Key) { override suspend fun clear(k: Key) {
transaction(database) { transaction(database) {
deleteWhere { selectById(k) } deleteWhere { selectById(it, k) }
}.also { _onDataCleared.emit(k) } }.also { _onDataCleared.emit(k) }
} }
} }

View File

@ -17,7 +17,7 @@ abstract class AbstractExposedReadKeyValuesRepo<Key, Value>(
Table(tableName ?: "") { Table(tableName ?: "") {
abstract val keyColumn: Column<*> abstract val keyColumn: Column<*>
abstract val ResultRow.asKey: Key abstract val ResultRow.asKey: Key
abstract val selectByValue: SqlExpressionBuilder.(Value) -> Op<Boolean> abstract val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean>
override suspend fun count(k: Key): Long = transaction(database) { select { selectById(k) }.count() } override suspend fun count(k: Key): Long = transaction(database) { select { selectById(k) }.count() }

View File

@ -4,6 +4,7 @@ import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
typealias ExposedOneToManyKeyValueRepo1<Key, Value> = ExposedKeyValuesRepo<Key, Value> typealias ExposedOneToManyKeyValueRepo1<Key, Value> = ExposedKeyValuesRepo<Key, Value>

View File

@ -15,10 +15,10 @@ open class ExposedReadKeyValuesRepo<Key, Value>(
override val keyColumn: Column<Key> = keyColumnAllocator() override val keyColumn: Column<Key> = keyColumnAllocator()
override val ResultRow.asKey: Key override val ResultRow.asKey: Key
get() = get(keyColumn) get() = get(keyColumn)
override val selectByValue: SqlExpressionBuilder.(Value) -> Op<Boolean> = { valueColumn.eq(it) } override val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean> = { valueColumn.eq(it) }
override val ResultRow.asObject: Value override val ResultRow.asObject: Value
get() = get(valueColumn) get() = get(valueColumn)
override val selectById: SqlExpressionBuilder.(Key) -> Op<Boolean> = { keyColumn.eq(it) } override val selectById: ISqlExpressionBuilder.(Key) -> Op<Boolean> = { keyColumn.eq(it) }
val valueColumn: Column<Value> = valueColumnAllocator() val valueColumn: Column<Value> = valueColumnAllocator()
init { initTable() } init { initTable() }