diff --git a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt index 7e7b34e..f0fb34c 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt @@ -1,8 +1,10 @@ package fr.postgresjson.connexion import com.fasterxml.jackson.core.type.TypeReference +import com.github.jasync.sql.db.QueryResult import fr.postgresjson.entity.EntityI import java.io.File +import java.util.concurrent.CompletableFuture import kotlin.text.RegexOption.IGNORE_CASE import kotlin.text.RegexOption.MULTILINE @@ -122,6 +124,10 @@ class Requester ( } inline fun ?>> select(values: List = emptyList()): R? = select(object: TypeReference() {}, values) + + fun exec(values: List = emptyList()): CompletableFuture { + return connection.exec(sql, values) + } } class Function(val name: String, val parameters: List, private val connection : Connection) { @@ -168,6 +174,13 @@ class Requester ( inline fun ?>> select(values: List = emptyList()): R? = select(object: TypeReference() {}, values) + fun exec(values: List = emptyList()): CompletableFuture { + val args = compileArgs(values) + val sql = "SELECT * FROM $name ($args)" + + return connection.exec(sql, values) + } + private fun compileArgs(values: List): String { val placeholders = values .filterIndexed { index, any -> diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index bc062b6..2d53c78 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -41,11 +41,11 @@ class ConnectionTest(): TestAbstract() { ) j; """ ) - assertTrue(objs !== null) + assertNotNull(objs) assertTrue(objs is List) - assertTrue(objs!!.size == 2) - assertTrue(objs[0].id == 1) - assertTrue(objs[0].test!!.id == 1) + assertEquals(objs!!.size, 2) + assertEquals(objs[0].id, 1) + assertEquals(objs[0].test!!.id, 1) } @Test diff --git a/src/test/kotlin/fr/postgresjson/RequestTest.kt b/src/test/kotlin/fr/postgresjson/RequestTest.kt index 79aa602..6aa6fd8 100644 --- a/src/test/kotlin/fr/postgresjson/RequestTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequestTest.kt @@ -1,10 +1,14 @@ package fr.postgresjson +import com.github.jasync.sql.db.QueryResult +import com.github.jasync.sql.db.util.isCompleted import fr.postgresjson.connexion.Requester import fr.postgresjson.entity.IdEntity +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import java.io.File +import java.util.concurrent.CompletableFuture class RequestTest: TestAbstract() { class ObjTest(var name:String): IdEntity(1) @@ -16,6 +20,7 @@ class RequestTest: TestAbstract() { .addQuery(resources) .getQuery("Test/selectOne") .selectOne() + assertEquals(objTest!!.id, 2) assertEquals(objTest.name, "test") } @@ -27,7 +32,32 @@ class RequestTest: TestAbstract() { .addFunction(resources) .getFunction("test_function") .selectOne(listOf("test", "plip")) + assertEquals(objTest!!.id, 3) assertEquals(objTest.name, "test") } + + @Test + fun callExecOnQuery() { + val resources = File(this::class.java.getResource("/sql/query").toURI()) + val future: CompletableFuture = Requester(getConnextion()) + .addQuery(resources) + .getQuery("Test/selectOne") + .exec() + + future.join() + Assertions.assertTrue(future.isCompleted) + } + + @Test + fun callExecOnFunction() { + val resources = File(this::class.java.getResource("/sql/function").toURI()) + val future: CompletableFuture = Requester(getConnextion()) + .addFunction(resources) + .getFunction("test_function") + .exec(listOf("test", "plip")) + + future.join() + Assertions.assertTrue(future.isCompleted) + } } \ No newline at end of file