Add Serializable Parameter

This commit is contained in:
2019-10-22 11:10:02 +02:00
parent 8eeb1e9e9d
commit dbf5fbcfc5
7 changed files with 81 additions and 23 deletions

View File

@@ -7,6 +7,7 @@ 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.entity.EntityI
import fr.postgresjson.entity.Serializable
import fr.postgresjson.serializer.Serializer
import fr.postgresjson.utils.LoggerDelegate
import org.slf4j.Logger
@@ -174,8 +175,9 @@ class Connection(
select(sql, object : TypeReference<List<R>>() {}, values, block)
override fun exec(sql: String, values: List<Any?>): QueryResult {
return stopwatchQuery(sql, values) {
connect().sendPreparedStatement(sql, compileArgs(values)).join()
val compiledValues = compileArgs(values)
return stopwatchQuery(sql, compiledValues) {
connect().sendPreparedStatement(sql, compiledValues).join()
}
}
@@ -186,8 +188,9 @@ class Connection(
}
override fun sendQuery(sql: String, values: List<Any?>): Int {
return stopwatchQuery(sql, values) {
replaceArgsIntoSql(sql, compileArgs(values)) {
val compiledValues = compileArgs(values)
return stopwatchQuery(sql, compiledValues) {
replaceArgsIntoSql(sql, compiledValues) {
connect().sendQuery(it).join().rowsAffected.toInt()
}
}
@@ -201,7 +204,7 @@ class Connection(
private fun compileArgs(values: List<Any?>): List<Any?> {
return values.map {
if (it is EntityI) {
if (it is Serializable) {
serializer.serialize(it)
} else {
it

View File

@@ -3,8 +3,9 @@ package fr.postgresjson.entity
import org.joda.time.DateTime
import java.util.*
/* ID */
interface EntityI
interface Serializable
interface EntityI : Serializable
interface Parameter : Serializable
abstract class Entity<T>(open var id: T? = null) : EntityI
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID>(id)

View File

@@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import fr.postgresjson.entity.EntityI
import fr.postgresjson.entity.Serializable
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
init {
@@ -22,16 +22,16 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
fun serialize(source: EntityI, pretty: Boolean = false): String {
fun serialize(source: Any, pretty: Boolean = false): String {
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
else mapper.writeValueAsString(source)
}
fun <E : EntityI> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
fun <E> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
return this.mapper.readValue(json, valueTypeRef)
}
inline fun <reified E : EntityI> deserialize(json: String): E? {
inline fun <reified E> deserialize(json: String): E? {
return this.mapper.readValue(json)
}
@@ -43,11 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
return deserializeList(json, object : TypeReference<E>() {})
}
fun <E : EntityI> deserialize(json: String, target: E): E {
fun <E> deserialize(json: String, target: E): E {
return mapper.readerForUpdating(target).readValue<E>(json)
}
}
fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
inline fun <reified E : EntityI> E.deserialize(json: String) = Serializer().deserialize(json, this)
inline fun <reified E : EntityI> String.deserialize() = Serializer().deserialize<E>(this)
fun Serializable.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
inline fun <reified E : Serializable> E.deserialize(json: String) = Serializer().deserialize(json, this)
inline fun <reified E : Serializable> String.deserialize() = Serializer().deserialize<E>(this)