feature #6: Implement named parameters

This commit is contained in:
2019-07-15 13:37:17 +02:00
parent b00f8cb5d0
commit e6b8e66b28
8 changed files with 263 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ package fr.postgresjson
import com.github.jasync.sql.db.util.isCompleted
import fr.postgresjson.connexion.Connection
import fr.postgresjson.entity.IdEntity
import org.junit.jupiter.api.Assertions.*
import org.junit.Assert.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.TestInstance
class ConnectionTest(): TestAbstract() {
private class ObjTest(var name: String): IdEntity()
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
private class ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
private lateinit var connection: Connection
@@ -60,10 +61,10 @@ class ConnectionTest(): TestAbstract() {
val o = ObjTest("myName")
o.id = 88
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id int, name text);", listOf(o))
assertTrue(obj !== null)
assertNotNull(obj)
assertTrue(obj is ObjTest)
assertTrue(obj!!.id == 88)
assertTrue(obj.name == "myName")
assertEquals(obj!!.id, 88)
assertEquals(obj.name, "myName")
}
@Test
@@ -73,4 +74,59 @@ class ConnectionTest(): TestAbstract() {
future.join()
assertTrue(future.isCompleted)
}
@Test
fun `select one with named parameters`() {
val result: ObjTest3? = connection.selectOne(
"SELECT json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)",
mapOf(
"first" to "ff",
"seconde" to "sec",
"third" to 123
)
)
assertEquals(result!!.first, "ff")
assertEquals(result.seconde, "sec")
assertEquals(result.third, 123)
}
@Test
fun `select with named parameters`() {
val params: Map<String, Any?> = mapOf(
"first" to "ff",
"third" to 123,
"seconde" to "sec"
)
val result: List<ObjTest3?> = connection.select(
"""
SELECT json_build_array(
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int),
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)
)
""".trimIndent(),
params
)
assertEquals(result[0]!!.first, "ff")
assertEquals(result[0]!!.seconde, "sec")
assertEquals(result[0]!!.third, 123)
}
@Test
fun `selectOne with named parameters`() {
val params: Map<String, Any?> = mapOf(
"first" to "ff",
"third" to 123,
"seconde" to "sec"
)
val result: ObjTest3? = connection.selectOne(
"""
SELECT json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)
""".trimIndent(),
params
)
assertNotNull(result)
assertEquals(result!!.first, "ff")
assertEquals(result.seconde, "sec")
assertEquals(result.third, 123)
}
}

View File

@@ -10,11 +10,11 @@ import org.junit.jupiter.api.Test
import java.io.File
import java.util.concurrent.CompletableFuture
class RequestTest: TestAbstract() {
class RequesterTest: TestAbstract() {
class ObjTest(var name:String): IdEntity(1)
@Test
fun getQueryFromFile() {
fun `get query from file`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val objTest: ObjTest? = Requester(getConnextion())
.addQuery(resources)
@@ -26,7 +26,7 @@ class RequestTest: TestAbstract() {
}
@Test
fun getFunctionFromFile() {
fun `get function from file`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val objTest: ObjTest? = Requester(getConnextion())
.addFunction(resources)
@@ -38,7 +38,7 @@ class RequestTest: TestAbstract() {
}
@Test
fun callExecOnQuery() {
fun `call exec on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val future: CompletableFuture<QueryResult> = Requester(getConnextion())
.addQuery(resources)
@@ -50,7 +50,7 @@ class RequestTest: TestAbstract() {
}
@Test
fun callExecOnFunction() {
fun `call exec on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val future: CompletableFuture<QueryResult> = Requester(getConnextion())
.addFunction(resources)
@@ -60,4 +60,37 @@ class RequestTest: TestAbstract() {
future.join()
Assertions.assertTrue(future.isCompleted)
}
@Test
fun `call selectOne on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: ObjTest = Requester(getConnextion())
.addFunction(resources)
.getFunction("test_function")
.selectOne(mapOf("name" to "myName"))!!
assertEquals("myName", obj.name)
}
@Test
fun `call selectOne on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val obj: ObjTest = Requester(getConnextion())
.addQuery(resources)
.getQuery("Test/selectOneWithParameters")
.selectOne(mapOf("name" to "myName"))!!
assertEquals("myName", obj.name)
}
@Test
fun `call select (multiple) on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: List<ObjTest>? = Requester(getConnextion())
.addFunction(resources)
.getFunction("test_function_multiple")
.select(mapOf("name" to "myName"))
assertEquals("myName", obj!![0].name)
}
}

View File

@@ -33,6 +33,19 @@ CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text d
AS
$$
BEGIN
result = json_build_object('id', 3, 'name', 'test');
result = json_build_object('id', 3, 'name', name);
END;
$$;
CREATE OR REPLACE FUNCTION test_function_multiple (name text default 'plop', IN hi text default 'hello', out result json)
LANGUAGE plpgsql
AS
$$
BEGIN
result = json_build_array(
json_build_object('id', 3, 'name', name),
json_build_object('id', 4, 'name', hi)
);
END;
$$;

View File

@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function_multiple (name text default 'plop', IN hi text default 'hello', out result json)
LANGUAGE plpgsql
AS
$$
BEGIN
result = json_build_array(
json_build_object('id', 3, 'name', name),
json_build_object('id', 4, 'name', hi)
);
END;
$$

View File

@@ -0,0 +1 @@
select json_build_object('id', 2, 'name', :name::text);