Database schema rewrote to SERDHA
This commit is contained in:
parent
dc108dcc55
commit
bba9b6a43f
@ -5,5 +5,8 @@ include(":impl")
|
|||||||
|
|
||||||
include(":telegram-api-impl")
|
include(":telegram-api-impl")
|
||||||
include(":storage-jdbc-sqlite")
|
include(":storage-jdbc-sqlite")
|
||||||
|
includeBuild("./storage-serdha/serdha-0-api")
|
||||||
|
include(":storage-serdha")
|
||||||
|
include(":storage-serdha:schema")
|
||||||
|
|
||||||
include(":exe")
|
include(":exe")
|
34
storage-serdha/schema/build.gradle.kts
Normal file
34
storage-serdha/schema/build.gradle.kts
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<Module> {
|
||||||
|
override fun createModuleTemplates(context: ModuleTemplate.VersionsInitializer.Scope) =
|
||||||
|
Module(context)
|
||||||
|
}
|
||||||
|
}
|
@ -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<COUNT>) : Reducer.Action<COUNT> {
|
||||||
|
val C_Counter = context.outputColumn("counter", context.types.U64, 0u)
|
||||||
|
|
||||||
|
override fun calculate(acc: OutputRow<COUNT>, row: OutputRow<COUNT>, newAcc: InputRow<COUNT>) {
|
||||||
|
newAcc[this.C_Counter] = acc[this.C_Counter] + 1u
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor : Reducer.Constructor<COUNT> {
|
||||||
|
override fun createReducer(context: Reducer.Constructor.Scope<COUNT>) =
|
||||||
|
COUNT(context)
|
||||||
|
}
|
||||||
|
}
|
@ -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<HAS_ROWS>) : Reducer.Action<HAS_ROWS> {
|
||||||
|
val C_Flag = context.outputColumn("flag", context.types.BOOLEAN, false)
|
||||||
|
|
||||||
|
override fun calculate(acc: OutputRow<HAS_ROWS>, row: OutputRow<HAS_ROWS>, newAcc: InputRow<HAS_ROWS>) {
|
||||||
|
newAcc[this.C_Flag] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor : Reducer.Constructor<HAS_ROWS> {
|
||||||
|
override fun createReducer(context: Reducer.Constructor.Scope<HAS_ROWS>) =
|
||||||
|
HAS_ROWS(context)
|
||||||
|
}
|
||||||
|
}
|
@ -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<Long, ULong, U64>(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)
|
||||||
|
|
||||||
|
}
|
@ -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<String, UByteArray, DatabaseType.BINARY_DATA>(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)
|
||||||
|
|
||||||
|
}
|
@ -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<ULong, UByteArray, DatabaseType.BINARY_DATA>(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)
|
||||||
|
|
||||||
|
}
|
@ -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<UByte, UByteArray, DatabaseType.BINARY_DATA>(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)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
These files exist only while SERDHA API is testing and there is no library that provides common functionality
|
@ -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 <qUE : Any, tUE : Any> SelectQuery.Folding.Constructor.Scope<qUE, tUE>.COUNT() =
|
||||||
|
this.foldRows(this.createReducer(COUNT)) { linker -> linker.link(linker.rUE.C_Counter) }
|
||||||
|
|
||||||
|
internal inline fun <qUE : Any, tUE : Any> SelectQuery.Folding.Constructor.Scope<qUE, tUE>.HAS_ROWS() =
|
||||||
|
this.foldRows(this.createReducer(HAS_ROWS)) { linker -> linker.link(linker.rUE.C_Flag) }
|
@ -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 <R> _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
|
||||||
|
|
||||||
|
}
|
@ -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)
|
@ -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<M_V0> {
|
||||||
|
override fun createSchema(context: CreateModuleScope) = M_V0(context)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_Chats>) {
|
||||||
|
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<T_Chats> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_Chats>) =
|
||||||
|
T_Chats(context)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_Moderators>,
|
||||||
|
usersTable: Table<T_Users, *>, chatsTable: Table<T_Chats, *>
|
||||||
|
) {
|
||||||
|
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<T_Users, *>,
|
||||||
|
private val chatsTable: Table<T_Chats, *>,
|
||||||
|
) : TableConstructor<T_Moderators> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_Moderators>) =
|
||||||
|
T_Moderators(context, this.usersTable, this.chatsTable)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor {
|
||||||
|
operator fun invoke(usersTable: Table<T_Users, *>, chatsTable: Table<T_Chats, *>): TableConstructor<T_Moderators> =
|
||||||
|
_Constructor(usersTable, chatsTable)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_QueueSets>, chatsTable: Table<T_Chats, *>) {
|
||||||
|
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<T_Chats, *>) : TableConstructor<T_QueueSets> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_QueueSets>) =
|
||||||
|
T_QueueSets(context, this.chatsTable)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor {
|
||||||
|
operator fun invoke(chatsTable: Table<T_Chats, *>): TableConstructor<T_QueueSets> =
|
||||||
|
_Constructor(chatsTable)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_Queues>, queueSetsTable: Table<T_QueueSets, *>) {
|
||||||
|
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<T_QueueSets, *>) : TableConstructor<T_Queues> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_Queues>) =
|
||||||
|
T_Queues(context, this.queueSetsTable)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor {
|
||||||
|
operator fun invoke(queueSetsTable: Table<T_QueueSets, *>): TableConstructor<T_Queues> =
|
||||||
|
_Constructor(queueSetsTable)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_User2Queue>,
|
||||||
|
usersTable: Table<T_Users, *>, queueSetsTable: Table<T_QueueSets, *>, queuesTable: Table<T_Queues, *>
|
||||||
|
) {
|
||||||
|
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<T_QueueSets, *>
|
||||||
|
) : RowExpression.Constructor<T_User2Queue, CheckQueueRefs._RowExports, Boolean> {
|
||||||
|
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<T_QueueSets, *>))
|
||||||
|
val C_HasQueries = context.returnColumn("has_queues", context.HAS_ROWS())
|
||||||
|
|
||||||
|
|
||||||
|
private class _Constructor(queueSetsTable: Table<T_QueueSets, *>) : SelectQuery.Folding.Constructor<_SelectQueuesCount, T_QueueSets> {
|
||||||
|
override val source: _Selectable<T_QueueSets> = queueSetsTable
|
||||||
|
|
||||||
|
override fun createSelectWithFold(context: SelectQuery.Folding.Constructor.Scope<_SelectQueuesCount, T_QueueSets>) =
|
||||||
|
_SelectQueuesCount(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor {
|
||||||
|
operator fun invoke(queueSetsTable: Table<T_QueueSets, *>) =
|
||||||
|
_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<T_Users, *>,
|
||||||
|
private val queueSetsTable: Table<T_QueueSets, *>,
|
||||||
|
private val queuesTable: Table<T_Queues, *>
|
||||||
|
) : TableConstructor<T_User2Queue> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_User2Queue>) =
|
||||||
|
T_User2Queue(context, this.usersTable, this.queueSetsTable, this.queuesTable)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object Constructor {
|
||||||
|
operator fun invoke(usersTable: Table<T_Users, *>, queueSetsTable: Table<T_QueueSets, *>, queuesTable: Table<T_Queues, *>): TableConstructor<T_User2Queue> =
|
||||||
|
_Constructor(usersTable, queueSetsTable, queuesTable)
|
||||||
|
}
|
||||||
|
}
|
@ -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<T_Users>) {
|
||||||
|
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<T_Users> {
|
||||||
|
override fun createTable(context: TableConstructor.Scope<T_Users>) =
|
||||||
|
T_Users(context)
|
||||||
|
}
|
||||||
|
}
|
1
storage-serdha/serdha-0-api
Submodule
1
storage-serdha/serdha-0-api
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit bb25b2be726a75fb6ee8bc792475ee64bcbae1c9
|
Loading…
Reference in New Issue
Block a user