From bb3b0d7e348daffe146ba2ac5de1da2b4abe775f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 31 Jul 2019 12:59:48 +0200 Subject: [PATCH] feature: update input with response if input and output is the same object --- .../kotlin/fr/postgresjson/connexion/Connection.kt | 9 ++++++++- src/test/kotlin/fr/postgresjson/RequesterTest.kt | 13 +++++++++++++ src/test/resources/fixtures/init.sql | 11 ++++++++++- .../sql/function/Test/function_test_object.sql | 8 ++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/sql/function/Test/function_test_object.sql diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 4113686..6ae91f3 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -44,12 +44,19 @@ class Connection( values: List, block: (QueryResult, R?) -> Unit ): R? { + val primaryObject = values.firstOrNull { + it is EntityI<*> && typeReference.type.typeName == it::class.java.name + } as R? val result = exec(sql, compileArgs(values)) val json = result.rows[0].getString(0) return if (json === null) { null } else { - serializer.deserialize(json, typeReference) + if (primaryObject != null) { + serializer.deserialize(json, primaryObject) + } else { + serializer.deserialize(json, typeReference) + } }.also { block(result, it) } diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 212d8b0..050647f 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -68,6 +68,19 @@ class RequesterTest: TestAbstract() { assertEquals("myName", obj.name) } + @Test + fun `call selectOne on function with object`() { + val resources = File(this::class.java.getResource("/sql/function").toURI()) + val obj2 = ObjTest("original") + val obj: ObjTest = Requester(getConnextion()) + .addFunction(resources) + .getFunction("test_function_object") + .selectOne("resource" to obj2)!! + + assertEquals("changedName", obj.name) + assertEquals("changedName", obj2.name) + } + @Test fun `call selectOne on query`() { val resources = File(this::class.java.getResource("/sql/query").toURI()) diff --git a/src/test/resources/fixtures/init.sql b/src/test/resources/fixtures/init.sql index b8939a8..2aeb03c 100644 --- a/src/test/resources/fixtures/init.sql +++ b/src/test/resources/fixtures/init.sql @@ -63,4 +63,13 @@ BEGIN INTO result, total LIMIT "limit" OFFSET "offset"; END; -$$ \ No newline at end of file +$$; + +CREATE OR REPLACE FUNCTION test_function_object (inout resource json) + LANGUAGE plpgsql +AS +$$ +BEGIN + resource = json_build_object('id', 1, 'name', 'changedName'); +END; +$$; \ No newline at end of file diff --git a/src/test/resources/sql/function/Test/function_test_object.sql b/src/test/resources/sql/function/Test/function_test_object.sql new file mode 100644 index 0000000..d176b96 --- /dev/null +++ b/src/test/resources/sql/function/Test/function_test_object.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE FUNCTION test_function_object (inout resource json) +LANGUAGE plpgsql +AS +$$ +BEGIN + resource = json_build_object('id', 1, 'name', 'changedName'); +END; +$$ \ No newline at end of file