add db interaction layer
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-04-04 23:21:17 +02:00
parent 97b5923c1e
commit af4307c6a4
11 changed files with 158 additions and 425 deletions

View File

@@ -0,0 +1,46 @@
package com.kmalbz.database
import com.typesafe.config.ConfigFactory
import com.zaxxer.hikari.*
import io.ktor.config.HoconApplicationConfig
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
object DatabaseFactory {
@KtorExperimentalAPI
private val appConfig = HoconApplicationConfig(ConfigFactory.load())
@KtorExperimentalAPI
private val dbUrl = appConfig.property("db.jdbcUrl").getString()
@KtorExperimentalAPI
private val dbUser = appConfig.property("db.dbUser").getString()
@KtorExperimentalAPI
private val dbPassword = appConfig.property("db.dbPassword").getString()
@KtorExperimentalAPI
fun init() {
Database.connect(hikari())
}
@KtorExperimentalAPI
private fun hikari(): HikariDataSource {
val config = HikariConfig()
config.driverClassName = "org.postgresql.Driver"
config.jdbcUrl = dbUrl
config.username = dbUser
config.password = dbPassword
config.maximumPoolSize = 3
config.isAutoCommit = false
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
config.validate()
return HikariDataSource(config)
}
suspend fun <T> dbQuery(block: () -> T): T =
withContext(Dispatchers.IO) {
transaction { block() }
}
}

View File

@@ -0,0 +1,14 @@
package com.kmalbz.database.dao
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.date
import java.time.LocalDate
object ResultObjects : Table() {
val id: Column<Int> = integer("id").autoIncrement()
val tag: Column<String> = varchar("tag",32)
val date: Column<LocalDate> = date("date")
val decision: Column<Boolean> = bool("decision")
val confidence: Column<Double> = double("confidence")
override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id")
}

View File

@@ -0,0 +1,11 @@
package com.kmalbz.database.model
import java.time.LocalDate
data class ResultObject(
val id: Int,
val tag: String,
val date: LocalDate,
val decision: Boolean,
val confidence: Double
)

View File

@@ -0,0 +1,57 @@
package com.kmalbz.database.service
import com.kmalbz.database.DatabaseFactory.dbQuery
import com.kmalbz.database.model.ResultObject
import com.kmalbz.database.dao.ResultObjects
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import java.time.LocalDate
class ResultObjectService {
suspend fun getAllResultObjects(): List<ResultObject> = dbQuery {
ResultObjects.selectAll().map { toResultObject(it) }
}
suspend fun getResultObjectbyTag(tag: String): ResultObject? = dbQuery {
ResultObjects.select {
(ResultObjects.tag eq tag)
}.mapNotNull { toResultObject(it) }
.singleOrNull()
}
suspend fun getResultObjectbyDate(date: LocalDate): List<ResultObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date eq date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjectbeforeDate(date: LocalDate): List<ResultObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date less date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjectafterDate(date: LocalDate): List<ResultObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date greater date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjectbyDecision(decision: Boolean): List<ResultObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.decision eq decision)
}.mapNotNull { toResultObject(it) }
}
private fun toResultObject(row: ResultRow): ResultObject =
ResultObject(
id = row[ResultObjects.id],
tag = row[ResultObjects.tag],
date = row[ResultObjects.date],
decision = row[ResultObjects.decision],
confidence = row[ResultObjects.confidence]
)
}