From dbf5fbcfc529b3d986a408332ad91e08f815ab91 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 22 Oct 2019 11:10:02 +0200 Subject: [PATCH] Add Serializable Parameter --- .idea/runConfigurations/Check.xml | 7 +++++ .idea/runConfigurations/Lint.xml | 31 +++++++++++++++++++ .../fr/postgresjson/connexion/Connection.kt | 13 +++++--- .../kotlin/fr/postgresjson/entity/Entity.kt | 5 +-- .../fr/postgresjson/serializer/Serializer.kt | 16 +++++----- .../kotlin/fr/postgresjson/ConnectionTest.kt | 16 ++++++++++ .../kotlin/fr/postgresjson/RequesterTest.kt | 16 +++++----- 7 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 .idea/runConfigurations/Check.xml create mode 100644 .idea/runConfigurations/Lint.xml diff --git a/.idea/runConfigurations/Check.xml b/.idea/runConfigurations/Check.xml new file mode 100644 index 0000000..b8c0970 --- /dev/null +++ b/.idea/runConfigurations/Check.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Lint.xml b/.idea/runConfigurations/Lint.xml new file mode 100644 index 0000000..85db345 --- /dev/null +++ b/.idea/runConfigurations/Lint.xml @@ -0,0 +1,31 @@ + + + + + + + + + true + + + \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index b585075..37b1ea1 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -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>() {}, values, block) override fun exec(sql: String, values: List): 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): 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): List { return values.map { - if (it is EntityI) { + if (it is Serializable) { serializer.serialize(it) } else { it diff --git a/src/main/kotlin/fr/postgresjson/entity/Entity.kt b/src/main/kotlin/fr/postgresjson/entity/Entity.kt index ded2ccc..446b98a 100644 --- a/src/main/kotlin/fr/postgresjson/entity/Entity.kt +++ b/src/main/kotlin/fr/postgresjson/entity/Entity.kt @@ -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(open var id: T? = null) : EntityI open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity(id) diff --git a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt index 7ff6209..52a339d 100644 --- a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt +++ b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt @@ -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 deserialize(json: String, valueTypeRef: TypeReference): E { + fun deserialize(json: String, valueTypeRef: TypeReference): E { return this.mapper.readValue(json, valueTypeRef) } - inline fun deserialize(json: String): E? { + inline fun deserialize(json: String): E? { return this.mapper.readValue(json) } @@ -43,11 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { return deserializeList(json, object : TypeReference() {}) } - fun deserialize(json: String, target: E): E { + fun deserialize(json: String, target: E): E { return mapper.readerForUpdating(target).readValue(json) } } -fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty) -inline fun E.deserialize(json: String) = Serializer().deserialize(json, this) -inline fun String.deserialize() = Serializer().deserialize(this) \ No newline at end of file +fun Serializable.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty) +inline fun E.deserialize(json: String) = Serializer().deserialize(json, this) +inline fun String.deserialize() = Serializer().deserialize(this) \ No newline at end of file diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index 18df595..e9cc617 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -2,6 +2,7 @@ package fr.postgresjson import fr.postgresjson.connexion.Paginated import fr.postgresjson.entity.IdEntity +import fr.postgresjson.entity.Parameter import org.junit.Assert.* import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test @@ -12,6 +13,8 @@ class ConnectionTest() : TestAbstract() { private class ObjTest(var name: String) : IdEntity() private class ObjTest2(var title: String, var test: ObjTest?) : IdEntity() private class ObjTest3(var first: String, var seconde: String, var third: Int) : IdEntity() + private class ObjTestWithParameterObject(var first: ParameterObject, var seconde: ParameterObject) : IdEntity() + private class ParameterObject(var third: String) : Parameter @Test fun getObject() { @@ -81,6 +84,19 @@ class ConnectionTest() : TestAbstract() { assertEquals(result.third, 123) } + @Test + fun `select one with named parameters object`() { + val result: ObjTestWithParameterObject? = connection.selectOne( + "SELECT json_build_object('first', :first::json, 'seconde', :seconde::json)", + mapOf( + "first" to ParameterObject("one"), + "seconde" to ParameterObject("two") + ) + ) + assertEquals("one", result!!.first.third) + assertEquals("two", result.seconde.third) + } + @Test fun `select with named parameters`() { val params: Map = mapOf( diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 2e3c3aa..de72950 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -133,10 +133,10 @@ class RequesterTest : TestAbstract() { .getQuery("Test/selectPaginated") .select(1, 2, mapOf("name" to "ff")) Assert.assertNotNull(result) - Assert.assertEquals(result.result[0].name, "ff") - Assert.assertEquals(result.result[1].name, "ff-2") - Assert.assertEquals(result.total, 10) - Assert.assertEquals(result.offset, 0) + Assert.assertEquals("ff", result.result[0].name) + Assert.assertEquals("ff-2", result.result[1].name) + Assert.assertEquals(10, result.total) + Assert.assertEquals(0, result.offset) } @Test @@ -147,10 +147,10 @@ class RequesterTest : TestAbstract() { .getFunction("test_function_paginated") .select(1, 2, mapOf("name" to "ff")) Assert.assertNotNull(result) - Assert.assertEquals(result.result[0].name, "ff") - Assert.assertEquals(result.result[1].name, "ff-2") - Assert.assertEquals(result.total, 10) - Assert.assertEquals(result.offset, 0) + Assert.assertEquals("ff", result.result[0].name) + Assert.assertEquals("ff-2", result.result[1].name) + Assert.assertEquals(10, result.total) + Assert.assertEquals(0, result.offset) } @Test