From 303d1b40e339aa051713db80ece33cb4c798141e Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 4 Oct 2023 23:28:08 +0100 Subject: [PATCH] feature: Better serializable column --- app/src/main/kotlin/database/Tables.kt | 44 ------------------- .../main/kotlin/database/entity/DocsEntity.kt | 27 ++++++++++++ .../database/exposed/SerializableColumn.kt | 8 +++- 3 files changed, 33 insertions(+), 46 deletions(-) delete mode 100644 app/src/main/kotlin/database/Tables.kt create mode 100644 app/src/main/kotlin/database/entity/DocsEntity.kt diff --git a/app/src/main/kotlin/database/Tables.kt b/app/src/main/kotlin/database/Tables.kt deleted file mode 100644 index 5187405..0000000 --- a/app/src/main/kotlin/database/Tables.kt +++ /dev/null @@ -1,44 +0,0 @@ -/** - * MIT License - * - * Copyright (c) 2019-2022 TriumphTeam and Contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package dev.triumphteam.docsly.database - -import org.jetbrains.exposed.dao.IntEntity -import org.jetbrains.exposed.dao.IntEntityClass -import org.jetbrains.exposed.dao.id.EntityID -import org.jetbrains.exposed.dao.id.IntIdTable -import org.jetbrains.exposed.sql.Column - -public object DocsTable : IntIdTable() { - public val name: Column = varchar("doc_name", 32).uniqueIndex() - public val version: Column = varchar("doc_version", 32).uniqueIndex() - public val guild: Column = long("guild_id").uniqueIndex() -} - -public class DocDao(id: EntityID) : IntEntity(id) { - public companion object : IntEntityClass(DocsTable) - - public var docId: EntityID by DocsTable.id - public var version: String by DocsTable.version - public var guild: Long by DocsTable.guild -} diff --git a/app/src/main/kotlin/database/entity/DocsEntity.kt b/app/src/main/kotlin/database/entity/DocsEntity.kt new file mode 100644 index 0000000..f870d01 --- /dev/null +++ b/app/src/main/kotlin/database/entity/DocsEntity.kt @@ -0,0 +1,27 @@ +package dev.triumphteam.docsly.database.entity + +import dev.triumphteam.docsly.database.exposed.serializable +import dev.triumphteam.docsly.serializable.DocElement +import org.jetbrains.exposed.dao.LongEntity +import org.jetbrains.exposed.dao.LongEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IdTable +import org.jetbrains.exposed.sql.Column + +public object DocsTable : IdTable() { + public val guild: Column = long("guild_id").uniqueIndex() + public val project: Column = text("project").uniqueIndex() + public val version: Column = text("version").uniqueIndex() + public val location: Column = text("location").uniqueIndex() + public val doc: Column = serializable("doc") + + override val primaryKey: PrimaryKey = PrimaryKey(guild, version, location) + override val id: Column> = guild.entityId() +} + +public class DocEntity(entityId: EntityID) : LongEntity(entityId) { + public companion object : LongEntityClass(DocsTable) + + public var version: String by DocsTable.version + public var guild: Long by DocsTable.guild +} diff --git a/app/src/main/kotlin/database/exposed/SerializableColumn.kt b/app/src/main/kotlin/database/exposed/SerializableColumn.kt index e1d921f..8725c8b 100644 --- a/app/src/main/kotlin/database/exposed/SerializableColumn.kt +++ b/app/src/main/kotlin/database/exposed/SerializableColumn.kt @@ -27,16 +27,20 @@ import com.impossibl.postgres.jdbc.PGArray import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.json.Json +import kotlinx.serialization.serializer import org.jetbrains.exposed.sql.Column import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.TextColumnType import java.sql.Clob import kotlin.reflect.KClass -private val json = Json +public val json: Json = Json public inline fun Table.serializable(name: String, serializer: KSerializer): Column = - registerColumn(name, SerializableColumnType(T::class, serializer)) + registerColumn(name, SerializableColumnType(T::class, serializer)) + +public inline fun Table.serializable(name: String): Column = + registerColumn(name, SerializableColumnType(T::class, json.serializersModule.serializer())) @Suppress("UNCHECKED_CAST") public class SerializableColumnType(