feature: serialize entity arg before send the request

This commit is contained in:
2019-06-17 14:44:30 +02:00
parent f960e4a66a
commit 797825966d
2 changed files with 22 additions and 2 deletions

View File

@@ -30,7 +30,7 @@ class Connection(
} }
fun <T, R : EntityI<T?>?> selectOne(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R? { fun <T, R : EntityI<T?>?> selectOne(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R? {
val future = connect().sendPreparedStatement(sql, values) val future = connect().sendPreparedStatement(sql, compileArgs(values))
val json = future.get().rows[0].getString(0) val json = future.get().rows[0].getString(0)
return if (json === null) { return if (json === null) {
null null
@@ -42,7 +42,7 @@ class Connection(
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? = selectOne(sql, object: TypeReference<R>() {}, values) 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 { fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R {
val future = connect().sendPreparedStatement(sql, values) val future = connect().sendPreparedStatement(sql, compileArgs(values))
val json = future.get().rows[0].getString(0) val json = future.get().rows[0].getString(0)
return if (json === null) { return if (json === null) {
listOf<EntityI<T?>?>() as R listOf<EntityI<T?>?>() as R
@@ -52,6 +52,16 @@ class Connection(
} }
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: List<Any?> = emptyList()): R = select(sql, object : TypeReference<R>() {}, values) 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
}
}
}
} }
class Requester ( class Requester (

View File

@@ -53,4 +53,14 @@ class ConnectionTest(): TestAbstract() {
assertNotNull(result) assertNotNull(result)
assertEquals("myName", result!!.name) assertEquals("myName", result!!.name)
} }
@Test
fun callRequestWithArgsEntity() {
val o = ObjTest("myName")
val obj: ObjTest? = connection.selectOne("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
assertTrue(obj !== null)
assertTrue(obj is ObjTest)
assertTrue(obj!!.id == 1)
assertTrue(obj.name == "myName")
}
} }