refactoring: Moves class into separated files
This commit is contained in:
@@ -6,7 +6,6 @@ import com.github.jasync.sql.db.QueryResult
|
||||
import com.github.jasync.sql.db.pool.ConnectionPool
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
||||
import com.github.jasync.sql.db.util.length
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
@@ -17,17 +16,6 @@ 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(), 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
|
||||
}
|
||||
|
||||
class Connection(
|
||||
private val database: String,
|
||||
private val username: String,
|
||||
@@ -210,20 +198,3 @@ class Connection(
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
data class Paginated<T: EntityI<*>>(
|
||||
val result: List<T>,
|
||||
val offset: Int,
|
||||
val limit: Int,
|
||||
val total: Int
|
||||
) {
|
||||
val currentPage: Int = (offset / limit) + 1
|
||||
val count: Int = result.length
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greather or equal than 0")
|
||||
if (limit < 1) error("limit must be greather than 1")
|
||||
if (total < 1) error("total must be greather or equal than 0")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt
Normal file
21
src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
interface EmbedExecutable {
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
|
||||
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(), 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?>, block: SelectPaginatedCallback<R> = {}): Paginated<R>
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
}
|
||||
16
src/main/kotlin/fr/postgresjson/connexion/Executable.kt
Normal file
16
src/main/kotlin/fr/postgresjson/connexion/Executable.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
interface Executable {
|
||||
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
|
||||
}
|
||||
119
src/main/kotlin/fr/postgresjson/connexion/Function.kt
Normal file
119
src/main/kotlin/fr/postgresjson/connexion/Function.kt
Normal file
@@ -0,0 +1,119 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.definition.Function
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
class Function(val definition: Function, override val connection: Connection): EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return definition.name
|
||||
}
|
||||
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
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, block)
|
||||
}
|
||||
|
||||
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?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
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?>, 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)
|
||||
|
||||
/**
|
||||
* Select list of entities 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)
|
||||
|
||||
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)
|
||||
.plus("limit" to limit)
|
||||
|
||||
val args = compileArgs(newValues)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, 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)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
private fun compileArgs(values: List<Any?>): String {
|
||||
val placeholders = values
|
||||
.filterIndexed { index, any ->
|
||||
definition.parameters[index].default === null || any !== null
|
||||
}
|
||||
.mapIndexed { index, any ->
|
||||
"?::" + definition.parameters[index].type
|
||||
}
|
||||
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
|
||||
private fun compileArgs(values: Map<String, Any?>): String {
|
||||
val parameters = definition.getParametersIndexedByName()
|
||||
val placeholders = values
|
||||
.filter { entry ->
|
||||
val parameter = parameters[entry.key] ?: error("Parameter ${entry.key} not exist")
|
||||
parameter.default === null || entry.value !== null
|
||||
}
|
||||
.map { entry ->
|
||||
val parameter = parameters[entry.key]!!
|
||||
""""${parameter.name}" := :${parameter.name}::${parameter.type}"""
|
||||
}
|
||||
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
}
|
||||
20
src/main/kotlin/fr/postgresjson/connexion/Paginated.kt
Normal file
20
src/main/kotlin/fr/postgresjson/connexion/Paginated.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.github.jasync.sql.db.util.length
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
data class Paginated<T: EntityI<*>>(
|
||||
val result: List<T>,
|
||||
val offset: Int,
|
||||
val limit: Int,
|
||||
val total: Int
|
||||
) {
|
||||
val currentPage: Int = (offset / limit) + 1
|
||||
val count: Int = result.length
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greather or equal than 0")
|
||||
if (limit < 1) error("limit must be greather than 1")
|
||||
if (total < 1) error("total must be greather or equal than 0")
|
||||
}
|
||||
}
|
||||
54
src/main/kotlin/fr/postgresjson/connexion/Query.kt
Normal file
54
src/main/kotlin/fr/postgresjson/connexion/Query.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
|
||||
class Query(private val sql: String, override val connection: Connection): EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return sql
|
||||
}
|
||||
|
||||
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(), noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
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?>, noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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(), 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)
|
||||
}
|
||||
|
||||
override fun exec(values: Map<String, Any?>): QueryResult {
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.io.File
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
@@ -73,183 +70,6 @@ class Requester(
|
||||
return queries[path]!!
|
||||
}
|
||||
|
||||
class Query(private val sql: String, override val connection: Connection): Executable {
|
||||
override fun toString(): String {
|
||||
return sql
|
||||
}
|
||||
|
||||
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(), noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
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?>, noinline block: SelectOneCallback<R> = {}): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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(), 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)
|
||||
}
|
||||
|
||||
override fun exec(values: Map<String, Any?>): QueryResult {
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
}
|
||||
|
||||
class Function(val definition: DefinitionFunction, override val connection: Connection): Executable {
|
||||
override fun toString(): String {
|
||||
return definition.name
|
||||
}
|
||||
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
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, block)
|
||||
}
|
||||
|
||||
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?>, block: (QueryResult, R?) -> Unit): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
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?>, 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)
|
||||
|
||||
/**
|
||||
* Select list of entities 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)
|
||||
|
||||
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)
|
||||
.plus("limit" to limit)
|
||||
|
||||
val args = compileArgs(newValues)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, 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)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
private fun compileArgs(values: List<Any?>): String {
|
||||
val placeholders = values
|
||||
.filterIndexed { index, any ->
|
||||
definition.parameters[index].default === null || any !== null
|
||||
}
|
||||
.mapIndexed { index, any ->
|
||||
"?::" + definition.parameters[index].type
|
||||
}
|
||||
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
|
||||
private fun compileArgs(values: Map<String, Any?>): String {
|
||||
val parameters = definition.getParametersIndexedByName()
|
||||
val placeholders = values
|
||||
.filter { entry ->
|
||||
val parameter = parameters[entry.key] ?: error("Parameter ${entry.key} not exist")
|
||||
parameter.default === null || entry.value !== null
|
||||
}
|
||||
.map { entry ->
|
||||
val parameter = parameters[entry.key]!!
|
||||
""""${parameter.name}" := :${parameter.name}::${parameter.type}"""
|
||||
}
|
||||
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
}
|
||||
|
||||
interface Executable {
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
|
||||
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(), 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?>, block: SelectPaginatedCallback<R> = {}): Paginated<R>
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
}
|
||||
|
||||
class RequesterFactory(
|
||||
private val host: String = "localhost",
|
||||
private val port: Int = 5432,
|
||||
|
||||
Reference in New Issue
Block a user