Lint
This commit is contained in:
@@ -22,7 +22,7 @@ class Connection(
|
||||
private val password: String,
|
||||
private val host: String = "localhost",
|
||||
private val port: Int = 5432
|
||||
): Executable {
|
||||
) : Executable {
|
||||
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
||||
private val serializer = Serializer()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
@@ -38,7 +38,7 @@ class Connection(
|
||||
|
||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
@@ -62,14 +62,14 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
select(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -80,14 +80,14 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
select(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
@@ -104,14 +104,14 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
select(sql, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
@@ -146,16 +146,16 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> 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(sql, page, limit, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -166,12 +166,12 @@ class Connection(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
select(sql, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun exec(sql: String, values: List<Any?>): QueryResult {
|
||||
return stopwatchQuery(sql, values) {
|
||||
@@ -256,6 +256,5 @@ class Connection(
|
||||
logger?.info("Query Error: $sqlForLog, $values", e)
|
||||
throw e
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,33 +13,33 @@ interface EmbedExecutable {
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/* Select Miltiples */
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/* Select Paginated */
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
|
||||
@@ -7,14 +7,14 @@ import fr.postgresjson.entity.EntityI
|
||||
interface Executable {
|
||||
/* Select One */
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -23,14 +23,14 @@ interface Executable {
|
||||
|
||||
/* Select Miltiples */
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
@@ -39,7 +39,7 @@ interface Executable {
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
fun <R: EntityI> select(
|
||||
fun <R : EntityI> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
|
||||
@@ -5,7 +5,7 @@ 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 {
|
||||
class Function(val definition: Function, override val connection: Connection) : EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return definition.name
|
||||
}
|
||||
@@ -17,7 +17,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
*/
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
@@ -28,22 +28,22 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
select(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
value: R,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, listOf(value), block)
|
||||
select(object : TypeReference<R>() {}, listOf(value), block)
|
||||
|
||||
/**
|
||||
* Select One entity with named parameters
|
||||
*/
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
@@ -54,13 +54,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
select(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
@@ -71,7 +71,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select list of entities with list of parameters
|
||||
*/
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -82,16 +82,16 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select list of entities with named parameters
|
||||
*/
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -102,13 +102,13 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
@@ -119,7 +119,7 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
/**
|
||||
* Select Multiple with pagination
|
||||
*/
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
@@ -137,21 +137,21 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return connection.select(sql, page, limit, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
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)
|
||||
select(page, limit, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values.toMap(), block)
|
||||
select(page, limit, object : TypeReference<List<R>>() {}, values.toMap(), block)
|
||||
|
||||
/* Execute function without traitements */
|
||||
|
||||
@@ -195,11 +195,11 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
val parameters = definition.getParametersIndexedByName()
|
||||
val placeholders = values
|
||||
.filter { entry ->
|
||||
val parameter = parameters[entry.key] ?: parameters["_"+entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
val parameter = parameters[entry.key] ?: parameters["_" + entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
parameter.default === null || entry.value !== null
|
||||
}
|
||||
.map { entry ->
|
||||
val parameter = parameters[entry.key] ?: parameters["_"+entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
val parameter = parameters[entry.key] ?: parameters["_" + entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
""""${parameter.name}" := :${parameter.name}::${parameter.type}"""
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
data class Paginated<T: EntityI>(
|
||||
data class Paginated<T : EntityI>(
|
||||
val result: List<T>,
|
||||
val offset: Int,
|
||||
val limit: Int,
|
||||
|
||||
@@ -4,15 +4,14 @@ 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 {
|
||||
class Query(override val name: String, private val sql: String, override val connection: Connection) : EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return sql
|
||||
}
|
||||
|
||||
/* Select One */
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
@@ -20,13 +19,13 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
select(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, R?) -> Unit
|
||||
@@ -34,15 +33,15 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> selectOne(
|
||||
inline fun <reified R : EntityI> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
select(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/* Select Multiples */
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -50,13 +49,13 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (QueryResult, List<R>) -> Unit
|
||||
@@ -64,13 +63,13 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
inline fun <reified R : EntityI> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
select(object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI> select(
|
||||
override fun <R : EntityI> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
@@ -82,13 +81,13 @@ class Query(override val name: String, private val sql: String, override val con
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
inline fun <reified R: EntityI> select(
|
||||
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)
|
||||
select(page, limit, object : TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
/* Execute function without traitements */
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class Requester(
|
||||
password: String = "dc-project",
|
||||
queriesDirectory: File? = null,
|
||||
functionsDirectory: File? = null
|
||||
): this(
|
||||
) : this(
|
||||
Connection(host = host, port = port, database = database, username = username, password = password),
|
||||
queriesDirectory,
|
||||
functionsDirectory
|
||||
|
||||
@@ -2,10 +2,9 @@ package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
|
||||
|
||||
open class Function(
|
||||
override val script: String
|
||||
): Resource, ParametersInterface {
|
||||
) : Resource, ParametersInterface {
|
||||
val returns: String
|
||||
override val name: String
|
||||
override val parameters: List<Parameter>
|
||||
@@ -24,7 +23,7 @@ open class Function(
|
||||
if (queryMatch !== null) {
|
||||
val functionName = queryMatch.groups.get("name")?.value?.trim() ?: error("Function name not found")
|
||||
val functionParameters = queryMatch.groups["params"]?.value?.trim()
|
||||
this.returns = queryMatch.groups["return"]?.value?.trim() ?:""
|
||||
this.returns = queryMatch.groups["return"]?.value?.trim() ?: ""
|
||||
|
||||
/* Create parameters definition */
|
||||
val parameters = if (functionParameters !== null) {
|
||||
@@ -47,8 +46,8 @@ open class Function(
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ParseException(message: String, cause: Throwable? = null): Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null): ParseException("Function not found in script", cause)
|
||||
abstract class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null) : ParseException("Function not found in script", cause)
|
||||
|
||||
fun getDefinition(): String {
|
||||
return parameters
|
||||
@@ -56,7 +55,6 @@ open class Function(
|
||||
.joinToString(", ") { "${it.name} ${it.type}" }.let {
|
||||
"$name ($it)"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getParametersIndexedByName(): Map<String, Parameter> {
|
||||
|
||||
@@ -18,7 +18,7 @@ class Parameter(val name: String, val type: String, direction: Direction? = Dire
|
||||
}
|
||||
}
|
||||
|
||||
constructor(name: String, type: String, direction: String? = "IN", default: Any? = null): this(
|
||||
constructor(name: String, type: String, direction: String? = "IN", default: Any? = null) : this(
|
||||
name = name,
|
||||
type = type,
|
||||
direction = direction?.let { Direction.valueOf(direction.toUpperCase()) },
|
||||
|
||||
@@ -6,9 +6,9 @@ import java.util.*
|
||||
/* ID */
|
||||
interface EntityI
|
||||
|
||||
abstract class Entity<T>(open var id: T? = null): EntityI
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
||||
abstract class Entity<T>(open var id: T? = null) : EntityI
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null) : Entity<Int>(id)
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
@@ -16,7 +16,7 @@ interface EntityVersioning<ID, NUMBER> {
|
||||
var versionNumber: NUMBER?
|
||||
}
|
||||
|
||||
class UuidEntityVersioning: EntityVersioning<UUID, Int> {
|
||||
class UuidEntityVersioning : EntityVersioning<UUID, Int> {
|
||||
override var versionId: UUID = UUID.randomUUID()
|
||||
override var versionNumber: Int? = null
|
||||
}
|
||||
@@ -38,77 +38,77 @@ interface EntityDeletedAt {
|
||||
}
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp: EntityCreatedAt {
|
||||
class EntityCreatedAtImp : EntityCreatedAt {
|
||||
override var createdAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityUpdatedAtImp: EntityUpdatedAt {
|
||||
class EntityUpdatedAtImp : EntityUpdatedAt {
|
||||
override var updatedAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityDeletedAtImp: EntityDeletedAt {
|
||||
class EntityDeletedAtImp : EntityDeletedAt {
|
||||
override var deletedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T: EntityI> {
|
||||
interface EntityCreatedBy<T : EntityI> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface EntityUpdatedBy<T: EntityI> {
|
||||
interface EntityUpdatedBy<T : EntityI> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T: EntityI> {
|
||||
interface EntityDeletedBy<T : EntityI> {
|
||||
var deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT: EntityI>(
|
||||
class EntityCreatedByImp<UserT : EntityI>(
|
||||
override var createdBy: UserT?
|
||||
): EntityCreatedBy<UserT>
|
||||
) : EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT: EntityI>(
|
||||
class EntityUpdatedByImp<UserT : EntityI>(
|
||||
override var updatedBy: UserT?
|
||||
): EntityUpdatedBy<UserT>
|
||||
) : EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT: EntityI>(
|
||||
class EntityDeletedByImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT?
|
||||
): EntityDeletedBy<UserT>
|
||||
) : EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityDeletedImp<UserT: EntityI>(
|
||||
class EntityDeletedImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT? = null
|
||||
): EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
) : EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT: EntityI>(
|
||||
class EntityUpdatedImp<UserT : EntityI>(
|
||||
override var updatedAt: DateTime? = null,
|
||||
override var updatedBy: UserT? = null
|
||||
): EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
) : EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
|
||||
class EntityCreatedImp<UserT: EntityI>(
|
||||
class EntityCreatedImp<UserT : EntityI>(
|
||||
override var createdAt: DateTime? = null,
|
||||
override var createdBy: UserT? = null
|
||||
): EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
) : EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT: EntityI> {
|
||||
interface Published<UserT : EntityI> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT: EntityI>(
|
||||
class EntityPublishedImp<UserT : EntityI>(
|
||||
override var publishedBy: UserT?
|
||||
): Published<UserT> {
|
||||
) : Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT: EntityI>(
|
||||
abstract class EntityImp<T, UserT : EntityI>(
|
||||
updatedBy: UserT?
|
||||
): Entity<T>(),
|
||||
) : Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(),
|
||||
EntityDeletedAt by EntityDeletedAtImp(),
|
||||
@@ -116,10 +116,10 @@ abstract class EntityImp<T, UserT: EntityI>(
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT: EntityI>(
|
||||
abstract class UuidEntityExtended<T, UserT : EntityI>(
|
||||
updatedBy: UserT?,
|
||||
publishedBy: UserT?
|
||||
):
|
||||
) :
|
||||
EntityImp<T, UserT>(updatedBy),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
||||
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||
|
||||
@@ -13,7 +13,7 @@ data class Function(
|
||||
val down: DefinitionFunction,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
): Migration {
|
||||
) : Migration {
|
||||
val name = up.name
|
||||
override var doExecute: Action? = null
|
||||
|
||||
@@ -28,7 +28,7 @@ data class Function(
|
||||
down: String,
|
||||
connection: Connection,
|
||||
executedAt: Date? = null
|
||||
): this(
|
||||
) : this(
|
||||
DefinitionFunction(up),
|
||||
DefinitionFunction(down),
|
||||
connection,
|
||||
|
||||
@@ -19,7 +19,7 @@ class MigrationEntity(
|
||||
val up: String,
|
||||
val down: String,
|
||||
val version: Int
|
||||
): Entity<String?>(filename)
|
||||
) : Entity<String?>(filename)
|
||||
|
||||
interface Migration {
|
||||
var executedAt: Date?
|
||||
@@ -40,9 +40,9 @@ data class Migrations private constructor(
|
||||
) {
|
||||
private var directories: List<File> = emptyList()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
constructor(directory: File, connection: Connection): this(listOf(directory), connection)
|
||||
constructor(directory: File, connection: Connection) : this(listOf(directory), connection)
|
||||
|
||||
constructor(directories: List<File>, connection: Connection): this(connection) {
|
||||
constructor(directories: List<File>, connection: Connection) : this(connection) {
|
||||
initDB()
|
||||
this.directories = directories
|
||||
reset()
|
||||
@@ -75,14 +75,14 @@ data class Migrations private constructor(
|
||||
*/
|
||||
private fun getMigrationFromDB() {
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
||||
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||
connection.select<MigrationEntity>(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<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
.map { query ->
|
||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
@@ -122,7 +122,7 @@ data class Migrations private constructor(
|
||||
val fileContent = file.readText()
|
||||
try {
|
||||
addFunction(fileContent)
|
||||
} catch(e: FunctionNotFound) {
|
||||
} catch (e: FunctionNotFound) {
|
||||
// Nothing
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ data class Migrations private constructor(
|
||||
|
||||
enum class Direction { UP, DOWN }
|
||||
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException):
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException) :
|
||||
Throwable("The file $path whas not found", cause)
|
||||
|
||||
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
|
||||
|
||||
@@ -11,7 +11,7 @@ data class Query(
|
||||
val down: String,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
): Migration, Entity<String?>(name) {
|
||||
) : Migration, Entity<String?>(name) {
|
||||
override var doExecute: Action? = null
|
||||
|
||||
override fun up(): Migration.Status {
|
||||
|
||||
@@ -19,7 +19,7 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
|
||||
mapper.registerModule(JodaModule())
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
}
|
||||
|
||||
fun serialize(source: EntityI, pretty: Boolean = false): String {
|
||||
@@ -27,11 +27,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
else mapper.writeValueAsString(source)
|
||||
}
|
||||
|
||||
fun <E: EntityI> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
fun <E : EntityI> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
return this.mapper.readValue(json, valueTypeRef)
|
||||
}
|
||||
|
||||
inline fun <reified E: EntityI> deserialize(json: String): E? {
|
||||
inline fun <reified E : EntityI> deserialize(json: String): E? {
|
||||
return this.mapper.readValue(json)
|
||||
}
|
||||
|
||||
@@ -40,14 +40,14 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
}
|
||||
|
||||
inline fun <reified E> deserializeList(json: String): E {
|
||||
return deserializeList(json, object: TypeReference<E>() {})
|
||||
return deserializeList(json, object : TypeReference<E>() {})
|
||||
}
|
||||
|
||||
fun <E: EntityI> deserialize(json: String, target: E): E {
|
||||
fun <E : EntityI> deserialize(json: String, target: E): E {
|
||||
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||
}
|
||||
}
|
||||
|
||||
fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E: EntityI> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E: EntityI> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
inline fun <reified E : EntityI> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E : EntityI> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
@@ -5,6 +5,6 @@ import org.slf4j.LoggerFactory
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal class LoggerDelegate<in R: Any>: ReadOnlyProperty<R, Logger> {
|
||||
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||
}
|
||||
Reference in New Issue
Block a user