From bba9b6a43f0e7037d3ab79bfcacf70ec701de1df Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 2 Feb 2025 03:41:02 +0300 Subject: [PATCH] Database schema rewrote to SERDHA --- settings.gradle.kts | 5 +- storage-serdha/schema/build.gradle.kts | 34 +++++++ .../bgtu/db0/storage/serdha/schema/Module.kt | 13 +++ .../db0/storage/serdha/schema/_temp/COUNT.kt | 19 ++++ .../storage/serdha/schema/_temp/HAS_ROWS.kt | 19 ++++ .../db0/storage/serdha/schema/_temp/S64.kt | 18 ++++ .../db0/storage/serdha/schema/_temp/STRING.kt | 19 ++++ .../db0/storage/serdha/schema/_temp/U64.kt | 39 ++++++++ .../db0/storage/serdha/schema/_temp/U8.kt | 21 ++++ .../db0/storage/serdha/schema/_temp/readme.md | 1 + .../storage/serdha/schema/_temp/reducers.kt | 12 +++ .../schema/_temp/transaction_methods.kt | 63 ++++++++++++ .../serdha/schema/_temp/virtual_types.kt | 13 +++ .../bgtu/db0/storage/serdha/schema/v0/M_V0.kt | 18 ++++ .../db0/storage/serdha/schema/v0/T_Chats.kt | 18 ++++ .../storage/serdha/schema/v0/T_Moderators.kt | 40 ++++++++ .../storage/serdha/schema/v0/T_QueueSets.kt | 30 ++++++ .../db0/storage/serdha/schema/v0/T_Queues.kt | 29 ++++++ .../storage/serdha/schema/v0/T_User2Queue.kt | 96 +++++++++++++++++++ .../db0/storage/serdha/schema/v0/T_Users.kt | 20 ++++ storage-serdha/serdha-0-api | 1 + 21 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 storage-serdha/schema/build.gradle.kts create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/Module.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/COUNT.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/HAS_ROWS.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/S64.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/STRING.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U64.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U8.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/readme.md create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/reducers.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/transaction_methods.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/virtual_types.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/M_V0.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Chats.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Moderators.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_QueueSets.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Queues.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_User2Queue.kt create mode 100644 storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Users.kt create mode 160000 storage-serdha/serdha-0-api diff --git a/settings.gradle.kts b/settings.gradle.kts index fbe70a5..08ee950 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -5,5 +5,8 @@ include(":impl") include(":telegram-api-impl") include(":storage-jdbc-sqlite") +includeBuild("./storage-serdha/serdha-0-api") +include(":storage-serdha") +include(":storage-serdha:schema") -include(":exe") +include(":exe") \ No newline at end of file diff --git a/storage-serdha/schema/build.gradle.kts b/storage-serdha/schema/build.gradle.kts new file mode 100644 index 0000000..7604b77 --- /dev/null +++ b/storage-serdha/schema/build.gradle.kts @@ -0,0 +1,34 @@ +import ru.landgrafhomyak.kotlin.kmp_gradle_build_helper.defineAllMultiplatformTargets + +buildscript { + repositories { + mavenCentral() + maven("https://maven.landgrafhomyak.ru/") + } + + dependencies { + classpath("ru.landgrafhomyak.kotlin:kotlin-mpp-gradle-build-helper:v0.2k2.0.20") + } +} + +plugins { + kotlin("multiplatform") version "2.0.20" +} + +repositories { + mavenCentral() + maven("https://maven.landgrafhomyak.ru/") +} + +kotlin { + jvmToolchain(21) + defineAllMultiplatformTargets() + + sourceSets { + commonMain { + dependencies { + compileOnly("ru.landgrafhomyak.db.serdha:serdha-0-api:0.0") + } + } + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/Module.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/Module.kt new file mode 100644 index 0000000..60aa9de --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/Module.kt @@ -0,0 +1,13 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0.M_V0 +import ru.landgrafhomyak.db.serdha0.api.module.ModuleTemplate + +class Module private constructor(context: ModuleTemplate.VersionsInitializer.Scope) { + val v0 = context.createTemplate("v0", M_V0) + + companion object Initializer : ModuleTemplate.VersionsInitializer { + override fun createModuleTemplates(context: ModuleTemplate.VersionsInitializer.Scope) = + Module(context) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/COUNT.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/COUNT.kt new file mode 100644 index 0000000..4047a27 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/COUNT.kt @@ -0,0 +1,19 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow + +@Suppress("PropertyName", "MemberVisibilityCanBePrivate") +class COUNT private constructor(context: Reducer.Constructor.Scope) : Reducer.Action { + val C_Counter = context.outputColumn("counter", context.types.U64, 0u) + + override fun calculate(acc: OutputRow, row: OutputRow, newAcc: InputRow) { + newAcc[this.C_Counter] = acc[this.C_Counter] + 1u + } + + companion object Constructor : Reducer.Constructor { + override fun createReducer(context: Reducer.Constructor.Scope) = + COUNT(context) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/HAS_ROWS.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/HAS_ROWS.kt new file mode 100644 index 0000000..9075444 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/HAS_ROWS.kt @@ -0,0 +1,19 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.Reducer +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class HAS_ROWS private constructor(context: Reducer.Constructor.Scope) : Reducer.Action { + val C_Flag = context.outputColumn("flag", context.types.BOOLEAN, false) + + override fun calculate(acc: OutputRow, row: OutputRow, newAcc: InputRow) { + newAcc[this.C_Flag] = true + } + + companion object Constructor : Reducer.Constructor { + override fun createReducer(context: Reducer.Constructor.Scope) = + HAS_ROWS(context) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/S64.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/S64.kt new file mode 100644 index 0000000..0455695 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/S64.kt @@ -0,0 +1,18 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType + +class S64(provider: DatabaseType.Provider) : + DatabaseType._VirtualType(provider.U64) { + + override val name: String get() = "INT_S64" + + override fun _unwrap(w: ULong): Long = w.toLong() + + override fun _wrap(w: Long): ULong = w.toULong() + + override fun _equals(l: Long, r: Long): Boolean = l == r + + override fun _compare(l: Long, r: Long): Int = l.compareTo(r) + +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/STRING.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/STRING.kt new file mode 100644 index 0000000..cdf84d6 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/STRING.kt @@ -0,0 +1,19 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType + +@OptIn(ExperimentalUnsignedTypes::class) +class STRING(provider: DatabaseType.Provider) : + DatabaseType._VirtualType(provider.BINARY_DATA) { + + override val name: String get() = "STRING" + + override fun _unwrap(w: UByteArray): String = w.asByteArray().decodeToString() + + override fun _wrap(w: String): UByteArray = w.encodeToByteArray().asUByteArray() + + override fun _equals(l: String, r: String): Boolean = l == r + + override fun _compare(l: String, r: String): Int = l.compareTo(r) + +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U64.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U64.kt new file mode 100644 index 0000000..9fa8e27 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U64.kt @@ -0,0 +1,39 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType + +@OptIn(ExperimentalUnsignedTypes::class) +class U64(provider: DatabaseType.Provider) : + DatabaseType._VirtualType(provider.BINARY_DATA(8u, true)) { + + override val name: String get() = "INT_U64" + + override fun _unwrap(w: UByteArray): ULong { + var result = w[0].toULong() shl 56 + result = result or (w[1].toULong() and 0xFFuL shl 48) + result = result or (w[2].toULong() and 0xFFuL shl 40) + result = result or (w[3].toULong() and 0xFFuL shl 32) + result = result or (w[4].toULong() and 0xFFuL shl 24) + result = result or (w[5].toULong() and 0xFFuL shl 16) + result = result or (w[6].toULong() and 0xFFuL shl 8) + return result or (w[7].toULong() and 0xFFuL) + } + + override fun _wrap(w: ULong): UByteArray { + val dst = UByteArray(8) + dst[0] = (w shr 56).toUByte() + dst[1] = (w shr 48).toUByte() + dst[2] = (w shr 40).toUByte() + dst[3] = (w shr 32).toUByte() + dst[4] = (w shr 24).toUByte() + dst[5] = (w shr 16).toUByte() + dst[6] = (w shr 8).toUByte() + dst[7] = w.toUByte() + return dst + } + + override fun _equals(l: ULong, r: ULong): Boolean = l == r + + override fun _compare(l: ULong, r: ULong): Int = l.compareTo(r) + +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U8.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U8.kt new file mode 100644 index 0000000..6698f10 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/U8.kt @@ -0,0 +1,21 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType + +@OptIn(ExperimentalUnsignedTypes::class) +class U8(provider: DatabaseType.Provider) : + DatabaseType._VirtualType(provider.BINARY_DATA(8u, true)) { + + override val name: String get() = "INT_U8" + + override fun _unwrap(w: UByteArray): UByte { + return w[0] + } + + override fun _wrap(w: UByte): UByteArray = ubyteArrayOf(w) + + override fun _equals(l: UByte, r: UByte): Boolean = l == r + + override fun _compare(l: UByte, r: UByte): Int = l.compareTo(r) + +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/readme.md b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/readme.md new file mode 100644 index 0000000..dcd44ad --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/readme.md @@ -0,0 +1 @@ +These files exist only while SERDHA API is testing and there is no library that provides common functionality \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/reducers.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/reducers.kt new file mode 100644 index 0000000..07bb839 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/reducers.kt @@ -0,0 +1,12 @@ +@file:JvmName("ReducersKt") +@file:Suppress("NOTHING_TO_INLINE", "FunctionName") + +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.queries.SelectQuery + +internal inline fun SelectQuery.Folding.Constructor.Scope.COUNT() = + this.foldRows(this.createReducer(COUNT)) { linker -> linker.link(linker.rUE.C_Counter) } + +internal inline fun SelectQuery.Folding.Constructor.Scope.HAS_ROWS() = + this.foldRows(this.createReducer(HAS_ROWS)) { linker -> linker.link(linker.rUE.C_Flag) } diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/transaction_methods.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/transaction_methods.kt new file mode 100644 index 0000000..c7983c3 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/transaction_methods.kt @@ -0,0 +1,63 @@ +@file:JvmName("TransactionMethodsKt") +@file:OptIn(ExperimentalContracts::class, LowLevelApi::class) +@file:Suppress("FunctionName") + +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.jvm.JvmName +import ru.landgrafhomyak.db.serdha0.api.LowLevelApi +import ru.landgrafhomyak.db.serdha0.api.queries._Query +import ru.landgrafhomyak.db.serdha0.api.runtime.InputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.Transaction + + +@PublishedApi +internal inline fun _safeAutoClose(onAbort: () -> Unit = {}, onSuccess: () -> Unit = {}, action: () -> R): R { + contract { + callsInPlace(action, InvocationKind.EXACTLY_ONCE) + callsInPlace(onAbort, InvocationKind.AT_MOST_ONCE) + callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE) + } + val ret: R + try { + ret = action() + } catch (e1: Throwable) { + try { + onAbort() + } catch (e2: Throwable) { + e1.addSuppressed(e2) + } + throw e1 + } + onSuccess() + return ret +} + +class ExpectedOneRowException() : Exception() +class TooManyRowsException() : Exception() + +suspend inline fun <_qUE : Any, R> Transaction.selectOneOrError( + compiledQuery: _Query.Params2Table<_qUE>, + params: (qUE: _qUE, i: InputRow<_qUE>) -> Unit, + result: (qUE: _qUE, o: OutputRow<_qUE>) -> R +): R { + val i = this._executeQuery(compiledQuery) + _safeAutoClose(onAbort = { i._abort() }) { + params(compiledQuery.uExt, i) + } + val o = i._finish() + val ret: R + _safeAutoClose(onAbort = { o._abort() }, onSuccess = { o._finish() }) { + if (!o._next()) + throw ExpectedOneRowException() + ret = result(compiledQuery.uExt, o) + if (o._next()) + throw TooManyRowsException() + } + return ret + +} diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/virtual_types.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/virtual_types.kt new file mode 100644 index 0000000..e0dde69 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/_temp/virtual_types.kt @@ -0,0 +1,13 @@ +@file:JvmName("VirtualTypesKt") + +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp + +import ru.landgrafhomyak.db.serdha0.api.misc.DatabaseType + +internal inline val DatabaseType.Provider.U8 get() = U8(this) + +internal inline val DatabaseType.Provider.U64 get() = U64(this) + +internal inline val DatabaseType.Provider.S64 get() = S64(this) + +internal inline val DatabaseType.Provider.STRING get() = STRING(this) \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/M_V0.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/M_V0.kt new file mode 100644 index 0000000..af4cb94 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/M_V0.kt @@ -0,0 +1,18 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.db.serdha0.api.module.CreateModuleScope +import ru.landgrafhomyak.db.serdha0.api.module.ModuleTemplate + +@Suppress("ClassName", "MemberVisibilityCanBePrivate") +class M_V0 private constructor(context: CreateModuleScope) { + val chats = context.createTable(context.rootNs, "chats", T_Chats) + val users = context.createTable(context.rootNs, "users", T_Users) + val queueSets = context.createTable(context.rootNs, "queue_sets", T_QueueSets(this.chats)) + val queues = context.createTable(context.rootNs, "queue", T_Queues(this.queueSets)) + val user2queue = context.createTable(context.rootNs, "user->queue", T_User2Queue(this.users, this.queueSets, this.queues)) + val moderators = context.createTable(context.rootNs, "moderators", T_Moderators(this.users, this.chats)) + + companion object Constructor : ModuleTemplate.ModuleConstructor { + override fun createSchema(context: CreateModuleScope) = M_V0(context) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Chats.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Chats.kt new file mode 100644 index 0000000..2c0bee0 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Chats.kt @@ -0,0 +1,18 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.S64 +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_Chats private constructor(context: TableConstructor.Scope) { + val C_RowId = context.rowId_column + val C_TgCid = context.column("telegram::chat_id", context.types.S64) + + val U_RowId = context.rowId_uniqueConstraint + val U_TgCid = context.uniqueIndex("telegram::chat_id", true, this.C_TgCid) + + companion object Constructor : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_Chats(context) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Moderators.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Moderators.kt new file mode 100644 index 0000000..2a3ce60 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Moderators.kt @@ -0,0 +1,40 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.HAS_ROWS +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.U8 +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.selectOneOrError +import ru.landgrafhomyak.db.serdha0.api.misc.RowExpression +import ru.landgrafhomyak.db.serdha0.api.misc._Selectable +import ru.landgrafhomyak.db.serdha0.api.queries.SelectQuery +import ru.landgrafhomyak.db.serdha0.api.queries._Query +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.Transaction +import ru.landgrafhomyak.db.serdha0.api.table.ForeignRowReference +import ru.landgrafhomyak.db.serdha0.api.table.Table +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_Moderators private constructor( + context: TableConstructor.Scope, + usersTable: Table, chatsTable: Table +) { + val C_User = context.foreignRowReference("user", usersTable, ForeignRowReference.OnDelete.CASCADE) + val C_Chat = context.foreignRowReference("chat", chatsTable, ForeignRowReference.OnDelete.CASCADE) + val C_Ordinal = context.column("can_create_queues", context.types.BOOLEAN) + val C_CanCloseQueues = context.column("can_close_queues", context.types.BOOLEAN) + + val U_UserInQueue = context.uniqueIndex("user_in_chat", true, this.C_User, this.C_Chat) + + private class _Constructor( + private val usersTable: Table, + private val chatsTable: Table, + ) : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_Moderators(context, this.usersTable, this.chatsTable) + } + + companion object Constructor { + operator fun invoke(usersTable: Table, chatsTable: Table): TableConstructor = + _Constructor(usersTable, chatsTable) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_QueueSets.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_QueueSets.kt new file mode 100644 index 0000000..715a14d --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_QueueSets.kt @@ -0,0 +1,30 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.U64 +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.STRING +import ru.landgrafhomyak.db.serdha0.api.table.ForeignRowReference +import ru.landgrafhomyak.db.serdha0.api.table.Table +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_QueueSets private constructor(creator: TableConstructor.Scope, chatsTable: Table) { + val C_RowId = creator.rowId_column + val C_Chat = creator.foreignRowReference("chat", chatsTable, ForeignRowReference.OnDelete.CASCADE) + val C_TgMid = creator.column("telegram::message_id", creator.types.U64) + val C_IsOpen = creator.column("is_open", creator.types.BOOLEAN) + val C_Name = creator.column("name", creator.types.STRING) + + val U_RowId = creator.rowId_uniqueConstraint + val U_TgMid = creator.uniqueIndex("telegram::message_id", true, this.C_Chat, this.C_TgMid) + + private class _Constructor(private val chatsTable: Table) : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_QueueSets(context, this.chatsTable) + + } + + companion object Constructor { + operator fun invoke(chatsTable: Table): TableConstructor = + _Constructor(chatsTable) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Queues.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Queues.kt new file mode 100644 index 0000000..b1cfd91 --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Queues.kt @@ -0,0 +1,29 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.U8 +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.STRING +import ru.landgrafhomyak.db.serdha0.api.table.ForeignRowReference +import ru.landgrafhomyak.db.serdha0.api.table.Table +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_Queues private constructor(creator: TableConstructor.Scope, queueSetsTable: Table) { + val C_RowId = creator.rowId_column + val C_QueueSet = creator.foreignRowReference("chat", queueSetsTable, ForeignRowReference.OnDelete.CASCADE) + val C_Name = creator.column("name", creator.types.STRING) + val C_Ordinal = creator.column("ordinal", creator.types.U8) + + val U_RowId = creator.rowId_uniqueConstraint + val U_Id = creator.uniqueIndex("id", true, this.C_QueueSet, this.C_Ordinal) + + private class _Constructor(private val queueSetsTable: Table) : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_Queues(context, this.queueSetsTable) + + } + + companion object Constructor { + operator fun invoke(queueSetsTable: Table): TableConstructor = + _Constructor(queueSetsTable) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_User2Queue.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_User2Queue.kt new file mode 100644 index 0000000..379d57f --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_User2Queue.kt @@ -0,0 +1,96 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.HAS_ROWS +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.U8 +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.selectOneOrError +import ru.landgrafhomyak.db.serdha0.api.misc.RowExpression +import ru.landgrafhomyak.db.serdha0.api.misc._Selectable +import ru.landgrafhomyak.db.serdha0.api.queries.SelectQuery +import ru.landgrafhomyak.db.serdha0.api.queries._Query +import ru.landgrafhomyak.db.serdha0.api.runtime.OutputRow +import ru.landgrafhomyak.db.serdha0.api.runtime.Transaction +import ru.landgrafhomyak.db.serdha0.api.table.ForeignRowReference +import ru.landgrafhomyak.db.serdha0.api.table.Table +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_User2Queue private constructor( + context: TableConstructor.Scope, + usersTable: Table, queueSetsTable: Table, queuesTable: Table +) { + val C_User = context.foreignRowReference("user", usersTable, ForeignRowReference.OnDelete.CASCADE) + val C_QueueSet = context.foreignRowReference("queue_set", queueSetsTable, ForeignRowReference.OnDelete.CASCADE) + val C_Queue = context.foreignRowReference("queue", context.types.nullableOf(context.types.ROW_ID(queuesTable)), queuesTable, ForeignRowReference.OnDelete.CASCADE) + val C_Ordinal = context.column("ordinal", context.types.U8) + + val U_UserInQueue = context.uniqueIndex("user_in_queue", true, this.C_User, this.C_QueueSet) + val U_QueueInSet = context.uniqueIndex("queue_in_set", true, this.C_QueueSet, this.C_Queue, this.C_Ordinal) + + val H_QueueRef = context.checkConstraint("queue_ref", CheckQueueRefs(queueSetsTable)) + + private class CheckQueueRefs( + private val queueSetsTable: Table + ) : RowExpression.Constructor { + override val rowExports = _RowExports + + override fun createRowExpression(internalQueries: _Query.Constructor._SelectsScope, runtimeQueries: _Query.Constructor._SelectsScope): RowExpression.Action<_RowExports, Boolean> { + val selectQueuesCount = runtimeQueries.selectFolding(_SelectQueuesCount(this.queueSetsTable)) + return _Action(selectQueuesCount) + } + + private class _RowExports(context: RowExpression.RowExportsConstructor.Scope<_RowExports, T_User2Queue>) { + val C_QueueSet = context.selectFromTargetTable(context.tt.uExt.C_QueueSet) + val C_Queue = context.selectFromTargetTable(context.tt.uExt.C_Queue) + + companion object Constructor : RowExpression.RowExportsConstructor<_RowExports, T_User2Queue> { + override fun createRowExports(context: RowExpression.RowExportsConstructor.Scope<_RowExports, T_User2Queue>) = + _RowExports(context) + } + } + + private class _SelectQueuesCount private constructor(context: SelectQuery.Folding.Constructor.Scope<_SelectQueuesCount, T_QueueSets>) { + val P_QueueSetId = context.queryParam("queue_set_id", context.types.ROW_ID(context.src as Table)) + val C_HasQueries = context.returnColumn("has_queues", context.HAS_ROWS()) + + + private class _Constructor(queueSetsTable: Table) : SelectQuery.Folding.Constructor<_SelectQueuesCount, T_QueueSets> { + override val source: _Selectable = queueSetsTable + + override fun createSelectWithFold(context: SelectQuery.Folding.Constructor.Scope<_SelectQueuesCount, T_QueueSets>) = + _SelectQueuesCount(context) + } + + companion object Constructor { + operator fun invoke(queueSetsTable: Table) = + _Constructor(queueSetsTable) + } + } + + private class _Action(private val selectQueuesCount: SelectQuery.Folding<_SelectQueuesCount>) : RowExpression.Action<_RowExports, Boolean> { + override suspend fun calculateRow(rowUE: _RowExports, row: OutputRow<_RowExports>, transaction: Transaction): Boolean { + val hasRows: Boolean = transaction.selectOneOrError( + this.selectQueuesCount, + params = { qUE, i -> i[qUE.P_QueueSetId] = row[rowUE.C_QueueSet] }, + result = { qUE, o -> o[qUE.C_HasQueries] } + ) + + return hasRows == (row[rowUE.C_Queue] == null) + } + } + } + + private class _Constructor( + private val usersTable: Table, + private val queueSetsTable: Table, + private val queuesTable: Table + ) : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_User2Queue(context, this.usersTable, this.queueSetsTable, this.queuesTable) + + } + + companion object Constructor { + operator fun invoke(usersTable: Table, queueSetsTable: Table, queuesTable: Table): TableConstructor = + _Constructor(usersTable, queueSetsTable, queuesTable) + } +} \ No newline at end of file diff --git a/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Users.kt b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Users.kt new file mode 100644 index 0000000..d92ac9e --- /dev/null +++ b/storage-serdha/schema/src/jvmMain/kotlin/ru/landgrafhomyak/bgtu/db0/storage/serdha/schema/v0/T_Users.kt @@ -0,0 +1,20 @@ +package ru.landgrafhomyak.bgtu.db0.storage.serdha.schema.v0 + +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.STRING +import ru.landgrafhomyak.bgtu.db0.storage.serdha.schema._temp.U64 +import ru.landgrafhomyak.db.serdha0.api.table.TableConstructor + +@Suppress("ClassName", "PropertyName", "MemberVisibilityCanBePrivate") +class T_Users private constructor(context: TableConstructor.Scope) { + val C_RowId = context.rowId_column + val C_TgUid = context.column("telegram::user_id", context.types.U64) + val C_DisplayName = context.column("display_name", context.types.STRING) + + val U_RowId = context.rowId_uniqueConstraint + val U_TgUid = context.uniqueIndex("telegram::user_id", true, this.C_TgUid) + + companion object Constructor : TableConstructor { + override fun createTable(context: TableConstructor.Scope) = + T_Users(context) + } +} \ No newline at end of file diff --git a/storage-serdha/serdha-0-api b/storage-serdha/serdha-0-api new file mode 160000 index 0000000..bb25b2b --- /dev/null +++ b/storage-serdha/serdha-0-api @@ -0,0 +1 @@ +Subproject commit bb25b2be726a75fb6ee8bc792475ee64bcbae1c9