diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 8355ef3..70cd4c5 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -44,7 +44,7 @@ class Connection( } fun disconnect() { - connection?.run { disconnect() } + connection?.disconnect() } fun inTransaction(f: (Connection) -> CompletableFuture) = connect().inTransaction(f) @@ -97,7 +97,7 @@ class Connection( values: List, block: (QueryResult, List) -> Unit ): List { - val result = exec(sql, compileArgs(values)) + val result = exec(sql, values) val json = result.rows[0].getString(0) return if (json === null) { listOf() as List diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt index ec9f1a9..79ce38b 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt @@ -10,8 +10,19 @@ sealed interface EmbedExecutable { val name: String /* Select One */ + /** - * Select One entity with list of parameters + * Update [EntityI] with one entity as argument + */ + fun update( + typeReference: TypeReference, + value: R, + block: SelectOneCallback = {} + ): R? = + selectOne(typeReference, listOf(value), block) + + /** + * Select One [EntityI] with [List] of parameters */ fun selectOne( typeReference: TypeReference, @@ -19,26 +30,60 @@ sealed interface EmbedExecutable { block: SelectOneCallback = {} ): R? + /** + * Select One [EntityI] with [Map] of parameters + */ fun selectOne( typeReference: TypeReference, values: Map, block: SelectOneCallback = {} ): R? + /** + * Select One [EntityI] with multiple [Pair] of parameters + */ + fun selectOne( + typeReference: TypeReference, + vararg values: Pair, + block: SelectOneCallback = {} + ): R? = + selectOne(typeReference, values.toMap(), block) + /* Select Multiples */ + + /** + * Select Multiple [EntityI] with [List] of parameters + */ fun select( typeReference: TypeReference>, values: List = emptyList(), block: SelectCallback = {} ): List + /** + * Select Multiple [EntityI] with [Map] of parameters + */ fun select( typeReference: TypeReference>, values: Map, block: SelectCallback = {} ): List + /** + * Select Multiple [EntityI] with multiple [Pair] of parameters + */ + fun select( + typeReference: TypeReference>, + vararg values: Pair, + block: SelectCallback = {} + ): List = + select(typeReference, values.toMap(), block) + /* Select Paginated */ + + /** + * Select Paginated [EntityI] with [Map] of parameters + */ fun select( page: Int, limit: Int, @@ -47,6 +92,18 @@ sealed interface EmbedExecutable { block: SelectPaginatedCallback = {} ): Paginated + /** + * Select Paginated [EntityI] with multiple [Pair] of parameters + */ + fun select( + page: Int, + limit: Int, + typeReference: TypeReference>, + vararg values: Pair, + block: SelectPaginatedCallback = {} + ): Paginated = + select(page, limit, typeReference, values.toMap(), block) + fun exec(values: List = emptyList()): QueryResult fun exec(values: Map): QueryResult fun exec(vararg values: Pair): QueryResult = exec(values.toMap()) @@ -57,6 +114,5 @@ sealed interface EmbedExecutable { fun sendQuery(values: List = emptyList()): Int fun sendQuery(values: Map): Int - fun sendQuery(vararg values: Pair): Int = - sendQuery(values.toMap()) + fun sendQuery(vararg values: Pair): Int = sendQuery(values.toMap()) } diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt new file mode 100644 index 0000000..ce64766 --- /dev/null +++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutableReified.kt @@ -0,0 +1,68 @@ +package fr.postgresjson.connexion + +import com.fasterxml.jackson.core.type.TypeReference +import fr.postgresjson.entity.EntityI + +/* Select One */ + +inline fun EmbedExecutable.update( + value: R, + noinline block: SelectOneCallback = {} +): R? = + update(object : TypeReference() {}, value, block) + +inline fun EmbedExecutable.selectOne( + values: List = emptyList(), + noinline block: SelectOneCallback = {} +): R? = + selectOne(object : TypeReference() {}, values, block) + +inline fun EmbedExecutable.selectOne( + values: Map, + noinline block: SelectOneCallback = {} +): R? = + selectOne(object : TypeReference() {}, values, block) + +inline fun EmbedExecutable.selectOne( + vararg values: Pair, + noinline block: SelectOneCallback = {} +): R? = + selectOne(object : TypeReference() {}, values = values, block) + +/* Select Multiples */ + +inline fun EmbedExecutable.select( + values: List = emptyList(), + noinline block: SelectCallback = {} +): List = + select(object : TypeReference>() {}, values, block) + +inline fun EmbedExecutable.select( + values: Map, + noinline block: SelectCallback = {} +): List = + select(object : TypeReference>() {}, values, block) + +inline fun EmbedExecutable.select( + vararg values: Pair, + noinline block: SelectCallback = {} +): List = + select(object : TypeReference>() {}, values = values, block) + +/* Select Paginated */ + +inline fun EmbedExecutable.select( + page: Int, + limit: Int, + values: Map = emptyMap(), + noinline block: SelectPaginatedCallback = {} +): Paginated = + select(page, limit, object : TypeReference>() {}, values, block) + +inline fun EmbedExecutable.select( + page: Int, + limit: Int, + vararg values: Pair, + noinline block: SelectPaginatedCallback = {} +): Paginated = + select(page, limit, object : TypeReference>() {}, values = values, block) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt index 5684ebd..2a7fe5a 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -5,8 +5,23 @@ import com.github.jasync.sql.db.QueryResult import fr.postgresjson.entity.EntityI interface Executable { + /* Select One */ + /** + * Update [EntityI] with one entity as argument + */ + fun update( + sql: String, + typeReference: TypeReference, + value: R, + block: SelectOneCallback = {} + ): R? = + selectOne(sql, typeReference, listOf(value), block) + + /** + * Select One [EntityI] with [List] of parameters + */ fun selectOne( sql: String, typeReference: TypeReference, @@ -14,6 +29,9 @@ interface Executable { block: SelectOneCallback = {} ): R? + /** + * Select One [EntityI] with [Map] of parameters + */ fun selectOne( sql: String, typeReference: TypeReference, @@ -21,8 +39,22 @@ interface Executable { block: SelectOneCallback = {} ): R? + /** + * Select One [EntityI] with multiple [Pair] of parameters + */ + fun selectOne( + sql: String, + typeReference: TypeReference, + vararg values: Pair, + block: SelectOneCallback = {} + ): R? = + selectOne(sql, typeReference, values.toMap(), block) + /* Select Multiples */ + /** + * Select Multiple [EntityI] with [List] of parameters + */ fun select( sql: String, typeReference: TypeReference>, @@ -30,6 +62,9 @@ interface Executable { block: SelectCallback = {} ): List + /** + * Select Multiple [EntityI] with [Map] of parameters + */ fun select( sql: String, typeReference: TypeReference>, @@ -37,8 +72,22 @@ interface Executable { block: SelectCallback = {} ): List + /** + * Select Multiple [EntityI] with multiple [Pair] of parameters + */ + fun select( + sql: String, + typeReference: TypeReference>, + vararg values: Pair, + block: SelectCallback = {} + ): List = + select(sql, typeReference, values.toMap(), block) + /* Select Paginated */ + /** + * Select Paginated [EntityI] with [Map] of parameters + */ fun select( sql: String, page: Int, @@ -48,8 +97,31 @@ interface Executable { block: SelectPaginatedCallback = {} ): Paginated + /** + * Select Paginated [EntityI] with multiple [Pair] of parameters + */ + fun select( + sql: String, + page: Int, + limit: Int, + typeReference: TypeReference>, + vararg values: Pair, + block: SelectPaginatedCallback = {} + ): Paginated = + select(sql, page, limit, typeReference, values.toMap(), block) + + fun exec(sql: String, value: R): QueryResult = exec(sql, listOf(value)) fun exec(sql: String, values: List = emptyList()): QueryResult fun exec(sql: String, values: Map): QueryResult + fun exec(sql: String, vararg values: Pair): QueryResult = exec(sql, values.toMap()) + + fun perform(sql: String, value: R) { perform(sql, listOf(value)) } + fun perform(sql: String, values: List) { exec(sql, values) } + fun perform(sql: String, values: Map) { exec(sql, values) } + fun perform(sql: String, vararg values: Pair) = perform(sql, values.toMap()) + + fun sendQuery(sql: String, value: R): Int = sendQuery(sql, listOf(value)) fun sendQuery(sql: String, values: List = emptyList()): Int fun sendQuery(sql: String, values: Map): Int + fun sendQuery(sql: String, vararg values: Pair): Int = sendQuery(sql, values.toMap()) } diff --git a/src/main/kotlin/fr/postgresjson/connexion/Function.kt b/src/main/kotlin/fr/postgresjson/connexion/Function.kt index bdd73c0..613d3bf 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Function.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Function.kt @@ -12,112 +12,64 @@ class Function(val definition: Function, override val connection: Connection) : override val name: String = definition.name + /** + * Update [EntityI] + */ + override fun update( + typeReference: TypeReference, + value: R, + block: (QueryResult, R?) -> Unit + ): R? = + connection.update(compileSql(value), typeReference, value, block) + /* Select One */ /** - * Select One entity with list of parameters + * Select One [EntityI] with [List] of parameters */ override fun selectOne( typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit - ): R? { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.selectOne(sql, typeReference, values, block) - } - - inline fun selectOne( - values: List = emptyList(), - noinline block: SelectOneCallback = {} ): R? = - selectOne(object : TypeReference() {}, values, block) - - inline fun selectOne( - value: R, - noinline block: SelectOneCallback = {} - ): R? = - selectOne(object : TypeReference() {}, listOf(value), block) + connection.selectOne(compileSql(values), typeReference, values, block) /** - * Select One entity with named parameters + * Select One [EntityI] with named parameters */ override fun selectOne( typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit - ): R? { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.selectOne(sql, typeReference, values, block) - } - - inline fun selectOne( - values: Map, - noinline block: SelectOneCallback = {} ): R? = - selectOne(object : TypeReference() {}, values, block) - - inline fun selectOne( - vararg values: Pair, - noinline block: SelectOneCallback = {} - ): R? = - selectOne(values.toMap(), block) + connection.selectOne(compileSql(values), typeReference, values, block) /* Select Multiples */ /** - * Select list of entities with list of parameters + * Select multiple [EntityI] with [List] of parameters */ override fun select( typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit - ): List { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.select(sql, typeReference, values, block) - } - - inline fun select( - values: List = emptyList(), - noinline block: SelectCallback = {} ): List = - select(object : TypeReference>() {}, values, block) + connection.select(compileSql(values), typeReference, values, block) /** - * Select list of entities with named parameters + * Select multiple [EntityI] with named parameters */ override fun select( typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit - ): List { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.select(sql, typeReference, values, block) - } - - inline fun select( - values: Map, - noinline block: SelectCallback = {} ): List = - select(object : TypeReference>() {}, values, block) - - inline fun select( - vararg values: Pair, - noinline block: SelectCallback = {} - ): List = - select(values.toMap(), block) + connection.select(compileSql(values), typeReference, values, block) /* Select Paginated */ /** - * Select Multiple with pagination + * Select Multiple [EntityI] with pagination */ override fun select( page: Int, @@ -131,43 +83,14 @@ class Function(val definition: Function, override val connection: Connection) : .plus("offset" to offset) .plus("limit" to limit) - val args = compileArgs(newValues) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.select(sql, page, limit, typeReference, values, block) + return connection.select(compileSql(newValues), page, limit, typeReference, values, block) } - inline fun select( - page: Int, - limit: Int, - values: Map = emptyMap(), - noinline block: SelectPaginatedCallback = {} - ): Paginated = - select(page, limit, object : TypeReference>() {}, values, block) + /* Execute function without treatments */ - inline fun select( - page: Int, - limit: Int, - vararg values: Pair, - noinline block: SelectPaginatedCallback = {} - ): Paginated = - select(page, limit, object : TypeReference>() {}, values.toMap(), block) + override fun exec(values: List): QueryResult = connection.exec(compileSql(values), values) - /* Execute function without traitements */ - - override fun exec(values: List): QueryResult { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.exec(sql, values) - } - - override fun exec(values: Map): QueryResult { - val args = compileArgs(values) - val sql = "SELECT * FROM ${definition.name} ($args)" - - return connection.exec(sql, values) - } + override fun exec(values: Map): QueryResult = connection.exec(compileSql(values), values) override fun sendQuery(values: List): Int { exec(values) @@ -179,6 +102,8 @@ class Function(val definition: Function, override val connection: Connection) : return 0 } + private fun compileArgs(value: R): String = compileArgs(listOf(value)) + private fun compileArgs(values: List): String { val placeholders = values .filterIndexed { index, value -> @@ -205,4 +130,8 @@ class Function(val definition: Function, override val connection: Connection) : return placeholders.joinToString(separator = ", ") } + + private fun compileSql(value: R): String = "SELECT * FROM ${definition.name} (${compileArgs(value)})" + private fun compileSql(values: List): String = "SELECT * FROM ${definition.name} (${compileArgs(values)})" + private fun compileSql(values: Map): String = "SELECT * FROM ${definition.name} (${compileArgs(values)})" } diff --git a/src/main/kotlin/fr/postgresjson/connexion/Query.kt b/src/main/kotlin/fr/postgresjson/connexion/Query.kt index 620cdff..4567cfd 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Query.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Query.kt @@ -9,101 +9,78 @@ class Query(override val name: String, private val sql: String, override val con return sql } + /** + * Update [EntityI] + */ + override fun update( + typeReference: TypeReference, + value: R, + block: (QueryResult, R?) -> Unit + ): R? = + connection.update(sql, typeReference, value, block) + /* Select One */ + /** + * Select One [EntityI] with [List] of parameters + */ override fun selectOne( typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit - ): R? { - return connection.selectOne(this.toString(), typeReference, values, block) - } - - inline fun selectOne( - values: List = emptyList(), - noinline block: SelectOneCallback = {} ): R? = - selectOne(object : TypeReference() {}, values, block) + connection.selectOne(sql, typeReference, values, block) + /** + * Select One [EntityI] with named parameters + */ override fun selectOne( typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit - ): R? { - return connection.selectOne(this.toString(), typeReference, values, block) - } - - inline fun selectOne( - values: Map, - noinline block: SelectOneCallback = {} ): R? = - selectOne(object : TypeReference() {}, values, block) + connection.selectOne(sql, typeReference, values, block) /* Select Multiples */ + /** + * Select multiple [EntityI] with [List] of parameters + */ override fun select( typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit - ): List { - return connection.select(this.toString(), typeReference, values, block) - } - - inline fun select( - values: List = emptyList(), - noinline block: SelectCallback = {} ): List = - select(object : TypeReference>() {}, values, block) + connection.select(sql, typeReference, values, block) override fun select( typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit - ): List { - return connection.select(this.toString(), typeReference, values, block) - } - - inline fun select( - values: Map, - noinline block: SelectCallback = {} ): List = - select(object : TypeReference>() {}, values, block) + connection.select(sql, typeReference, values, block) + /* Select Paginated */ + + /** + * Select Multiple [EntityI] with pagination + */ override fun select( page: Int, limit: Int, typeReference: TypeReference>, values: Map, block: (QueryResult, Paginated) -> Unit - ): Paginated { - return connection.select(this.toString(), page, limit, typeReference, values, block) - } - - /* Select Paginated */ - - inline fun select( - page: Int, - limit: Int, - values: Map = emptyMap(), - noinline block: SelectPaginatedCallback = {} ): Paginated = - select(page, limit, object : TypeReference>() {}, values, block) + connection.select(sql, page, limit, typeReference, values, block) - /* Execute function without traitements */ + /* Execute function without treatments */ - override fun exec(values: List): QueryResult { - return connection.exec(sql, values) - } + override fun exec(values: List): QueryResult = connection.exec(sql, values) - override fun exec(values: Map): QueryResult { - return connection.exec(sql, values) - } + override fun exec(values: Map): QueryResult = connection.exec(sql, values) - override fun sendQuery(values: List): Int { - return connection.sendQuery(sql, values) - } + override fun sendQuery(values: List): Int = connection.sendQuery(sql, values) - override fun sendQuery(values: Map): Int { - return connection.sendQuery(sql, values) - } + override fun sendQuery(values: Map): Int = connection.sendQuery(sql, values) } diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index 8dfdc3e..a0645ac 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -12,7 +12,7 @@ import org.junit.jupiter.api.TestInstance import java.util.UUID @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class ConnectionTest() : TestAbstract() { +class ConnectionTest : TestAbstract() { private class ObjTest(val name: String, id: UUID = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")) : UuidEntity(id) private class ObjTest2(val title: String, var test: ObjTest?) : UuidEntity() private class ObjTest3(val first: String, var seconde: String, var third: Int) : UuidEntity() diff --git a/src/test/kotlin/fr/postgresjson/EntityTest.kt b/src/test/kotlin/fr/postgresjson/EntityTest.kt index 7b49fdb..1111f8d 100644 --- a/src/test/kotlin/fr/postgresjson/EntityTest.kt +++ b/src/test/kotlin/fr/postgresjson/EntityTest.kt @@ -16,11 +16,11 @@ import java.util.UUID @TestInstance(TestInstance.Lifecycle.PER_CLASS) class EntityTest() { private class User(id: UUID = UUID.randomUUID()) : Entity(id) - private class ObjTest(var name: String) : UuidEntityExtended(User(), User()) + private class ObjTest(val name: String) : UuidEntityExtended(User(), User()) @Test fun getObject() { - val obj: ObjTest? = ObjTest("plop") + val obj = ObjTest("plop") assertTrue(obj is ObjTest) assertTrue(obj is UuidEntityExtended) assertTrue(obj is EntityI) diff --git a/src/test/kotlin/fr/postgresjson/MigrationTest.kt b/src/test/kotlin/fr/postgresjson/MigrationTest.kt index 0e4ed2f..6d97a34 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -1,6 +1,7 @@ package fr.postgresjson import fr.postgresjson.connexion.Requester +import fr.postgresjson.connexion.selectOne import fr.postgresjson.migration.Migration import fr.postgresjson.migration.Migrations import org.amshove.kluent.`should be equal to` @@ -13,10 +14,10 @@ import org.junit.jupiter.api.TestInstance import java.util.UUID @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class MigrationTest() : TestAbstract() { +class MigrationTest : TestAbstract() { @Test fun `run up query`() { - val resources = this::class.java.getResource("/sql/migrations").toURI() + val resources = this::class.java.getResource("/sql/migrations")!!.toURI() val m = Migrations(connection, resources) m.up().apply { this `should contain` Pair("1", Migration.Status.OK) @@ -28,7 +29,7 @@ class MigrationTest() : TestAbstract() { @Test fun `migration up Query should throw error if no down`() { - val resources = this::class.java.getResource("/sql/migration_without_down").toURI() + val resources = this::class.java.getResource("/sql/migration_without_down")!!.toURI() invoking { Migrations(resources, connection) } shouldThrow Migrations.DownMigrationNotDefined::class @@ -36,7 +37,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run forced down query`() { - val resources = this::class.java.getResource("/sql/migrations").toURI() + val resources = this::class.java.getResource("/sql/migrations")!!.toURI() val m = Migrations(resources, connection) repeat(3) { m.down(true).apply { @@ -48,7 +49,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run dry migrations`() { - val resources = this::class.java.getResource("/sql/real_migrations").toURI() + val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() Migrations(resources, connection).apply { runDry().size `should be equal to` 2 } @@ -59,7 +60,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run dry migrations launch twice`() { - val resources = this::class.java.getResource("/sql/real_migrations").toURI() + val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() Migrations(resources, connection).apply { runDry().size `should be equal to` 2 runDry().size `should be equal to` 2 @@ -68,7 +69,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run migrations`() { - val resources = this::class.java.getResource("/sql/real_migrations").toURI() + val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() Migrations(resources, connection).apply { run().apply { size `should be equal to` 1 @@ -78,8 +79,8 @@ class MigrationTest() : TestAbstract() { @Test fun `run migrations force down`() { - val resources = this::class.java.getResource("/sql/real_migrations").toURI() - val resourcesFunctions = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() + val resourcesFunctions = this::class.java.getResource("/sql/function/Test")!!.toURI() Migrations(listOf(resources, resourcesFunctions), connection).apply { up().apply { size `should be equal to` 6 @@ -94,7 +95,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run functions migrations`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")!!.toURI() Migrations(resources, connection).apply { run().size `should be equal to` 5 } @@ -109,7 +110,7 @@ class MigrationTest() : TestAbstract() { @Test fun `run functions migrations and drop if exist`() { - val resources = this::class.java.getResource("/sql/function/Test1").toURI() + val resources = this::class.java.getResource("/sql/function/Test1")!!.toURI() Migrations(resources, connection).apply { run().size `should be equal to` 1 } @@ -121,7 +122,7 @@ class MigrationTest() : TestAbstract() { Assertions.assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d")) Assertions.assertEquals(objTest.name, "test") - val resources2 = this::class.java.getResource("/sql/function/Test2").toURI() + val resources2 = this::class.java.getResource("/sql/function/Test2")!!.toURI() Migrations(resources2, connection).apply { run().size `should be equal to` 1 } diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 5f737a4..8a529ef 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -4,6 +4,9 @@ import fr.postgresjson.connexion.Paginated import fr.postgresjson.connexion.Requester import fr.postgresjson.connexion.Requester.NoFunctionDefined import fr.postgresjson.connexion.Requester.NoQueryDefined +import fr.postgresjson.connexion.select +import fr.postgresjson.connexion.selectOne +import fr.postgresjson.connexion.update import fr.postgresjson.entity.UuidEntity import org.junit.Assert import org.junit.jupiter.api.Assertions.assertEquals @@ -16,7 +19,7 @@ class RequesterTest : TestAbstract() { @Test fun `requester constructor empty`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")!!.toURI() val name: String = Requester(connection) .apply { addFunctions(resources) } .getFunction("test_function") @@ -27,7 +30,7 @@ class RequesterTest : TestAbstract() { @Test fun `requester constructor function directory`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val name: String = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .name @@ -37,7 +40,7 @@ class RequesterTest : TestAbstract() { @Test fun `requester constructor query directory`() { - val resources = this::class.java.getResource("/sql/query/Test").toURI() + val resources = this::class.java.getResource("/sql/query/Test")?.toURI() val name: String = Requester(connection, queriesDirectory = resources) .getQuery("DeleteTest") .name @@ -47,7 +50,7 @@ class RequesterTest : TestAbstract() { @Test fun `function toString`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val name: String = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .toString() @@ -88,7 +91,7 @@ class RequesterTest : TestAbstract() { @Test fun `get query from file`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")!!.toURI() val objTest: ObjTest? = Requester(connection) .apply { addQuery(resources) } .getQuery("selectOne") @@ -100,7 +103,7 @@ class RequesterTest : TestAbstract() { @Test fun `get query from file with wrong name throw exception`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() assertThrows(NoQueryDefined::class.java) { Requester(connection, queriesDirectory = resources) .getQuery("wrongName") @@ -109,7 +112,7 @@ class RequesterTest : TestAbstract() { @Test fun `get queries from file`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val name: String = Requester(connection, queriesDirectory = resources) .getQueries()[0].name @@ -118,7 +121,7 @@ class RequesterTest : TestAbstract() { @Test fun `get function from file with wrong name throw exception`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() assertThrows(NoFunctionDefined::class.java) { Requester(connection, functionsDirectory = resources) .getFunction("wrongName") @@ -127,7 +130,7 @@ class RequesterTest : TestAbstract() { @Test fun `get function from file`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val objTest: ObjTest? = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .selectOne(listOf("test", "plip")) @@ -138,7 +141,7 @@ class RequesterTest : TestAbstract() { @Test fun `call exec on query`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val result = Requester(connection, queriesDirectory = resources) .getQuery("selectOne") .exec() @@ -148,7 +151,7 @@ class RequesterTest : TestAbstract() { @Test fun `call exec on function`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val result = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .exec(listOf("test", "plip")) @@ -158,7 +161,7 @@ class RequesterTest : TestAbstract() { @Test fun `call sendQuery on query with name`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val result = Requester(connection, queriesDirectory = resources) .getQuery("DeleteTest") .sendQuery() @@ -168,7 +171,7 @@ class RequesterTest : TestAbstract() { @Test fun `call sendQuery on function`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val result = Requester(connection, functionsDirectory = resources) .getFunction("function_void") .sendQuery(listOf("test")) @@ -178,7 +181,7 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj: ObjTest = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .selectOne(mapOf("name" to "myName"))!! @@ -188,7 +191,7 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function with object and named argument`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj2 = ObjTest("original") val obj: ObjTest = Requester(connection, functionsDirectory = resources) .getFunction("test_function_object") @@ -200,11 +203,11 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function with object`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj2 = ObjTest("original") val obj: ObjTest = Requester(connection, functionsDirectory = resources) .getFunction("test_function_object") - .selectOne(obj2)!! + .update(obj2)!! assertEquals("changedName", obj.name) assertEquals("original", obj2.name) @@ -212,7 +215,7 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on function with object and no arguments`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() val obj: ObjTest = Requester(connection, functionsDirectory = resources) .getFunction("test_function") .selectOne()!! @@ -222,7 +225,7 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on query`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val obj: ObjTest = Requester(connection, queriesDirectory = resources) .getQuery("selectOneWithParameters") .selectOne(mapOf("name" to "myName"))!! @@ -232,27 +235,60 @@ class RequesterTest : TestAbstract() { @Test fun `call select (multiple) on function with named argument`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() - val obj: List? = Requester(connection, functionsDirectory = resources) + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() + val obj: List = Requester(connection, functionsDirectory = resources) .getFunction("test_function_multiple") .select(mapOf("name" to "myName")) - assertEquals("myName", obj!![0].name) + assertEquals("myName", obj[0].name) } @Test fun `call select (multiple) on function with ordered arguments`() { - val resources = this::class.java.getResource("/sql/function/Test").toURI() - val obj: List? = Requester(connection, functionsDirectory = resources) + val resources = this::class.java.getResource("/sql/function/Test")?.toURI() + val obj: List = Requester(connection, functionsDirectory = resources) .getFunction("test_function_multiple") .select(listOf("myName")) - assertEquals("myName", obj!![0].name) + assertEquals("myName", obj[0].name) + } + + @Test + fun `call select multiple (named arguments)`() { + val resources = this::class.java.getResource("/sql/query")?.toURI() + val result: List = Requester(connection, queriesDirectory = resources) + .getQuery("selectMultiple") + .select(mapOf("name" to "ff")) + Assert.assertNotNull(result) + Assert.assertEquals("ff", result[0].name) + Assert.assertEquals("ff-2", result[1].name) + } + + @Test + fun `call select multiple (named arguments as pair)`() { + val resources = this::class.java.getResource("/sql/query")?.toURI() + val result: List = Requester(connection, queriesDirectory = resources) + .getQuery("selectMultiple") + .select("name" to "ff") + Assert.assertNotNull(result) + Assert.assertEquals("ff", result[0].name) + Assert.assertEquals("ff-2", result[1].name) + } + + @Test + fun `call select multiple (ordered argument)`() { + val resources = this::class.java.getResource("/sql/query")?.toURI() + val result: List = Requester(connection, queriesDirectory = resources) + .getQuery("selectMultipleOrderedArgs") + .select(listOf("ff", "aa")) + Assert.assertNotNull(result) + Assert.assertEquals("ff", result[0].name) + Assert.assertEquals("aa-2", result[1].name) } @Test fun `call select paginated on query`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val result: Paginated = Requester(connection, queriesDirectory = resources) .getQuery("selectPaginated") .select(1, 2, mapOf("name" to "ff")) @@ -265,7 +301,7 @@ class RequesterTest : TestAbstract() { @Test fun `call select paginated on function`() { - val resources = this::class.java.getResource("/sql/function").toURI() + val resources = this::class.java.getResource("/sql/function")?.toURI() val result: Paginated = Requester(connection, functionsDirectory = resources) .getFunction("test_function_paginated") .select(1, 2, mapOf("name" to "ff")) @@ -278,7 +314,7 @@ class RequesterTest : TestAbstract() { @Test fun `call selectOne on query with extra parameter`() { - val resources = this::class.java.getResource("/sql/query").toURI() + val resources = this::class.java.getResource("/sql/query")?.toURI() val obj: ObjTest = Requester(connection, queriesDirectory = resources) .getQuery("selectOneWithParameters") .selectOne(mapOf("name" to "myName")) { diff --git a/src/test/kotlin/fr/postgresjson/SerializerTest.kt b/src/test/kotlin/fr/postgresjson/SerializerTest.kt index eee238f..b8d7b3a 100644 --- a/src/test/kotlin/fr/postgresjson/SerializerTest.kt +++ b/src/test/kotlin/fr/postgresjson/SerializerTest.kt @@ -2,7 +2,6 @@ package fr.postgresjson import fr.postgresjson.entity.UuidEntity import fr.postgresjson.serializer.Serializer -import fr.postgresjson.serializer.deserialize import fr.postgresjson.serializer.serialize import org.joda.time.DateTime import org.junit.jupiter.api.Assertions.assertEquals @@ -21,7 +20,6 @@ internal class SerializerTest { private val objSerialized: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528"}""" private val objSerializedWithExtra: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528","toto":"tata"}""" - private val objSerializedUpdate = """{"val1":"update","val2":123}""" private lateinit var obj: ObjTest @BeforeEach diff --git a/src/test/kotlin/fr/postgresjson/TestAbstract.kt b/src/test/kotlin/fr/postgresjson/TestAbstract.kt index dce0d78..433ed07 100644 --- a/src/test/kotlin/fr/postgresjson/TestAbstract.kt +++ b/src/test/kotlin/fr/postgresjson/TestAbstract.kt @@ -13,7 +13,7 @@ abstract class TestAbstract { @BeforeEach fun beforeAll() { - val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI()) + val initSQL = File(this::class.java.getResource("/fixtures/init.sql")!!.toURI()) connection .connect() .sendQuery(initSQL.readText()) @@ -22,9 +22,9 @@ abstract class TestAbstract { @AfterEach fun afterAll() { - val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI()) - connection.connect().apply { - sendQuery(downSQL.readText()).join() - }.disconnect() + val downSQL = File(this::class.java.getResource("/fixtures/down.sql")!!.toURI()) + connection + .apply { connect().sendQuery(downSQL.readText()).join() } + .disconnect() } } diff --git a/src/test/resources/sql/query/Test/selectMultiple.sql b/src/test/resources/sql/query/Test/selectMultiple.sql new file mode 100644 index 0000000..a8e41d6 --- /dev/null +++ b/src/test/resources/sql/query/Test/selectMultiple.sql @@ -0,0 +1,4 @@ +SELECT json_build_array( + json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', :name::text), + json_build_object('id', '6085c12e-e94d-4ae1-b7ad-23acc7a82a98', 'name', :name::text || '-2') +) \ No newline at end of file diff --git a/src/test/resources/sql/query/Test/selectMultipleOrderedArgs.sql b/src/test/resources/sql/query/Test/selectMultipleOrderedArgs.sql new file mode 100644 index 0000000..b7764a4 --- /dev/null +++ b/src/test/resources/sql/query/Test/selectMultipleOrderedArgs.sql @@ -0,0 +1,4 @@ +SELECT json_build_array( + json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', ?::text), + json_build_object('id', '6085c12e-e94d-4ae1-b7ad-23acc7a82a98', 'name', ?::text || '-2') +) \ No newline at end of file