feature: add exec method on query & function
This commit is contained in:
@@ -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 <T, reified R : List<EntityI<T?>?>> select(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): CompletableFuture<QueryResult> {
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
}
|
||||
|
||||
class Function(val name: String, val parameters: List<Parameter>, private val connection : Connection) {
|
||||
@@ -168,6 +174,13 @@ class Requester (
|
||||
|
||||
inline fun <T, reified R: List<EntityI<T?>?>> select(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): CompletableFuture<QueryResult> {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM $name ($args)"
|
||||
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
|
||||
private fun compileArgs(values: List<Any?>): String {
|
||||
val placeholders = values
|
||||
.filterIndexed { index, any ->
|
||||
|
||||
@@ -41,11 +41,11 @@ class ConnectionTest(): TestAbstract() {
|
||||
) j;
|
||||
"""
|
||||
)
|
||||
assertTrue(objs !== null)
|
||||
assertNotNull(objs)
|
||||
assertTrue(objs is List<ObjTest2>)
|
||||
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
|
||||
|
||||
@@ -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<QueryResult> = 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<QueryResult> = Requester(getConnextion())
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
.exec(listOf("test", "plip"))
|
||||
|
||||
future.join()
|
||||
Assertions.assertTrue(future.isCompleted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user