package moe.kageru.kodeshare.persistence import moe.kageru.kodeshare.Paste import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction import org.mariadb.jdbc.MariaDbDataSource object PasteDao { fun select(id: Long): Transient? = transaction { PasteTable.select { PasteTable.id.eq(id) }.firstOrNull() }?.toPaste() fun insert(paste: Paste): Transient = transaction { val id = PasteTable.insert { it[content] = paste.content it[html] = paste.html }[PasteTable.id] Transient(id, paste) } init { val source = MariaDbDataSource().apply { userName = "kodeshare" setPassword("12345") databaseName = "kode" } Database.connect(source) transaction { SchemaUtils.drop(PasteTable) SchemaUtils.create(PasteTable) } } } private fun ResultRow.toPaste() = Transient( get(PasteTable.id), Paste( content = get(PasteTable.content), html = get(PasteTable.html) ) ) private object PasteTable : Table() { val id = long("id").primaryKey().autoIncrement() val content = text("content") val html = text("html").nullable() } /* * Better have that one generic class * after deciding to make everything else * not generic because it would be overkill */ data class Transient(val id: Long, val data: DATA)