64 lines
2.4 KiB
Kotlin
64 lines
2.4 KiB
Kotlin
package fr.postgresjson.connexion
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference
|
|
import com.github.jasync.sql.db.pool.ConnectionPool
|
|
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
|
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
|
import fr.postgresjson.Serializer
|
|
import fr.postgresjson.entity.EntityI
|
|
|
|
class Connection(
|
|
private val database: String,
|
|
private val username: String,
|
|
private val password: String,
|
|
private val host: String = "localhost",
|
|
private val port: Int = 5432
|
|
) {
|
|
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
|
private val serializer = Serializer()
|
|
|
|
fun connect(): ConnectionPool<PostgreSQLConnection> {
|
|
if (!::connection.isInitialized || !connection.isConnected()) {
|
|
connection = PostgreSQLConnectionBuilder.createConnectionPool(
|
|
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
|
|
)
|
|
}
|
|
return connection
|
|
}
|
|
|
|
fun <T, R : EntityI<T?>?> selectOne(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R? {
|
|
val future = connect().sendPreparedStatement(sql, compileArgs(values))
|
|
val json = future.get().rows[0].getString(0)
|
|
return if (json === null) {
|
|
null
|
|
} else {
|
|
serializer.deserialize(json, typeReference)
|
|
}
|
|
}
|
|
|
|
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? = selectOne(sql, object: TypeReference<R>() {}, values)
|
|
|
|
fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R {
|
|
val future = connect().sendPreparedStatement(sql, compileArgs(values))
|
|
val json = future.get().rows[0].getString(0)
|
|
return if (json === null) {
|
|
listOf<EntityI<T?>?>() as R
|
|
} else {
|
|
serializer.deserializeList(json, typeReference)
|
|
}
|
|
}
|
|
|
|
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: List<Any?> = emptyList()): R = select(sql, object : TypeReference<R>() {}, values)
|
|
|
|
private fun compileArgs(values: List<Any?>): List<Any?> {
|
|
return values.map {
|
|
if (it is EntityI<*>) {
|
|
serializer.serialize(it)
|
|
} else {
|
|
it
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|