Files
Suwayomi-Server/server/src/main/kotlin/suwayomi/tachidesk/global/impl/GlobalMeta.kt
renovate[bot] c117d380a3 Update exposed to v1 (major) (#1868)
* Update exposed to v1

* Update Exposed

* Add Kotlinx.DateTime extensions

* Update H2

* Review comments

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Syer10 <syer10@users.noreply.github.com>
2026-05-12 12:53:41 -04:00

63 lines
2.2 KiB
Kotlin

package suwayomi.tachidesk.global.impl
import org.jetbrains.exposed.v1.core.dao.id.EntityID
import org.jetbrains.exposed.v1.core.inList
import org.jetbrains.exposed.v1.core.statements.BatchUpdateStatement
import org.jetbrains.exposed.v1.jdbc.batchInsert
import org.jetbrains.exposed.v1.jdbc.selectAll
import org.jetbrains.exposed.v1.jdbc.statements.toExecutable
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
import suwayomi.tachidesk.global.model.table.GlobalMetaTable
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
object GlobalMeta {
fun modifyMeta(
key: String,
value: String,
) {
modifyMetas(mapOf(key to value))
}
fun modifyMetas(meta: Map<String, String>) {
transaction {
val dbMetaMap =
GlobalMetaTable
.selectAll()
.where { GlobalMetaTable.key inList meta.keys }
.associateBy { it[GlobalMetaTable.key] }
val (existingMeta, newMeta) = meta.toList().partition { (key) -> key in dbMetaMap.keys }
if (existingMeta.isNotEmpty()) {
BatchUpdateStatement(GlobalMetaTable)
.apply {
existingMeta.forEach { (key, value) ->
addBatch(EntityID(dbMetaMap[key]!![GlobalMetaTable.id].value, GlobalMetaTable))
this[GlobalMetaTable.value] = value
}
}.toExecutable()
.execute(this@transaction)
}
if (newMeta.isNotEmpty()) {
GlobalMetaTable.batchInsert(newMeta) { (key, value) ->
this[GlobalMetaTable.key] = key
this[GlobalMetaTable.value] = value
}
}
}
}
fun getMetaMap(): Map<String, String> =
transaction {
GlobalMetaTable
.selectAll()
.associate { it[GlobalMetaTable.key] to it[GlobalMetaTable.value] }
}
}