implement SelectOne and multiple

add Fixtures
This commit is contained in:
2019-06-04 13:28:38 +02:00
parent c5d4bfb07a
commit 81e46735df
5 changed files with 69 additions and 17 deletions

View File

@@ -26,7 +26,7 @@ class Connection(
return connection
}
inline fun <T, reified R :EntityI<T?>> execute(sql: String, values: List<Any?> = emptyList()): R? {
inline fun <T, reified R : EntityI<T?>> selectOne(sql: String, values: List<Any?> = emptyList()): R? {
val future = connect().sendPreparedStatement(sql, values)
val json = future.get().rows[0].getString(0)
if (json === null) {
@@ -37,4 +37,16 @@ class Connection(
return obj
}
}
inline fun <T, reified R : List<EntityI<T?>>> select(sql: String, values: List<Any?> = emptyList()): R {
val future = connect().sendPreparedStatement(sql, values)
val json = future.get().rows[0].getString(0)
if (json === null) {
return listOf<EntityI<T?>>() as R
} else {
val obj = serializer.deserializeList<R>(json)
return obj
}
}
}

View File

@@ -33,13 +33,15 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
}
inline fun <T, reified E : EntityI<T?>> deserialize(json: String): E {
val unserialized = this.mapper.readValue<E>(json)
return collection.get(unserialized.id) ?: unserialized
return this.mapper.readValue<E>(json)
}
inline fun <T, reified E : EntityI<T?>> deserialize(json: String, target: E): E {
val unserialized = mapper.readerForUpdating(target).readValue<E>(json)
return collection.get(unserialized.id) ?: unserialized
inline fun <reified E> deserializeList(json: String): E {
return mapper.readValue(json)
}
fun <T, E : EntityI<T?>> deserialize(json: String, target: E): E {
return mapper.readerForUpdating(target).readValue<E>(json)
}
}