feature #11: select with extra parameters
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
/out/
|
||||
/build/
|
||||
.gradle
|
||||
/var/log/
|
||||
@@ -13,13 +13,16 @@ import fr.postgresjson.utils.LoggerDelegate
|
||||
import org.slf4j.Logger
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
||||
typealias SelectCallback<T> = QueryResult.(List<T>) -> Unit
|
||||
typealias SelectPaginatedCallback<T> = QueryResult.(Paginated<T>) -> Unit
|
||||
|
||||
interface Executable {
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: List<Any?> = emptyList()): List<R>
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: Map<String, Any?>): List<R>
|
||||
fun <R: EntityI<*>> select(sql: String, page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>): Paginated<R>
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList(), block: SelectOneCallback<R> = {}): R?
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>, block: SelectOneCallback<R> = {}): R?
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: List<Any?> = emptyList(), block: SelectCallback<R> = {}): List<R>
|
||||
fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: Map<String, Any?>, block: SelectCallback<R> = {}): List<R>
|
||||
fun <R: EntityI<*>> select(sql: String, page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>, block: SelectPaginatedCallback<R> = {}): Paginated<R>
|
||||
fun exec(sql: String, values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(sql: String, values: Map<String, Any?>): QueryResult
|
||||
fun sendQuery(sql: String): QueryResult
|
||||
@@ -47,47 +50,52 @@ class Connection(
|
||||
|
||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
||||
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
null
|
||||
} else {
|
||||
serializer.deserialize(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(sql: String, values: List<Any?> = emptyList()): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(sql: String, values: List<Any?> = emptyList(), noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<R>, values: Map<String, Any?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
return replaceArgs(sql, values) {
|
||||
select(this.sql, typeReference, this.parameters)
|
||||
select(this.sql, typeReference, this.parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(sql: String, values: Map<String, Any?>): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(sql: String, values: Map<String, Any?>, noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: List<Any?>): List<R> {
|
||||
override fun <R: EntityI<*>> select(sql: String, typeReference: TypeReference<List<R>>, values: List<Any?>, block: (QueryResult, List<R>) -> Unit): List<R> {
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
listOf<EntityI<*>>() as List<R>
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(sql: String, values: List<Any?> = emptyList()): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(sql: String, values: List<Any?> = emptyList(), noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, Paginated<R>) -> Unit
|
||||
): Paginated<R> {
|
||||
val offset = (page - 1) * limit
|
||||
val newValues = values
|
||||
@@ -95,11 +103,11 @@ class Connection(
|
||||
.plus("limit" to limit)
|
||||
|
||||
val line = replaceArgs(sql, newValues) {
|
||||
exec(this.sql, compileArgs(this.parameters)).rows[0]
|
||||
exec(this.sql, compileArgs(this.parameters))
|
||||
}
|
||||
|
||||
return line.run {
|
||||
val json = getString(0)
|
||||
val json = rows[0].getString(0)
|
||||
val entities = if (json === null) {
|
||||
listOf<EntityI<*>>() as List<R>
|
||||
} else {
|
||||
@@ -109,8 +117,10 @@ class Connection(
|
||||
entities,
|
||||
offset,
|
||||
limit,
|
||||
getInt("total") ?: error("The query not return total")
|
||||
rows[0].getInt("total") ?: error("The query not return total")
|
||||
)
|
||||
}.also {
|
||||
block(line, it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,22 +128,24 @@ class Connection(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap()
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(sql, page, limit, object: TypeReference<List<R>>() {}, values)
|
||||
select(sql, page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
): List<R> {
|
||||
return replaceArgs(sql, values) {
|
||||
select(this.sql, typeReference, this.parameters)
|
||||
select(this.sql, typeReference, this.parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(sql: String, values: Map<String, Any?>): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(sql: String, values: Map<String, Any?>, noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun exec(sql: String, values: List<Any?>): QueryResult {
|
||||
return stopwatchQuery(sql, values) {
|
||||
@@ -156,9 +168,9 @@ class Connection(
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is EntityI<*>) {
|
||||
val json = serializer.serialize(it)
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
||||
json
|
||||
serializer.serialize(it).apply {
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
||||
}
|
||||
} else {
|
||||
it
|
||||
}
|
||||
|
||||
@@ -78,35 +78,39 @@ class Requester(
|
||||
return sql
|
||||
}
|
||||
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: List<Any?> = emptyList(), noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: Map<String, Any?>, noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: List<Any?>): List<R> {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
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()): List<R> = select(object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(values: List<Any?> = emptyList(), noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: Map<String, Any?>): List<R> {
|
||||
return connection.select(this.toString(), typeReference, values)
|
||||
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?>): List<R> = select(object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(values: Map<String, Any?>, noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>): Paginated<R> {
|
||||
return connection.select(this.toString(), page, limit, typeReference, values)
|
||||
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)
|
||||
}
|
||||
inline fun <reified R: EntityI<*>> select(page: Int, limit: Int, values: Map<String, Any?> = emptyMap()): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values)
|
||||
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)
|
||||
|
||||
override fun exec(values: List<Any?>): QueryResult {
|
||||
return connection.exec(sql, values)
|
||||
@@ -125,52 +129,56 @@ class Requester(
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values)
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: List<Any?> = emptyList(), noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select One entity with named parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R? {
|
||||
override fun <R: EntityI<*>> select(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.select(sql, typeReference, values)
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: Map<String, Any?>): R? = select(object: TypeReference<R>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> selectOne(values: Map<String, Any?>, noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select list of entities with list of parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: List<Any?>): List<R> {
|
||||
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)
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(values: List<Any?> = emptyList()): List<R> = select(object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(values: List<Any?> = emptyList(), noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select list of entities with named parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: Map<String, Any?>): List<R> {
|
||||
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)
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(values: Map<String, Any?>): List<R> = select(object: TypeReference<List<R>>() {}, values)
|
||||
inline fun <reified R: EntityI<*>> select(values: Map<String, Any?>, noinline block: SelectCallback<R> = {}): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>): Paginated<R> {
|
||||
override fun <R: EntityI<*>> select(page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>, block: (QueryResult, Paginated<R>) -> Unit): Paginated<R> {
|
||||
val offset = (page - 1) * limit
|
||||
val newValues = values
|
||||
.plus("offset" to offset)
|
||||
@@ -179,10 +187,10 @@ class Requester(
|
||||
val args = compileArgs(newValues)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, page, limit, typeReference, values)
|
||||
return connection.select(sql, page, limit, typeReference, values, block)
|
||||
}
|
||||
inline fun <reified R: EntityI<*>> select(page: Int, limit: Int, values: Map<String, Any?> = emptyMap()): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values)
|
||||
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)
|
||||
|
||||
override fun exec(values: List<Any?>): QueryResult {
|
||||
val args = compileArgs(values)
|
||||
@@ -230,13 +238,13 @@ class Requester(
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>): R?
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList(), block: SelectOneCallback<R> = {}): R?
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<R>, values: Map<String, Any?>, block: SelectOneCallback<R> = {}): R?
|
||||
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: List<Any?> = emptyList()): List<R>
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: Map<String, Any?>): List<R>
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: List<Any?> = emptyList(), block: SelectCallback<R> = {}): List<R>
|
||||
fun <R: EntityI<*>> select(typeReference: TypeReference<List<R>>, values: Map<String, Any?>, block: SelectCallback<R> = {}): List<R>
|
||||
|
||||
fun <R: EntityI<*>> select(page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>): Paginated<R>
|
||||
fun <R: EntityI<*>> select(page: Int, limit: Int, typeReference: TypeReference<List<R>>, values: Map<String, Any?>, block: SelectPaginatedCallback<R> = {}): Paginated<R>
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
|
||||
@@ -151,4 +151,26 @@ class ConnectionTest(): TestAbstract() {
|
||||
assertEquals(result.total, 10)
|
||||
assertEquals(result.offset, 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selectOne with extra 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), 'plop'::text as other
|
||||
""".trimIndent(),
|
||||
params
|
||||
) {
|
||||
assertEquals("ff", it!!.first)
|
||||
assertEquals("plop", rows[0].getString("other"))
|
||||
}
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result!!.first)
|
||||
assertEquals("sec", result.seconde)
|
||||
assertEquals(123, result.third)
|
||||
}
|
||||
}
|
||||
@@ -117,4 +117,18 @@ class RequesterTest: TestAbstract() {
|
||||
Assert.assertEquals(result.total, 10)
|
||||
Assert.assertEquals(result.offset, 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
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", it!!.name)
|
||||
Assert.assertEquals("plop", rows[0].getString("other"))
|
||||
}!!
|
||||
|
||||
assertEquals("myName", obj.name)
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
select json_build_object('id', 2, 'name', :name::text);
|
||||
select json_build_object('id', 2, 'name', :name::text), 'plop'::text as other;
|
||||
Reference in New Issue
Block a user