feature: add exec method on query & function
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
|
import com.github.jasync.sql.db.QueryResult
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
import kotlin.text.RegexOption.IGNORE_CASE
|
import kotlin.text.RegexOption.IGNORE_CASE
|
||||||
import kotlin.text.RegexOption.MULTILINE
|
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)
|
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) {
|
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)
|
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 {
|
private fun compileArgs(values: List<Any?>): String {
|
||||||
val placeholders = values
|
val placeholders = values
|
||||||
.filterIndexed { index, any ->
|
.filterIndexed { index, any ->
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ class ConnectionTest(): TestAbstract() {
|
|||||||
) j;
|
) j;
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
assertTrue(objs !== null)
|
assertNotNull(objs)
|
||||||
assertTrue(objs is List<ObjTest2>)
|
assertTrue(objs is List<ObjTest2>)
|
||||||
assertTrue(objs!!.size == 2)
|
assertEquals(objs!!.size, 2)
|
||||||
assertTrue(objs[0].id == 1)
|
assertEquals(objs[0].id, 1)
|
||||||
assertTrue(objs[0].test!!.id == 1)
|
assertEquals(objs[0].test!!.id, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package fr.postgresjson
|
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.connexion.Requester
|
||||||
import fr.postgresjson.entity.IdEntity
|
import fr.postgresjson.entity.IdEntity
|
||||||
|
import org.junit.jupiter.api.Assertions
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
|
||||||
class RequestTest: TestAbstract() {
|
class RequestTest: TestAbstract() {
|
||||||
class ObjTest(var name:String): IdEntity(1)
|
class ObjTest(var name:String): IdEntity(1)
|
||||||
@@ -16,6 +20,7 @@ class RequestTest: TestAbstract() {
|
|||||||
.addQuery(resources)
|
.addQuery(resources)
|
||||||
.getQuery("Test/selectOne")
|
.getQuery("Test/selectOne")
|
||||||
.selectOne()
|
.selectOne()
|
||||||
|
|
||||||
assertEquals(objTest!!.id, 2)
|
assertEquals(objTest!!.id, 2)
|
||||||
assertEquals(objTest.name, "test")
|
assertEquals(objTest.name, "test")
|
||||||
}
|
}
|
||||||
@@ -27,7 +32,32 @@ class RequestTest: TestAbstract() {
|
|||||||
.addFunction(resources)
|
.addFunction(resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(listOf("test", "plip"))
|
.selectOne(listOf("test", "plip"))
|
||||||
|
|
||||||
assertEquals(objTest!!.id, 3)
|
assertEquals(objTest!!.id, 3)
|
||||||
assertEquals(objTest.name, "test")
|
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