Clean the request process
Remove paginated Remove Entity classes Add Annotation for serialize object rename selectOne/selectMultiple to execute
This commit is contained in:
@@ -3,21 +3,19 @@ package fr.postgresjson.connexion
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import com.github.jasync.sql.db.ResultSet
|
||||
import com.github.jasync.sql.db.general.ArrayRowData
|
||||
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.entity.Serializable
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
import kotlin.jvm.Throws
|
||||
import org.slf4j.Logger
|
||||
import kotlin.random.Random
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
|
||||
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
||||
typealias SelectCallback<T> = QueryResult.(List<T>) -> Unit
|
||||
typealias SelectPaginatedCallback<T> = QueryResult.(Paginated<T>) -> Unit
|
||||
// TODO move execute function outside
|
||||
// TODO create function executeNullable
|
||||
|
||||
class Connection(
|
||||
private val database: String,
|
||||
@@ -25,18 +23,18 @@ class Connection(
|
||||
private val password: String,
|
||||
private val host: String = "localhost",
|
||||
private val port: Int = 5432
|
||||
) : Executable {
|
||||
private var connection: ConnectionPool<PostgreSQLConnection>? = null
|
||||
) : ExecutableRaw {
|
||||
private var connectionPool: ConnectionPool<PostgreSQLConnection>? = null
|
||||
private val serializer = Serializer()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
|
||||
internal fun connect(): ConnectionPool<PostgreSQLConnection> {
|
||||
return connection.let { connectionPool ->
|
||||
return connectionPool.let { connectionPool ->
|
||||
if (connectionPool == null || !connectionPool.isConnected()) {
|
||||
PostgreSQLConnectionBuilder.createConnectionPool(
|
||||
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
|
||||
).also {
|
||||
connection = it
|
||||
this.connectionPool = it
|
||||
}
|
||||
} else {
|
||||
connectionPool
|
||||
@@ -45,7 +43,7 @@ class Connection(
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
connection?.disconnect()
|
||||
connectionPool?.disconnect()
|
||||
}
|
||||
|
||||
fun <A> inTransaction(block: Connection.() -> A?): A? = connect().run {
|
||||
@@ -59,16 +57,19 @@ class Connection(
|
||||
}
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [List] of parameters
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
@Throws(DataNotFoundException::class)
|
||||
override fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? {
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.rows.firstOrNull()?.getString(0)
|
||||
val result: QueryResult = exec(sql, compileArgs(values))
|
||||
if (result.rows.size == 0) throw DataNotFoundException(sql)
|
||||
|
||||
val json: String? = result.rows.firstOrNull()?.getString(0)
|
||||
return if (json === null) {
|
||||
null
|
||||
} else {
|
||||
@@ -79,119 +80,16 @@ class Connection(
|
||||
}
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with named parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
override fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? {
|
||||
return replaceArgs(sql, values) {
|
||||
selectOne(this.sql, typeReference, parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
/* Select Multiples */
|
||||
|
||||
/**
|
||||
* Select multiple [EntityI] with [List] of parameters
|
||||
*/
|
||||
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, values)
|
||||
val json = result.rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
emptyList()
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select multiple [EntityI] with [Map] of parameters
|
||||
*/
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(List<R>) -> Unit
|
||||
): List<R> {
|
||||
return replaceArgs(sql, values) {
|
||||
select(this.sql, typeReference, this.parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select multiple or one [EntityI] with [Map] of parameters
|
||||
*/
|
||||
override fun <R : Any?> selectAny(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(R) -> Unit
|
||||
): R {
|
||||
val result = exec(sql, values)
|
||||
val json = result.rows[0].getString(0)
|
||||
return if (json === null) {
|
||||
null as R
|
||||
} else {
|
||||
serializer.deserialize(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
}
|
||||
}
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
/**
|
||||
* Select Multiple [EntityI] with pagination
|
||||
*/
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
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 line = replaceArgs(sql, newValues) {
|
||||
exec(this.sql, this.parameters)
|
||||
}
|
||||
|
||||
return line.run {
|
||||
val firstLine = rows.firstOrNull() ?: queryError("The query has no return", sql, newValues)
|
||||
if (!rows.columnNames().contains("total")) queryError("""The query not return the "total" column""", sql, newValues, rows)
|
||||
val total = try {
|
||||
firstLine.getInt("total") ?: queryError("The query return \"total\" must not be null", sql, newValues, rows)
|
||||
} catch (e: ClassCastException) {
|
||||
queryError("""Column "total" must be an integer""", sql, newValues, rows)
|
||||
}
|
||||
val json = firstLine.getString(0)
|
||||
val entities = if (json == null) {
|
||||
emptyList()
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}
|
||||
Paginated(
|
||||
entities,
|
||||
offset,
|
||||
limit,
|
||||
total
|
||||
)
|
||||
}.also {
|
||||
block(line, it)
|
||||
execute(this.sql, typeReference, parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,10 +129,12 @@ class Connection(
|
||||
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is Serializable || (it is List<*> && it.firstOrNull() is Serializable)) {
|
||||
serializer.serialize(it)
|
||||
} else {
|
||||
it
|
||||
when {
|
||||
it == null -> it
|
||||
it is List<*> && it.isEmpty() -> it
|
||||
it is List<*> && it.first()!!::class.hasAnnotation<SqlSerializable>() -> serializer.serialize(it)
|
||||
it::class.hasAnnotation<SqlSerializable>() -> serializer.serialize(it)
|
||||
else -> it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
class DataNotFoundException(val queryExecuted: String): Exception() {
|
||||
override val message: String
|
||||
get() = "No data return for the query"
|
||||
}
|
||||
@@ -2,116 +2,43 @@ package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
sealed interface EmbedExecutable {
|
||||
sealed interface EmbedExecutable : Executable {
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
val name: String
|
||||
|
||||
/* Select One */
|
||||
|
||||
/**
|
||||
* Update [EntityI] with one entity as argument
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
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(
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [Map] of parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
fun <R : EntityI> selectOne(
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with multiple [Pair] of parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
fun <R : EntityI> selectOne(
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
vararg values: Pair<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<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?>,
|
||||
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 Multiple or One [EntityI] with multiple [Pair] of parameters
|
||||
*/
|
||||
fun <R : Any?> selectAny(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(R) -> Unit = {}
|
||||
): R
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
/**
|
||||
* Select Paginated [EntityI] with [Map] of parameters
|
||||
*/
|
||||
fun <R : EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
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)
|
||||
execute(typeReference, values.toMap(), block)
|
||||
|
||||
fun exec(values: List<Any?>): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
|
||||
@@ -1,68 +1,25 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
/* 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?>,
|
||||
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(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
values: List<Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R : EntityI> EmbedExecutable.select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R : EntityI> EmbedExecutable.select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
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)
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values = values, block)
|
||||
|
||||
@@ -1,146 +1,3 @@
|
||||
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 {
|
||||
|
||||
/* Update */
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [List] of parameters
|
||||
*/
|
||||
fun <R : EntityI> selectOne(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [Map] of parameters
|
||||
*/
|
||||
fun <R : EntityI> selectOne(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
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>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/**
|
||||
* Select Multiple [EntityI] with [Map] of parameters
|
||||
*/
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/**
|
||||
* Select Multiple or One [EntityI] with [Map] of parameters
|
||||
*/
|
||||
fun <R : Any?> selectAny(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(R) -> Unit = {}
|
||||
): 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,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
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?>): QueryResult
|
||||
fun exec(sql: String, values: Map<String, Any?>): QueryResult
|
||||
fun exec(sql: String, vararg values: Pair<String, Any?>): QueryResult = exec(sql, values.toMap())
|
||||
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun <R : EntityI> sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: List<Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): QueryResult = sendQuery(sql, values.toMap())
|
||||
}
|
||||
sealed interface Executable
|
||||
76
src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
Normal file
76
src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
Normal file
@@ -0,0 +1,76 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
typealias SelectCallback<R> = QueryResult.(R?) -> Unit
|
||||
|
||||
sealed interface ExecutableRaw : Executable {
|
||||
/**
|
||||
* Select with one entity as argument
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
value: R,
|
||||
block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(sql, typeReference, listOf(value), block)
|
||||
|
||||
/**
|
||||
* Select with [List] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select with [Map] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select with multiple [Pair] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
vararg values: Pair<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): R? = execute(sql, typeReference, values.toMap(), block)
|
||||
|
||||
fun <R : Any?> exec(sql: String, value: R): QueryResult = exec(sql, listOf(value))
|
||||
fun exec(sql: String, values: List<Any?>): QueryResult
|
||||
fun exec(sql: String, values: Map<String, Any?>): QueryResult
|
||||
fun exec(sql: String, vararg values: Pair<String, Any?>): QueryResult = exec(sql, values.toMap())
|
||||
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun <R : Any?> sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: List<Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): QueryResult = sendQuery(sql, values.toMap())
|
||||
}
|
||||
@@ -1,106 +1,37 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
/* Update */
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
/**
|
||||
* Update [EntityI] with one entity as argument
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.update(
|
||||
sql: String,
|
||||
value: R,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
update(sql, object : TypeReference<R>() {}, value, block)
|
||||
|
||||
/* Select One */
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [List] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.selectOne(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
selectOne(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [Map] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.selectOne(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
selectOne(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with multiple [Pair] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.selectOne(
|
||||
sql: String,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
selectOne(sql, object : TypeReference<R>() {}, values = values, block)
|
||||
|
||||
/* Select Multiples */
|
||||
|
||||
/**
|
||||
* Select Multiple [EntityI] with [List] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object : TypeReference<List<R>>() {}, values, block)
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select Multiple [EntityI] with [Map] of parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object : TypeReference<List<R>>() {}, values, block)
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select Multiple [EntityI] with multiple [Pair] of parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object : TypeReference<List<R>>() {}, values = values, block)
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
/**
|
||||
* Select Paginated [EntityI] with [Map] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(sql, page, limit, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select Paginated [EntityI] with multiple [Pair] of parameters
|
||||
*/
|
||||
inline fun <reified R : EntityI> Executable.select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(sql, page, limit, object : TypeReference<List<R>>() {}, values = values, block)
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values = values, block)
|
||||
|
||||
@@ -3,7 +3,6 @@ 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 {
|
||||
@@ -12,86 +11,42 @@ class Function(val definition: Function, override val connection: Connection) :
|
||||
|
||||
override val name: String = definition.name
|
||||
|
||||
/* Select One */
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [List] of parameters
|
||||
* Select with [List] of parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
connection.selectOne(compileSql(values), typeReference, values, block)
|
||||
connection.execute(compileSql(values), typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with named parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
connection.selectOne(compileSql(values), typeReference, values, block)
|
||||
|
||||
/* Select Multiples */
|
||||
connection.execute(compileSql(values), typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select multiple [EntityI] with [List] of parameters
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
): List<R> =
|
||||
connection.select(compileSql(values), typeReference, values, block)
|
||||
|
||||
/**
|
||||
* 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> =
|
||||
connection.select(compileSql(values), typeReference, values, block)
|
||||
|
||||
override fun <R : Any?> selectAny(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(R) -> Unit
|
||||
): R =
|
||||
connection.selectAny(compileSql(values.toMap()), typeReference, values.toMap(), 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> {
|
||||
val offset = (page - 1) * limit
|
||||
val newValues = values
|
||||
.plus("offset" to offset)
|
||||
.plus("limit" to limit)
|
||||
|
||||
return connection.select(compileSql(newValues), page, limit, typeReference, values, block)
|
||||
}
|
||||
|
||||
/* Execute function without treatments */
|
||||
|
||||
override fun exec(values: List<Any?>): QueryResult = connection.exec(compileSql(values), values)
|
||||
|
||||
/**
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(compileSql(values), values)
|
||||
|
||||
private fun <R : EntityI> compileArgs(value: R): String = compileArgs(listOf(value))
|
||||
private fun <A : Any?> compileParameters(value: A): String = compileParameters(listOf(value))
|
||||
|
||||
private fun compileArgs(values: List<Any?>): String {
|
||||
/**
|
||||
* Add cast to all parameters
|
||||
*/
|
||||
private fun compileParameters(values: List<Any?>): String {
|
||||
val placeholders = values
|
||||
.filterIndexed { index, value ->
|
||||
definition.parameters[index].default === null || value != null
|
||||
@@ -103,7 +58,10 @@ class Function(val definition: Function, override val connection: Connection) :
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
|
||||
private fun compileArgs(values: Map<String, Any?>): String {
|
||||
/**
|
||||
* Cast and add named parameters
|
||||
*/
|
||||
private fun compileParameters(values: Map<String, Any?>): String {
|
||||
val parameters = definition.getParametersIndexedByName()
|
||||
val placeholders = values
|
||||
.filter { entry ->
|
||||
@@ -118,7 +76,16 @@ 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)})"
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun <A : Any?> compileSql(value: A): String = "SELECT * FROM ${definition.name} (${compileParameters(value)})"
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun compileSql(values: List<Any?>): String = "SELECT * FROM ${definition.name} (${compileParameters(values)})"
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun compileSql(values: Map<String, Any?>): String = "SELECT * FROM ${definition.name} (${compileParameters(values)})"
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.math.ceil
|
||||
|
||||
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.size
|
||||
val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil()
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greater or equal than 0")
|
||||
if (limit < 1) error("limit must be greater or equal than 1")
|
||||
if (total < 0) error("total must be greater or equal than 0")
|
||||
}
|
||||
|
||||
fun isLastPage(): Boolean = currentPage >= totalPages
|
||||
|
||||
private fun Double.ceil(): Int = ceil(this).toInt()
|
||||
}
|
||||
@@ -2,81 +2,40 @@ 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(override val name: String, private val sql: String, override val connection: Connection) : EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return sql
|
||||
}
|
||||
|
||||
/* Select One */
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with [List] of parameters
|
||||
* Select with unnamed of parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: SelectOneCallback<R>
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
connection.selectOne(sql, typeReference, values, block)
|
||||
connection.execute(sql, typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select One [EntityI] with named parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R : EntityI> selectOne(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R>
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
connection.selectOne(sql, typeReference, values, block)
|
||||
|
||||
/* Select Multiples */
|
||||
connection.execute(sql, typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select multiple [EntityI] with [List] of parameters
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: SelectCallback<R>
|
||||
): List<R> =
|
||||
connection.select(sql, typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select multiple [EntityI] with [Map] of parameters
|
||||
*/
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R>
|
||||
): List<R> =
|
||||
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> =
|
||||
connection.select(sql, page, limit, typeReference, values, block)
|
||||
|
||||
override fun <R> selectAny(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: QueryResult.(R) -> Unit
|
||||
): R = connection.selectAny(sql, typeReference, values.toMap(), block)
|
||||
|
||||
/* Execute function without treatments */
|
||||
|
||||
override fun exec(values: List<Any?>): QueryResult = connection.exec(sql, values)
|
||||
|
||||
/**
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(sql, values)
|
||||
|
||||
/**
|
||||
@@ -88,6 +47,7 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(values: Map<String, Any?>): QueryResult = connection.sendQuery(sql, values)
|
||||
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
|
||||
@@ -4,12 +4,24 @@ import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
/**
|
||||
* Convert [QueryDefinition], to runnable [Query]
|
||||
*/
|
||||
fun QueryDefinition.toRunnable(connection: Connection): Query = Query(name, script, connection)
|
||||
|
||||
/**
|
||||
* Convert Sequence of [QueryDefinition], to runnable Sequence of [Query]
|
||||
*/
|
||||
fun Sequence<QueryDefinition>.toRunnable(connection: Connection): Sequence<Query> = map { it.toRunnable(connection) }
|
||||
|
||||
/**
|
||||
* Convert Sequence of [Query], to [Map] of [Query] with name as key
|
||||
*/
|
||||
fun Sequence<Query>.toMutableMap(): MutableMap<String, Query> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
/**
|
||||
* Create a [Map] of [Query] from a [URI] pointing to the queries folder
|
||||
*/
|
||||
internal fun URI.toQuery(connection: Connection): MutableMap<String, Query> = searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.toRunnable(connection)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
annotation class SqlSerializable
|
||||
Reference in New Issue
Block a user