diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 0a067d1..80e0600 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -206,6 +206,12 @@ class Connection( } } + override fun sendQuery(sql: String, values: Map): Int { + return replaceArgs(sql, values) { + sendQuery(this.sql, this.parameters) + } + } + private fun compileArgs(values: List): List { return values.map { if (it is EntityI<*>) { diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt index 7a22d82..33564d4 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()): ResultSet fun exec(values: Map): ResultSet + 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 fefac71..1bb50bf 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -51,4 +51,5 @@ interface Executable { fun exec(sql: String, values: List = emptyList()): ResultSet fun exec(sql: String, values: Map): ResultSet 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 fab6597..33f0d9e 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 5dff06a..79c9ec2 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): ResultSet { 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 7023eed..b5f8e66 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)) } 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 f4d58ea..7d41302 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -4,8 +4,8 @@ import fr.postgresjson.connexion.Paginated import fr.postgresjson.connexion.Requester import fr.postgresjson.entity.IdEntity import org.junit.Assert -import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Test import java.io.File @@ -44,7 +44,7 @@ class RequesterTest: TestAbstract() { .getQuery("Test/selectOne") .exec() - Assertions.assertNotNull(result.getString(1)) + assertNotNull(result.getString(1)) } @Test @@ -55,7 +55,29 @@ class RequesterTest: TestAbstract() { .getFunction("test_function") .exec(listOf("test", "plip")) - Assertions.assertNotNull(result.getString(1)) + assertNotNull(result.getString(1)) + } + + @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 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