Skip to content

Instantly share code, notes, and snippets.

@bjonnh
Created April 29, 2018 04:24
Show Gist options
  • Save bjonnh/4b2b706e61a9ed367eaee788a64e371b to your computer and use it in GitHub Desktop.
Save bjonnh/4b2b706e61a9ed367eaee788a64e371b to your computer and use it in GitHub Desktop.
JDBI with kotlin using generics
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