From 24349fc71f9ef56939f4b95c056bc187171ce763 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 4 Aug 2019 21:09:24 +0200 Subject: [PATCH] use sendQuery if no return is expected --- .../fr/postgresjson/connexion/Connection.kt | 27 ++++++++++++++++--- .../postgresjson/connexion/EmbedExecutable.kt | 2 ++ .../fr/postgresjson/connexion/Executable.kt | 3 ++- .../fr/postgresjson/connexion/Function.kt | 16 ++++++++--- .../kotlin/fr/postgresjson/connexion/Query.kt | 8 ++++++ .../fr/postgresjson/migration/Function.kt | 4 +-- .../kotlin/fr/postgresjson/MigrationTest.kt | 11 ++++---- .../kotlin/fr/postgresjson/RequesterTest.kt | 22 +++++++++++++++ .../kotlin/fr/postgresjson/TestAbstract.kt | 4 ++- src/test/resources/fixtures/init.sql | 11 +++++++- .../sql/function/Test/function_void.sql | 8 ++++++ src/test/resources/sql/query/Test/exec.sql | 1 + 12 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 src/test/resources/sql/function/Test/function_void.sql create mode 100644 src/test/resources/sql/query/Test/exec.sql diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index cb47567..45e7ed5 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -185,9 +185,17 @@ class Connection( } } - override fun sendQuery(sql: String): QueryResult { - return stopwatchQuery(sql) { - connect().sendQuery(sql).join() + override fun sendQuery(sql: String, values: List): Int { + return stopwatchQuery(sql, values) { + replaceArgsIntoSql(sql, compileArgs(values)) { + connect().sendQuery(it).join().rowsAffected.toInt() + } + } + } + + override fun sendQuery(sql: String, values: Map): Int { + return replaceArgs(sql, values) { + sendQuery(this.sql, this.parameters) } } @@ -219,6 +227,19 @@ class Connection( return block(ParametersQuery(newSql, newArgs)) } + private fun replaceArgsIntoSql(sql: String, values: List, block: (String) -> T): T { + val paramRegex = "(? + values[i] ?: error("Parameter $i missing") + val valToReplace = values[i].toString() + ++i + "'$valToReplace'" + } + + return block(newSql) + } + data class ParametersQuery(val sql: String, val parameters: List) private fun stopwatchQuery(sql: String, values: List = emptyList(), callback: () -> T): T { diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt index 59437c8..0428988 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt @@ -49,4 +49,6 @@ interface EmbedExecutable { fun exec(values: List = emptyList()): QueryResult fun exec(values: Map): QueryResult + fun sendQuery(values: List = emptyList()): Int + fun sendQuery(values: Map): Int } \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt index 08daedd..2ef5bcb 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -50,5 +50,6 @@ interface Executable { fun exec(sql: String, values: List = emptyList()): QueryResult fun exec(sql: String, values: Map): QueryResult - fun sendQuery(sql: String): QueryResult + fun sendQuery(sql: String, values: List = emptyList()): Int + fun sendQuery(sql: String, values: Map): Int } \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/connexion/Function.kt b/src/main/kotlin/fr/postgresjson/connexion/Function.kt index 4d42538..e4de50a 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Function.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Function.kt @@ -169,12 +169,22 @@ class Function(val definition: Function, override val connection: Connection): E return connection.exec(sql, values) } + override fun sendQuery(values: List): Int { + exec(values) + return 0 + } + + override fun sendQuery(values: Map): Int { + exec(values) + return 0 + } + private fun compileArgs(values: List): String { val placeholders = values - .filterIndexed { index, any -> - definition.parameters[index].default === null || any !== null + .filterIndexed { index, value -> + definition.parameters[index].default === null || value != null } - .mapIndexed { index, any -> + .mapIndexed { index, _ -> "?::" + definition.parameters[index].type } diff --git a/src/main/kotlin/fr/postgresjson/connexion/Query.kt b/src/main/kotlin/fr/postgresjson/connexion/Query.kt index 5bdce80..ab9624d 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Query.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Query.kt @@ -99,4 +99,12 @@ class Query(override val name: String, private val sql: String, override val con override fun exec(values: Map): QueryResult { return connection.exec(sql, values) } + + override fun sendQuery(values: List): Int { + return connection.sendQuery(sql, values) + } + + override fun sendQuery(values: Map): Int { + return connection.sendQuery(sql, values) + } } \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index 2078521..3f657a5 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -17,7 +17,7 @@ data class Function( init { if (up.name != down.name) { - throw Exception("UP and DOWN migration must have the same name [${up.name} !== ${down.name}]") + throw Exception("UP and DOWN migration must have the same name [${up.name} != ${down.name}]") } } @@ -49,7 +49,7 @@ data class Function( connection.sendQuery(down.script) this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let { - connection.exec(it, listOf(down)) + connection.sendQuery(it, listOf(down.name)) } return Status.OK } diff --git a/src/test/kotlin/fr/postgresjson/MigrationTest.kt b/src/test/kotlin/fr/postgresjson/MigrationTest.kt index cc4e4a1..aabc9fc 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -79,14 +79,15 @@ class MigrationTest(): TestAbstract() { @Test fun `run migrations force down`() { val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) - Migrations(resources, getConnextion()).apply { + val resourcesFunctions = File(this::class.java.getResource("/sql/function").toURI()) + Migrations(listOf(resources, resourcesFunctions), getConnextion()).apply { up().apply { - size `should be equal to` 1 + size `should be equal to` 6 } } - Migrations(resources, getConnextion()).apply { + Migrations(listOf(resources, resourcesFunctions), getConnextion()).apply { forceAllDown().apply { - size `should be equal to` 1 + size `should be equal to` 6 } } } @@ -95,7 +96,7 @@ class MigrationTest(): TestAbstract() { fun `run functions migrations`() { val resources = File(this::class.java.getResource("/sql/function").toURI()) Migrations(resources, getConnextion()).apply { - run().size `should be equal to` 4 + run().size `should be equal to` 5 } val objTest: RequesterTest.ObjTest? = Requester(getConnextion()) diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 050647f..9177ed1 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -57,6 +57,28 @@ class RequesterTest: TestAbstract() { assertEquals(1, result.rowsAffected) } + @Test + fun `call sendQuery on query`() { + val resources = File(this::class.java.getResource("/sql/query").toURI()) + val result = Requester(getConnextion()) + .addQuery(resources) + .getQuery("Test/exec") + .sendQuery() + + assertEquals(0, result) + } + + @Test + fun `call sendQuery on function`() { + val resources = File(this::class.java.getResource("/sql/function").toURI()) + val result = Requester(getConnextion()) + .addFunction(resources) + .getFunction("function_void") + .sendQuery(listOf("test")) + + assertEquals(0, result) + } + @Test fun `call selectOne on function`() { val resources = File(this::class.java.getResource("/sql/function").toURI()) diff --git a/src/test/kotlin/fr/postgresjson/TestAbstract.kt b/src/test/kotlin/fr/postgresjson/TestAbstract.kt index 7120a09..b80a70c 100644 --- a/src/test/kotlin/fr/postgresjson/TestAbstract.kt +++ b/src/test/kotlin/fr/postgresjson/TestAbstract.kt @@ -9,8 +9,9 @@ import java.io.File @TestInstance(PER_CLASS) abstract class TestAbstract { + private val con = Connection(database = "test", username = "test", password = "test") protected fun getConnextion(): Connection { - return Connection(database = "test", username = "test", password = "test") + return con } @BeforeEach @@ -24,5 +25,6 @@ abstract class TestAbstract { fun afterAll() { val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI()) getConnextion().connect().sendQuery(downSQL.readText()).join() + getConnextion().connect().disconnect() } } \ No newline at end of file diff --git a/src/test/resources/fixtures/init.sql b/src/test/resources/fixtures/init.sql index 2aeb03c..04dc61e 100644 --- a/src/test/resources/fixtures/init.sql +++ b/src/test/resources/fixtures/init.sql @@ -72,4 +72,13 @@ $$ BEGIN resource = json_build_object('id', 1, 'name', 'changedName'); END; -$$; \ No newline at end of file +$$; + +CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void + LANGUAGE plpgsql +AS +$$ +BEGIN + PERFORM 1; +END; +$$ \ No newline at end of file diff --git a/src/test/resources/sql/function/Test/function_void.sql b/src/test/resources/sql/function/Test/function_void.sql new file mode 100644 index 0000000..dfb08ac --- /dev/null +++ b/src/test/resources/sql/function/Test/function_void.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void +LANGUAGE plpgsql +AS +$$ +BEGIN + PERFORM 1; +END; +$$; \ No newline at end of file diff --git a/src/test/resources/sql/query/Test/exec.sql b/src/test/resources/sql/query/Test/exec.sql new file mode 100644 index 0000000..32cdc6e --- /dev/null +++ b/src/test/resources/sql/query/Test/exec.sql @@ -0,0 +1 @@ +delete FROM test where 2038538 = 2; \ No newline at end of file