2019-09-19 22:04:23 +02:00
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 < Paste > ? = transaction {
PasteTable . select { PasteTable . id . eq ( id ) } . firstOrNull ( )
} ?. toPaste ( )
fun insert ( paste : Paste ) : Transient < Paste > = transaction {
val id = PasteTable . insert {
it [ content ] = paste . content
it [ html ] = paste . html
} [ PasteTable . id ]
Transient ( id , paste )
}
init {
val source = MariaDbDataSource ( ) . apply {
2019-09-22 18:26:28 +02:00
userName = " kodeshare "
2019-09-19 22:04:23 +02:00
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 < DATA > ( val id : Long , val data : DATA )