-
-
Save bjonnh/4b2b706e61a9ed367eaee788a64e371b to your computer and use it in GitHub Desktop.
JDBI with kotlin using generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface DAO { | |
abstract fun list(): List<DbCitation> | |
abstract fun listlimited(@Bind("limit") limit: Int): List<DbCitation> | |
abstract fun count(): Int | |
} | |
abstract class Db<T: DAO>(var host: String, | |
var user: String, | |
var password: String, | |
var db: String) { | |
private var dao: T? = null | |
private var jdbi: Jdbi? = null | |
protected abstract var parser: JSONParser | |
fun connect() { | |
this.jdbi = | |
Jdbi.create("jdbc:postgresql://$host/$db", | |
user, | |
password) | |
.installPlugin(PostgresPlugin()) | |
.installPlugin(SqlObjectPlugin()) | |
.installPlugin(KotlinPlugin()) | |
.installPlugin(KotlinSqlObjectPlugin()) | |
} | |
fun dao(): T? { | |
if (dao == null) | |
this.dao = this.jdbi?.onDemand<T>(T::class.java) | |
return this.dao | |
} | |
fun listlimited(limit: Int): List<GenericCitation?>? { | |
return this.dao()?.listlimited(limit)?.map { it -> this.parser.parse(it.data)} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment