Extract Reified method to Extension

Add update method
This commit is contained in:
2021-07-16 01:53:12 +02:00
parent eb3a732440
commit 69f85b5bf5
14 changed files with 357 additions and 212 deletions

View File

@@ -44,7 +44,7 @@ class Connection(
}
fun disconnect() {
connection?.run { disconnect() }
connection?.disconnect()
}
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
@@ -97,7 +97,7 @@ class Connection(
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
val result = exec(sql, compileArgs(values))
val result = exec(sql, values)
val json = result.rows[0].getString(0)
return if (json === null) {
listOf<EntityI>() as List<R>

View File

@@ -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 <R : EntityI> update(
typeReference: TypeReference<R>,
value: R,
block: SelectOneCallback<R> = {}
): R? =
selectOne(typeReference, listOf(value), block)
/**
* Select One [EntityI] with [List] of parameters
*/
fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
@@ -19,26 +30,60 @@ sealed interface EmbedExecutable {
block: SelectOneCallback<R> = {}
): R?
/**
* Select One [EntityI] with [Map] of parameters
*/
fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
values: Map<String, Any?>,
block: SelectOneCallback<R> = {}
): R?
/**
* Select One [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
vararg values: Pair<String, Any?>,
block: SelectOneCallback<R> = {}
): R? =
selectOne(typeReference, values.toMap(), block)
/* Select Multiples */
/**
* Select Multiple [EntityI] with [List] of parameters
*/
fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: List<Any?> = emptyList(),
block: SelectCallback<R> = {}
): List<R>
/**
* Select Multiple [EntityI] with [Map] of parameters
*/
fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: SelectCallback<R> = {}
): List<R>
/**
* Select Multiple [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
vararg values: Pair<String, Any?>,
block: SelectCallback<R> = {}
): List<R> =
select(typeReference, values.toMap(), block)
/* Select Paginated */
/**
* Select Paginated [EntityI] with [Map] of parameters
*/
fun <R : EntityI> select(
page: Int,
limit: Int,
@@ -47,6 +92,18 @@ sealed interface EmbedExecutable {
block: SelectPaginatedCallback<R> = {}
): Paginated<R>
/**
* Select Paginated [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> select(
page: Int,
limit: Int,
typeReference: TypeReference<List<R>>,
vararg values: Pair<String, Any?>,
block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, typeReference, values.toMap(), block)
fun exec(values: List<Any?> = emptyList()): QueryResult
fun exec(values: Map<String, Any?>): QueryResult
fun exec(vararg values: Pair<String, Any?>): QueryResult = exec(values.toMap())
@@ -57,6 +114,5 @@ sealed interface EmbedExecutable {
fun sendQuery(values: List<Any?> = emptyList()): Int
fun sendQuery(values: Map<String, Any?>): Int
fun sendQuery(vararg values: Pair<String, Any?>): Int =
sendQuery(values.toMap())
fun sendQuery(vararg values: Pair<String, Any?>): Int = sendQuery(values.toMap())
}

View File

@@ -0,0 +1,68 @@
package fr.postgresjson.connexion
import com.fasterxml.jackson.core.type.TypeReference
import fr.postgresjson.entity.EntityI
/* Select One */
inline fun <reified R : EntityI> EmbedExecutable.update(
value: R,
noinline block: SelectOneCallback<R> = {}
): R? =
update(object : TypeReference<R>() {}, value, block)
inline fun <reified R : EntityI> EmbedExecutable.selectOne(
values: List<Any?> = emptyList(),
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
inline fun <reified R : EntityI> EmbedExecutable.selectOne(
values: Map<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
inline fun <reified R : EntityI> EmbedExecutable.selectOne(
vararg values: Pair<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values = values, block)
/* Select Multiples */
inline fun <reified R : EntityI> EmbedExecutable.select(
values: List<Any?> = emptyList(),
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
inline fun <reified R : EntityI> EmbedExecutable.select(
values: Map<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
inline fun <reified R : EntityI> EmbedExecutable.select(
vararg values: Pair<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values = values, block)
/* Select Paginated */
inline fun <reified R : EntityI> EmbedExecutable.select(
page: Int,
limit: Int,
values: Map<String, Any?> = emptyMap(),
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, object : TypeReference<List<R>>() {}, values, block)
inline fun <reified R : EntityI> EmbedExecutable.select(
page: Int,
limit: Int,
vararg values: Pair<String, Any?>,
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, object : TypeReference<List<R>>() {}, values = values, block)

View File

@@ -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 <R : EntityI> update(
sql: String,
typeReference: TypeReference<R>,
value: R,
block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, typeReference, listOf(value), block)
/**
* Select One [EntityI] with [List] of parameters
*/
fun <R : EntityI> selectOne(
sql: String,
typeReference: TypeReference<R>,
@@ -14,6 +29,9 @@ interface Executable {
block: SelectOneCallback<R> = {}
): R?
/**
* Select One [EntityI] with [Map] of parameters
*/
fun <R : EntityI> selectOne(
sql: String,
typeReference: TypeReference<R>,
@@ -21,8 +39,22 @@ interface Executable {
block: SelectOneCallback<R> = {}
): R?
/**
* Select One [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> selectOne(
sql: String,
typeReference: TypeReference<R>,
vararg values: Pair<String, Any?>,
block: SelectOneCallback<R> = {}
): R? =
selectOne(sql, typeReference, values.toMap(), block)
/* Select Multiples */
/**
* Select Multiple [EntityI] with [List] of parameters
*/
fun <R : EntityI> select(
sql: String,
typeReference: TypeReference<List<R>>,
@@ -30,6 +62,9 @@ interface Executable {
block: SelectCallback<R> = {}
): List<R>
/**
* Select Multiple [EntityI] with [Map] of parameters
*/
fun <R : EntityI> select(
sql: String,
typeReference: TypeReference<List<R>>,
@@ -37,8 +72,22 @@ interface Executable {
block: SelectCallback<R> = {}
): List<R>
/**
* Select Multiple [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> select(
sql: String,
typeReference: TypeReference<List<R>>,
vararg values: Pair<String, Any?>,
block: SelectCallback<R> = {}
): List<R> =
select(sql, typeReference, values.toMap(), block)
/* Select Paginated */
/**
* Select Paginated [EntityI] with [Map] of parameters
*/
fun <R : EntityI> select(
sql: String,
page: Int,
@@ -48,8 +97,31 @@ interface Executable {
block: SelectPaginatedCallback<R> = {}
): Paginated<R>
/**
* Select Paginated [EntityI] with multiple [Pair] of parameters
*/
fun <R : EntityI> select(
sql: String,
page: Int,
limit: Int,
typeReference: TypeReference<List<R>>,
vararg values: Pair<String, Any?>,
block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(sql, page, limit, typeReference, values.toMap(), block)
fun <R : EntityI> exec(sql: String, value: R): QueryResult = exec(sql, listOf(value))
fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult
fun exec(sql: String, values: Map<String, Any?>): QueryResult
fun exec(sql: String, vararg values: Pair<String, Any?>): QueryResult = exec(sql, values.toMap())
fun <R : EntityI> perform(sql: String, value: R) { perform(sql, listOf(value)) }
fun perform(sql: String, values: List<Any?>) { exec(sql, values) }
fun perform(sql: String, values: Map<String, Any?>) { exec(sql, values) }
fun perform(sql: String, vararg values: Pair<String, Any?>) = perform(sql, values.toMap())
fun <R : EntityI> sendQuery(sql: String, value: R): Int = sendQuery(sql, listOf(value))
fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
fun sendQuery(sql: String, values: Map<String, Any?>): Int
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): Int = sendQuery(sql, values.toMap())
}

View File

@@ -12,112 +12,64 @@ class Function(val definition: Function, override val connection: Connection) :
override val name: String = definition.name
/**
* Update [EntityI]
*/
override fun <R : EntityI> update(
typeReference: TypeReference<R>,
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 <R : EntityI> selectOne(
typeReference: TypeReference<R>,
values: List<Any?>,
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 <reified R : EntityI> selectOne(
values: List<Any?> = emptyList(),
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
inline fun <reified R : EntityI> selectOne(
value: R,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, listOf(value), block)
connection.selectOne(compileSql(values), typeReference, values, block)
/**
* Select One entity with named parameters
* Select One [EntityI] with named parameters
*/
override fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
values: Map<String, Any?>,
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 <reified R : EntityI> selectOne(
values: Map<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
inline fun <reified R : EntityI> selectOne(
vararg values: Pair<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): 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 <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
return connection.select(sql, typeReference, values, block)
}
inline fun <reified R : EntityI> select(
values: List<Any?> = emptyList(),
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
connection.select(compileSql(values), typeReference, values, block)
/**
* Select list of entities with named parameters
* Select multiple [EntityI] with named parameters
*/
override fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
return connection.select(sql, typeReference, values, block)
}
inline fun <reified R : EntityI> select(
values: Map<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
inline fun <reified R : EntityI> select(
vararg values: Pair<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(values.toMap(), block)
connection.select(compileSql(values), typeReference, values, block)
/* Select Paginated */
/**
* Select Multiple with pagination
* Select Multiple [EntityI] with pagination
*/
override fun <R : EntityI> 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 <reified R : EntityI> select(
page: Int,
limit: Int,
values: Map<String, Any?> = emptyMap(),
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, object : TypeReference<List<R>>() {}, values, block)
/* Execute function without treatments */
inline fun <reified R : EntityI> select(
page: Int,
limit: Int,
vararg values: Pair<String, Any?>,
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, object : TypeReference<List<R>>() {}, values.toMap(), block)
override fun exec(values: List<Any?>): QueryResult = connection.exec(compileSql(values), values)
/* Execute function without traitements */
override fun exec(values: List<Any?>): QueryResult {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
return connection.exec(sql, values)
}
override fun exec(values: Map<String, Any?>): QueryResult {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
return connection.exec(sql, values)
}
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(compileSql(values), values)
override fun sendQuery(values: List<Any?>): Int {
exec(values)
@@ -179,6 +102,8 @@ class Function(val definition: Function, override val connection: Connection) :
return 0
}
private fun <R : EntityI> compileArgs(value: R): String = compileArgs(listOf(value))
private fun compileArgs(values: List<Any?>): 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 <R : EntityI> compileSql(value: R): String = "SELECT * FROM ${definition.name} (${compileArgs(value)})"
private fun compileSql(values: List<Any?>): String = "SELECT * FROM ${definition.name} (${compileArgs(values)})"
private fun compileSql(values: Map<String, Any?>): String = "SELECT * FROM ${definition.name} (${compileArgs(values)})"
}

View File

@@ -9,101 +9,78 @@ class Query(override val name: String, private val sql: String, override val con
return sql
}
/**
* Update [EntityI]
*/
override fun <R : EntityI> update(
typeReference: TypeReference<R>,
value: R,
block: (QueryResult, R?) -> Unit
): R? =
connection.update(sql, typeReference, value, block)
/* Select One */
/**
* Select One [EntityI] with [List] of parameters
*/
override fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
values: List<Any?>,
block: (QueryResult, R?) -> Unit
): R? {
return connection.selectOne(this.toString(), typeReference, values, block)
}
inline fun <reified R : EntityI> selectOne(
values: List<Any?> = emptyList(),
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
connection.selectOne(sql, typeReference, values, block)
/**
* Select One [EntityI] with named parameters
*/
override fun <R : EntityI> selectOne(
typeReference: TypeReference<R>,
values: Map<String, Any?>,
block: (QueryResult, R?) -> Unit
): R? {
return connection.selectOne(this.toString(), typeReference, values, block)
}
inline fun <reified R : EntityI> selectOne(
values: Map<String, Any?>,
noinline block: SelectOneCallback<R> = {}
): R? =
selectOne(object : TypeReference<R>() {}, values, block)
connection.selectOne(sql, typeReference, values, block)
/* Select Multiples */
/**
* Select multiple [EntityI] with [List] of parameters
*/
override fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: List<Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
return connection.select(this.toString(), typeReference, values, block)
}
inline fun <reified R : EntityI> select(
values: List<Any?> = emptyList(),
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
connection.select(sql, typeReference, values, block)
override fun <R : EntityI> select(
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, List<R>) -> Unit
): List<R> {
return connection.select(this.toString(), typeReference, values, block)
}
inline fun <reified R : EntityI> select(
values: Map<String, Any?>,
noinline block: SelectCallback<R> = {}
): List<R> =
select(object : TypeReference<List<R>>() {}, values, block)
connection.select(sql, typeReference, values, block)
/* Select Paginated */
/**
* Select Multiple [EntityI] with pagination
*/
override fun <R : EntityI> select(
page: Int,
limit: Int,
typeReference: TypeReference<List<R>>,
values: Map<String, Any?>,
block: (QueryResult, Paginated<R>) -> Unit
): Paginated<R> {
return connection.select(this.toString(), page, limit, typeReference, values, block)
}
/* Select Paginated */
inline fun <reified R : EntityI> select(
page: Int,
limit: Int,
values: Map<String, Any?> = emptyMap(),
noinline block: SelectPaginatedCallback<R> = {}
): Paginated<R> =
select(page, limit, object : TypeReference<List<R>>() {}, values, block)
connection.select(sql, page, limit, typeReference, values, block)
/* Execute function without traitements */
/* Execute function without treatments */
override fun exec(values: List<Any?>): QueryResult {
return connection.exec(sql, values)
}
override fun exec(values: List<Any?>): QueryResult = connection.exec(sql, values)
override fun exec(values: Map<String, Any?>): QueryResult {
return connection.exec(sql, values)
}
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(sql, values)
override fun sendQuery(values: List<Any?>): Int {
return connection.sendQuery(sql, values)
}
override fun sendQuery(values: List<Any?>): Int = connection.sendQuery(sql, values)
override fun sendQuery(values: Map<String, Any?>): Int {
return connection.sendQuery(sql, values)
}
override fun sendQuery(values: Map<String, Any?>): Int = connection.sendQuery(sql, values)
}

View File

@@ -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()

View File

@@ -16,11 +16,11 @@ import java.util.UUID
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class EntityTest() {
private class User(id: UUID = UUID.randomUUID()) : Entity<UUID>(id)
private class ObjTest(var name: String) : UuidEntityExtended<Int?, User>(User(), User())
private class ObjTest(val name: String) : UuidEntityExtended<Int?, User>(User(), User())
@Test
fun getObject() {
val obj: ObjTest? = ObjTest("plop")
val obj = ObjTest("plop")
assertTrue(obj is ObjTest)
assertTrue(obj is UuidEntityExtended<Int?, User>)
assertTrue(obj is EntityI)

View File

@@ -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
}

View File

@@ -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<ObjTest>? = Requester(connection, functionsDirectory = resources)
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
val obj: List<ObjTest> = 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<ObjTest>? = Requester(connection, functionsDirectory = resources)
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
val obj: List<ObjTest> = 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<ObjTest> = 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<ObjTest> = 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<ObjTest> = 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<ObjTest> = 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<ObjTest> = 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")) {

View File

@@ -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

View File

@@ -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()
}
}

View File

@@ -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')
)

View File

@@ -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')
)