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
|
||||
@@ -1,126 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
|
||||
interface EntityRefI<T> : EntityI {
|
||||
val id: T
|
||||
}
|
||||
|
||||
interface UuidEntityI : EntityRefI<UUID> {
|
||||
override val id: UUID
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override val id: T) : EntityRefI<T>
|
||||
open class UuidEntity(id: UUID? = null) : UuidEntityI, Entity<UUID>(id ?: UUID.randomUUID())
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
val versionNumber: NUMBER
|
||||
val versionId: ID
|
||||
}
|
||||
|
||||
class UuidEntityVersioning(
|
||||
override val versionNumber: Int,
|
||||
versionId: UUID? = null
|
||||
) : EntityVersioning<UUID, Int> {
|
||||
override val versionId: UUID = versionId ?: UUID.randomUUID()
|
||||
}
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
val createdAt: DateTime
|
||||
}
|
||||
interface EntityUpdatedAt {
|
||||
val updatedAt: DateTime
|
||||
}
|
||||
|
||||
interface EntityDeletedAt {
|
||||
val deletedAt: DateTime?
|
||||
fun isDeleted(): Boolean {
|
||||
return deletedAt?.let {
|
||||
it < DateTime.now()
|
||||
} ?: false
|
||||
}
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp(
|
||||
override val createdAt: DateTime = DateTime.now()
|
||||
) : EntityCreatedAt
|
||||
|
||||
class EntityUpdatedAtImp(
|
||||
override val updatedAt: DateTime = DateTime.now()
|
||||
) : EntityUpdatedAt
|
||||
|
||||
class EntityDeletedAtImp(
|
||||
override val deletedAt: DateTime? = null
|
||||
) : EntityDeletedAt
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T : EntityI> {
|
||||
val createdBy: T
|
||||
}
|
||||
interface EntityUpdatedBy<T : EntityI> {
|
||||
val updatedBy: T
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T : EntityI> {
|
||||
val deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT : EntityI>(
|
||||
override val createdBy: UserT
|
||||
) : EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT : EntityI>(
|
||||
override val updatedBy: UserT
|
||||
) : EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT : EntityI>(
|
||||
override val deletedBy: UserT?
|
||||
) : EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityCreatedImp<UserT : EntityI>(
|
||||
override val createdAt: DateTime = DateTime.now(),
|
||||
createdBy: UserT
|
||||
) : EntityCreatedBy<UserT> by EntityCreatedByImp(createdBy),
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT : EntityI>(
|
||||
updatedAt: DateTime = DateTime.now(),
|
||||
override val updatedBy: UserT
|
||||
) : EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(updatedAt)
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT : EntityI> {
|
||||
val publishedAt: DateTime?
|
||||
val publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT : EntityI>(
|
||||
override val publishedBy: UserT?
|
||||
) : Published<UserT> {
|
||||
override val publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT : EntityI>(
|
||||
updatedBy: UserT,
|
||||
updatedAt: DateTime = DateTime.now()
|
||||
) : UuidEntity(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(updatedAt),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(updatedAt),
|
||||
EntityDeletedAt by EntityDeletedAtImp(),
|
||||
EntityCreatedBy<UserT> by EntityCreatedByImp(updatedBy),
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT : EntityI>(
|
||||
updatedBy: UserT,
|
||||
publishedBy: UserT?
|
||||
) :
|
||||
EntityImp<T, UserT>(updatedBy),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(0),
|
||||
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||
@@ -1,5 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
interface Serializable
|
||||
interface EntityI : Serializable
|
||||
interface Parameter : Serializable
|
||||
@@ -103,23 +103,21 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
||||
val hasReturn: Boolean = parameters.any { it.direction != IN } || (returns != "" && returns != "void")
|
||||
|
||||
val generics = mutableListOf<String>()
|
||||
if (hasReturn) generics.add("reified E: Any?")
|
||||
if (hasInputArgs) generics.add("S: Serializable")
|
||||
if (hasReturn) generics.add("reified E: Any")
|
||||
if (hasInputArgs) generics.add("S: Any?")
|
||||
|
||||
val functionDecl = if (generics.isNotEmpty()) "inline fun <${generics.joinToString(", ")}>" else "fun"
|
||||
|
||||
val importSerializable = if (hasInputArgs) "import fr.postgresjson.entity.Serializable\n" else ""
|
||||
|
||||
if (hasReturn) {
|
||||
return """
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import com.fasterxml.jackson.core.type.TypeReference
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|$importSerializable
|
||||
|$functionDecl Requester.$kotlinName($args): E {
|
||||
|
|
||||
|$functionDecl Requester.$kotlinName($args): E? {
|
||||
| return getFunction("$name")
|
||||
| .selectAny<E>(object : TypeReference<E>() {}, ${parameters.toMapOf()})
|
||||
| .execute<E>(object : TypeReference<E>() {}, ${parameters.toMapOf()})
|
||||
|}
|
||||
""".trimMargin()
|
||||
} else {
|
||||
@@ -127,7 +125,7 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|$importSerializable
|
||||
|
|
||||
|$functionDecl Requester.$kotlinName($args): Unit {
|
||||
| getFunction("$name")
|
||||
| .exec(${parameters.toMapOf()})
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.postgresjson.migration
|
||||
|
||||
import com.github.jasync.sql.db.postgresql.exceptions.GenericDatabaseException
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.selectOne
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import java.util.Date
|
||||
@@ -50,11 +50,11 @@ data class Function(
|
||||
|
||||
this::class.java.classLoader
|
||||
.getResource("sql/migration/insertFunction.sql")!!.readText()
|
||||
.let { connection.selectOne<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script)) }
|
||||
?.let { function ->
|
||||
executedAt = function.executedAt
|
||||
.let { connection.execute<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script)) }
|
||||
?.let { migration: MigrationEntity ->
|
||||
executedAt = migration.executedAt
|
||||
doExecute = Action.OK
|
||||
}
|
||||
} ?: error("No migration executed")
|
||||
|
||||
Status.OK
|
||||
} catch (e: Throwable) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package fr.postgresjson.migration
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.selectOne
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import java.util.Date
|
||||
|
||||
data class MigrationScript(
|
||||
@@ -12,40 +12,44 @@ data class MigrationScript(
|
||||
val down: String,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
) : Migration, Entity<String?>(name) {
|
||||
) : Migration {
|
||||
override var doExecute: Action? = null
|
||||
|
||||
override fun up(): Migration.Status {
|
||||
connection.sendQuery(up)
|
||||
override fun up(): Status {
|
||||
return try {
|
||||
connection.sendQuery(up)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
|
||||
connection.selectOne<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
|
||||
executedAt = query.executedAt
|
||||
doExecute = Action.OK
|
||||
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
|
||||
connection.execute<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
|
||||
executedAt = query.executedAt
|
||||
doExecute = Action.OK
|
||||
} ?: error("No migration executed")
|
||||
}
|
||||
}
|
||||
|
||||
return Migration.Status.OK
|
||||
Status.OK
|
||||
} catch (e: Throwable) {
|
||||
Status.UP_FAIL
|
||||
}
|
||||
}
|
||||
|
||||
override fun down(): Migration.Status {
|
||||
override fun down(): Status {
|
||||
connection.sendQuery(down)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
||||
connection.exec(it, listOf(name))
|
||||
}
|
||||
|
||||
return Migration.Status.OK
|
||||
return Status.OK
|
||||
}
|
||||
|
||||
override fun test(): Migration.Status {
|
||||
override fun test(): Status {
|
||||
connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
sendQuery("ROLLBACK")
|
||||
}
|
||||
|
||||
return Migration.Status.OK
|
||||
return Status.OK
|
||||
}
|
||||
|
||||
fun copy(): MigrationScript {
|
||||
|
||||
@@ -2,7 +2,6 @@ package fr.postgresjson.migration
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
@@ -20,7 +19,7 @@ class MigrationEntity(
|
||||
val up: String,
|
||||
val down: String,
|
||||
val version: Int
|
||||
) : Entity<String?>(filename)
|
||||
)
|
||||
|
||||
interface Migration {
|
||||
var executedAt: Date?
|
||||
@@ -76,15 +75,15 @@ class Migrations private constructor(
|
||||
*/
|
||||
private fun getMigrationFromDB() {
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
||||
connection.select(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
.map { function ->
|
||||
connection.execute(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
?.map { function ->
|
||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||
}
|
||||
}
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||
connection.select(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
.map { query ->
|
||||
connection.execute(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
?.map { query ->
|
||||
migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.fasterxml.jackson.datatype.joda.JodaModule
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import fr.postgresjson.entity.Serializable
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
|
||||
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
init {
|
||||
@@ -44,8 +44,13 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Serializable.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
fun List<Serializable>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E : Serializable> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
inline fun <reified E : Any?> QueryResult.deserialize(): E? {
|
||||
val value = this.rows.firstOrNull()?.getString(0)
|
||||
return if (value == null) {
|
||||
null
|
||||
} else {
|
||||
Serializer().deserialize<E>(value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Serializable> T.toTypeReference(): TypeReference<T> = object : TypeReference<T>() {}
|
||||
inline fun <reified T : Any> T.toTypeReference(): TypeReference<T> = object : TypeReference<T>() {}
|
||||
|
||||
Reference in New Issue
Block a user