diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 1bd52d6..4113686 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -38,7 +38,12 @@ class Connection( fun inTransaction(f: (Connection) -> CompletableFuture) = connect().inTransaction(f) - override fun > select(sql: String, typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit): R? { + override fun > select( + sql: String, + typeReference: TypeReference, + values: List, + block: (QueryResult, R?) -> Unit + ): R? { val result = exec(sql, compileArgs(values)) val json = result.rows[0].getString(0) return if (json === null) { @@ -50,19 +55,37 @@ class Connection( } } - inline fun > selectOne(sql: String, values: List = emptyList(), noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + sql: String, + values: List = emptyList(), + noinline block: SelectOneCallback = {} + ): R? = select(sql, object: TypeReference() {}, values, block) - override fun > select(sql: String, typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit): R? { + override fun > select( + sql: String, + typeReference: TypeReference, + values: Map, + block: (QueryResult, R?) -> Unit + ): R? { return replaceArgs(sql, values) { select(this.sql, typeReference, this.parameters, block) } } - inline fun > selectOne(sql: String, values: Map, noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + sql: String, + values: Map, + noinline block: SelectOneCallback = {} + ): R? = select(sql, object: TypeReference() {}, values, block) - override fun > select(sql: String, typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit): List { + override fun > select( + sql: String, + typeReference: TypeReference>, + values: List, + block: (QueryResult, List) -> Unit + ): List { val result = exec(sql, compileArgs(values)) val json = result.rows[0].getString(0) return if (json === null) { @@ -74,7 +97,11 @@ class Connection( } } - inline fun > select(sql: String, values: List = emptyList(), noinline block: SelectCallback = {}): List = + inline fun > select( + sql: String, + values: List = emptyList(), + noinline block: SelectCallback = {} + ): List = select(sql, object: TypeReference>() {}, values, block) override fun > select( @@ -132,7 +159,11 @@ class Connection( } } - inline fun > select(sql: String, values: Map, noinline block: SelectCallback = {}): List = + inline fun > select( + sql: String, + values: Map, + noinline block: SelectCallback = {} + ): List = select(sql, object: TypeReference>() {}, values, block) override fun exec(sql: String, values: List): QueryResult { @@ -184,7 +215,7 @@ class Connection( data class ParametersQuery(val sql: String, val parameters: List) private fun stopwatchQuery(sql: String, values: List = emptyList(), callback: () -> T): T { - val sqlForLog = "\n"+sql.prependIndent() + val sqlForLog = "\n${sql.prependIndent()}" try { val start = System.currentTimeMillis() val result = callback() diff --git a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt index 773c72e..7818fdf 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/EmbedExecutable.kt @@ -8,13 +8,43 @@ interface EmbedExecutable { val connection: Connection override fun toString(): String - fun > select(typeReference: TypeReference, values: List = emptyList(), block: SelectOneCallback = {}): R? - fun > select(typeReference: TypeReference, values: Map, block: SelectOneCallback = {}): R? + /* Select One */ + /** + * Select One entity with list of parameters + */ + fun > select( + typeReference: TypeReference, + values: List = emptyList(), + block: SelectOneCallback = {} + ): R? - fun > select(typeReference: TypeReference>, values: List = emptyList(), block: SelectCallback = {}): List - fun > select(typeReference: TypeReference>, values: Map, block: SelectCallback = {}): List + fun > select( + typeReference: TypeReference, + values: Map, + block: SelectOneCallback = {} + ): R? - fun > select(page: Int, limit: Int, typeReference: TypeReference>, values: Map, block: SelectPaginatedCallback = {}): Paginated + /* Select Miltiples */ + fun > select( + typeReference: TypeReference>, + values: List = emptyList(), + block: SelectCallback = {} + ): List + + fun > select( + typeReference: TypeReference>, + values: Map, + block: SelectCallback = {} + ): List + + /* Select Paginated */ + fun > select( + page: Int, + limit: Int, + typeReference: TypeReference>, + values: Map, + block: SelectPaginatedCallback = {} + ): Paginated fun exec(values: List = emptyList()): QueryResult fun exec(values: Map): QueryResult diff --git a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt index 7cb599b..08daedd 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Executable.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Executable.kt @@ -5,11 +5,49 @@ import com.github.jasync.sql.db.QueryResult import fr.postgresjson.entity.EntityI interface Executable { - fun > select(sql: String, typeReference: TypeReference, values: List = emptyList(), block: SelectOneCallback = {}): R? - fun > select(sql: String, typeReference: TypeReference, values: Map, block: SelectOneCallback = {}): R? - fun > select(sql: String, typeReference: TypeReference>, values: List = emptyList(), block: SelectCallback = {}): List - fun > select(sql: String, typeReference: TypeReference>, values: Map, block: SelectCallback = {}): List - fun > select(sql: String, page: Int, limit: Int, typeReference: TypeReference>, values: Map, block: SelectPaginatedCallback = {}): Paginated + /* Select One */ + + fun > select( + sql: String, + typeReference: TypeReference, + values: List = emptyList(), + block: SelectOneCallback = {} + ): R? + + fun > select( + sql: String, + typeReference: TypeReference, + values: Map, + block: SelectOneCallback = {} + ): R? + + /* Select Miltiples */ + + fun > select( + sql: String, + typeReference: TypeReference>, + values: List = emptyList(), + block: SelectCallback = {} + ): List + + fun > select( + sql: String, + typeReference: TypeReference>, + values: Map, + block: SelectCallback = {} + ): List + + /* Select Paginated */ + + fun > select( + sql: String, + page: Int, + limit: Int, + typeReference: TypeReference>, + values: Map, + block: SelectPaginatedCallback = {} + ): Paginated + fun exec(sql: String, values: List = emptyList()): QueryResult fun exec(sql: String, values: Map): QueryResult fun sendQuery(sql: String): QueryResult diff --git a/src/main/kotlin/fr/postgresjson/connexion/Function.kt b/src/main/kotlin/fr/postgresjson/connexion/Function.kt index ac2c095..69877e9 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Function.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Function.kt @@ -10,59 +10,102 @@ class Function(val definition: Function, override val connection: Connection): E return definition.name } + /* Select One */ + /** * Select One entity with list of parameters */ - override fun > select(typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit): R? { + override fun > select( + typeReference: TypeReference, + values: List, + block: (QueryResult, R?) -> Unit + ): R? { val args = compileArgs(values) val sql = "SELECT * FROM ${definition.name} ($args)" return connection.select(sql, typeReference, values, block) } - inline fun > selectOne(values: List = emptyList(), noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + values: List = emptyList(), + noinline block: SelectOneCallback = {} + ): R? = select(object: TypeReference() {}, values, block) /** * Select One entity with named parameters */ - override fun > select(typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit): R? { + override fun > select( + typeReference: TypeReference, + values: Map, + block: (QueryResult, R?) -> Unit + ): R? { val args = compileArgs(values) val sql = "SELECT * FROM ${definition.name} ($args)" return connection.select(sql, typeReference, values, block) } - inline fun > selectOne(values: Map, noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + values: Map, + noinline block: SelectOneCallback = {} + ): R? = select(object: TypeReference() {}, values, block) + /* Select Multiples */ + /** * Select list of entities with list of parameters */ - override fun > select(typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit): List { + override fun > select( + typeReference: TypeReference>, + values: List, + block: (QueryResult, List) -> Unit + ): List { val args = compileArgs(values) val sql = "SELECT * FROM ${definition.name} ($args)" return connection.select(sql, typeReference, values, block) } - inline fun > select(values: List = emptyList(), noinline block: SelectCallback = {}): List = + inline fun > select( + values: List = emptyList(), + noinline block: SelectCallback = {} + ): List = select(object: TypeReference>() {}, values, block) /** * Select list of entities with named parameters */ - override fun > select(typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit): List { + override fun > select( + typeReference: TypeReference>, + values: Map, + block: (QueryResult, List) -> Unit + ): List { val args = compileArgs(values) val sql = "SELECT * FROM ${definition.name} ($args)" return connection.select(sql, typeReference, values, block) } - inline fun > select(values: Map, noinline block: SelectCallback = {}): List = + inline fun > select( + values: Map, + noinline block: SelectCallback = {} + ): List = select(object: TypeReference>() {}, values, block) - override fun > select(page: Int, limit: Int, typeReference: TypeReference>, values: Map, block: (QueryResult, Paginated) -> Unit): Paginated { + /* Select Paginated */ + + /** + * Select Multiple with pagination + */ + override fun > select( + page: Int, + limit: Int, + typeReference: TypeReference>, + values: Map, + block: (QueryResult, Paginated) -> Unit + ): Paginated { val offset = (page - 1) * limit val newValues = values .plus("offset" to offset) @@ -73,9 +116,17 @@ class Function(val definition: Function, override val connection: Connection): E return connection.select(sql, page, limit, typeReference, values, block) } - inline fun > select(page: Int, limit: Int, values: Map = emptyMap(), noinline block: SelectPaginatedCallback = {}): Paginated = + + inline fun > select( + page: Int, + limit: Int, + values: Map = emptyMap(), + noinline block: SelectPaginatedCallback = {} + ): Paginated = select(page, limit, object: TypeReference>() {}, values, block) + /* Execute function without traitements */ + override fun exec(values: List): QueryResult { val args = compileArgs(values) val sql = "SELECT * FROM ${definition.name} ($args)" diff --git a/src/main/kotlin/fr/postgresjson/connexion/Query.kt b/src/main/kotlin/fr/postgresjson/connexion/Query.kt index 5a35ad4..692c802 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Query.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Query.kt @@ -10,40 +10,88 @@ class Query(private val sql: String, override val connection: Connection): Embed return sql } - override fun > select(typeReference: TypeReference, values: List, block: (QueryResult, R?) -> Unit): R? { + /* Select One */ + + override fun > select( + typeReference: TypeReference, + values: List, + block: (QueryResult, R?) -> Unit + ): R? { return connection.select(this.toString(), typeReference, values, block) } - inline fun > selectOne(values: List = emptyList(), noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + values: List = emptyList(), + noinline block: SelectOneCallback = {} + ): R? = select(object: TypeReference() {}, values, block) - override fun > select(typeReference: TypeReference, values: Map, block: (QueryResult, R?) -> Unit): R? { + override fun > select( + typeReference: TypeReference, + values: Map, + block: (QueryResult, R?) -> Unit + ): R? { return connection.select(this.toString(), typeReference, values, block) } - inline fun > selectOne(values: Map, noinline block: SelectOneCallback = {}): R? = + inline fun > selectOne( + values: Map, + noinline block: SelectOneCallback = {} + ): R? = select(object: TypeReference() {}, values, block) - override fun > select(typeReference: TypeReference>, values: List, block: (QueryResult, List) -> Unit): List { + /* Select Multiples */ + + override fun > select( + typeReference: TypeReference>, + values: List, + block: (QueryResult, List) -> Unit + ): List { return connection.select(this.toString(), typeReference, values, block) } - inline fun > select(values: List = emptyList(), noinline block: SelectCallback = {}): List = + inline fun > select( + values: List = emptyList(), + noinline block: SelectCallback = {} + ): List = select(object: TypeReference>() {}, values, block) - override fun > select(typeReference: TypeReference>, values: Map, block: (QueryResult, List) -> Unit): List { + override fun > select( + typeReference: TypeReference>, + values: Map, + block: (QueryResult, List) -> Unit + ): List { return connection.select(this.toString(), typeReference, values, block) } - inline fun > select(values: Map, noinline block: SelectCallback = {}): List = + inline fun > select( + values: Map, + noinline block: SelectCallback = {} + ): List = select(object: TypeReference>() {}, values, block) - override fun > select(page: Int, limit: Int, typeReference: TypeReference>, values: Map, block: (QueryResult, Paginated) -> Unit): Paginated { + override fun > select( + page: Int, + limit: Int, + typeReference: TypeReference>, + values: Map, + block: (QueryResult, Paginated) -> Unit + ): Paginated { return connection.select(this.toString(), page, limit, typeReference, values, block) } - inline fun > select(page: Int, limit: Int, values: Map = emptyMap(), noinline block: SelectPaginatedCallback = {}): Paginated = + + /* Select Paginated */ + + inline fun > select( + page: Int, + limit: Int, + values: Map = emptyMap(), + noinline block: SelectPaginatedCallback = {} + ): Paginated = select(page, limit, object: TypeReference>() {}, values, block) + /* Execute function without traitements */ + override fun exec(values: List): QueryResult { return connection.exec(sql, values) } diff --git a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt index 8ce672f..2d34c1c 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt @@ -80,7 +80,8 @@ class Requester( private val functionsDirectory: File? = null ) { fun createRequester(): Requester { - val con = Connection(host = host, port = port, database = database, username = username, password = password) + val con = + Connection(host = host, port = port, database = database, username = username, password = password) val req = Requester(con) return initRequester(req) diff --git a/src/main/kotlin/fr/postgresjson/definition/Function.kt b/src/main/kotlin/fr/postgresjson/definition/Function.kt index 48ec3f4..c144066 100644 --- a/src/main/kotlin/fr/postgresjson/definition/Function.kt +++ b/src/main/kotlin/fr/postgresjson/definition/Function.kt @@ -3,20 +3,22 @@ package fr.postgresjson.definition import java.io.File -open class Function ( +open class Function( override val script: String -) : Resource, ParametersInterface { +): Resource, ParametersInterface { val returns: String? override val name: String override val parameters: List override var source: File? = null init { - val functionRegex = """create (or replace )?(procedure|function) *(?[^(\s]+)\s*\((?(\s*((IN|OUT|INOUT|VARIADIC)?\s+)?([^\s,)]+\s+)?([^\s,)]+)(\s+(?:default\s|=)\s*[^\s,)]+)?\s*(,|(?=\))))*)\) *(?RETURNS *[^ ]+)?""" - .toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)) + val functionRegex = + """create (or replace )?(procedure|function) *(?[^(\s]+)\s*\((?(\s*((IN|OUT|INOUT|VARIADIC)?\s+)?([^\s,)]+\s+)?([^\s,)]+)(\s+(?:default\s|=)\s*[^\s,)]+)?\s*(,|(?=\))))*)\) *(?RETURNS *[^ ]+)?""" + .toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)) - val paramsRegex = """\s*(?((?IN|OUT|INOUT|VARIADIC)?\s+)?("?(?[^\s,")]+)"?\s+)?(?[^\s,)]+)(\s+(?default\s|=)\s*[^\s,)]+)?)\s*(,|$)""" - .toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)) + val paramsRegex = + """\s*(?((?IN|OUT|INOUT|VARIADIC)?\s+)?("?(?[^\s,")]+)"?\s+)?(?[^\s,)]+)(\s+(?default\s|=)\s*[^\s,)]+)?)\s*(,|$)""" + .toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE)) val queryMatch = functionRegex.find(script) if (queryMatch !== null) { @@ -32,7 +34,8 @@ open class Function ( paramsMatch.groups["name"]!!.value.trim(), paramsMatch.groups["type"]!!.value.trim(), paramsMatch.groups["direction"]?.value?.trim(), - paramsMatch.groups["default"]?.value?.trim()) + paramsMatch.groups["default"]?.value?.trim() + ) }.toList() } else { listOf() @@ -43,10 +46,11 @@ open class Function ( throw FunctionNotFound() } } + 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 { + fun getDefinition(): String { return "$name (" + parameters.joinToString(", ") + ") $returns" } @@ -56,21 +60,28 @@ open class Function ( }.toMap() } - infix fun `has same definition` (other: Function): Boolean { + infix fun `has same definition`(other: Function): Boolean { return other.getDefinition() == this.getDefinition() } - infix fun `is same` (other: Function): Boolean { + infix fun `is same`(other: Function): Boolean { return other.script == this.script } companion object { fun build(source: File): List { return source.readText() - .split("CREATE +(OR REPLACE +)?(PROCEDURE|FUNCTION)".toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))) + .split( + "CREATE +(OR REPLACE +)?(PROCEDURE|FUNCTION)".toRegex( + setOf( + RegexOption.IGNORE_CASE, + RegexOption.MULTILINE + ) + ) + ) .map { - Function("CREATE OR REPLACE FUNCTION $it") - } + Function("CREATE OR REPLACE FUNCTION $it") + } } } } \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/definition/Parameter.kt b/src/main/kotlin/fr/postgresjson/definition/Parameter.kt index 4deb502..4a7d97c 100644 --- a/src/main/kotlin/fr/postgresjson/definition/Parameter.kt +++ b/src/main/kotlin/fr/postgresjson/definition/Parameter.kt @@ -7,8 +7,7 @@ interface ParameterI { val default: String } -class Parameter(val name: String, val type: String, direction: Direction? = Direction.IN, val default: Any? = null) -{ +class Parameter(val name: String, val type: String, direction: Direction? = Direction.IN, val default: Any? = null) { val direction: Direction init { @@ -19,10 +18,10 @@ 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())}, + direction = direction?.let { Direction.valueOf(direction.toUpperCase()) }, default = default ) diff --git a/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt b/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt index 7bd3918..ad99307 100644 --- a/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt +++ b/src/main/kotlin/fr/postgresjson/entity/EntitiesCollections.kt @@ -5,17 +5,17 @@ import kotlin.reflect.KClass class EntitiesCollections { private val collections: MutableMap, EntityCollection>> = mutableMapOf() - fun > get(id: I, className: KClass): R? { + fun > get(id: I, className: KClass): R? { val collection = collections[className] val entity = collection?.get(id!!) return entity as R? } - inline fun > get(id: I): R? { + inline fun > get(id: I): R? { return get(id, R::class) } - fun > set(entity: R): EntitiesCollections { + fun > set(entity: R): EntitiesCollections { if (collections[entity.className] == null) { collections[entity.className] = EntityCollection() } @@ -25,7 +25,7 @@ class EntitiesCollections { return this } - class EntityCollection> { + class EntityCollection> { private var collection: MutableMap = mutableMapOf() fun get(id: T): E? { diff --git a/src/main/kotlin/fr/postgresjson/entity/Entity.kt b/src/main/kotlin/fr/postgresjson/entity/Entity.kt index dbd0796..f770ece 100644 --- a/src/main/kotlin/fr/postgresjson/entity/Entity.kt +++ b/src/main/kotlin/fr/postgresjson/entity/Entity.kt @@ -12,22 +12,22 @@ interface EntityI { @JsonIgnore() get() = this::class as KClass> } -abstract class Entity(override var id: T? = null) : EntityI -abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity(id) -abstract class IdEntity(override var id: Int? = null) : Entity(id) +abstract class Entity(override var id: T? = null): EntityI +abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity(id) +abstract class IdEntity(override var id: Int? = null): Entity(id) /* Version */ interface EntityVersioning { var version: T } -interface EntityVersioningIncrement : EntityVersioning -class EntityVersioningIncrementImp() : EntityVersioningIncrement { +interface EntityVersioningIncrement: EntityVersioning +class EntityVersioningIncrementImp: EntityVersioningIncrement { override var version: Int? = null } -interface EntityVersioningDate : EntityVersioning -class EntityVersioningDateImp() : EntityVersioningDate { +interface EntityVersioningDate: EntityVersioning +class EntityVersioningDateImp: EntityVersioningDate { override var version: DateTime? = null } @@ -40,15 +40,15 @@ interface EntityUpdatedAt { var updatedAt: DateTime? } -class EntityCreatedAtImp : EntityCreatedAt { +class EntityCreatedAtImp: EntityCreatedAt { override var createdAt: DateTime? = null } -class EntityUpdatedAtImp : EntityUpdatedAt { +class EntityUpdatedAtImp: EntityUpdatedAt { override var updatedAt: DateTime? = null } -interface User : EntityI { +interface User: EntityI { fun isValid(): Boolean } @@ -61,11 +61,11 @@ interface UpdatedBy { var updatedBy: User? } -class EntityCreatedByImp : CreatedBy { +class EntityCreatedByImp: CreatedBy { override var createdBy: User? = null } -class EntityUpdatedByImp : UpdatedBy { +class EntityUpdatedByImp: UpdatedBy { override var updatedBy: User? = null } @@ -75,19 +75,19 @@ interface Published { var publishedBy: User? } -class EntityPublishedImp : Published { +class EntityPublishedImp: Published { override var publishedAt: DateTime? = null override var publishedBy: User? = null } /* Implementation */ -abstract class EntityImp : Entity(), +abstract class EntityImp: Entity(), EntityCreatedAt by EntityCreatedAtImp(), EntityUpdatedAt by EntityUpdatedAtImp(), CreatedBy by EntityCreatedByImp(), UpdatedBy by EntityUpdatedByImp() -abstract class EntityExtended : +abstract class EntityExtended: EntityImp(), EntityVersioningIncrement by EntityVersioningIncrementImp(), Published by EntityPublishedImp() diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index bfca5da..a5689c1 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -59,7 +59,7 @@ data class Function( connection.inTransaction { up() down() - it.sendQuery("ROLLBACK"); + it.sendQuery("ROLLBACK") }.join() return Status.OK // TODO diff --git a/src/main/kotlin/fr/postgresjson/repository/Repository.kt b/src/main/kotlin/fr/postgresjson/repository/Repository.kt index b2dcaee..8a6d1ca 100644 --- a/src/main/kotlin/fr/postgresjson/repository/Repository.kt +++ b/src/main/kotlin/fr/postgresjson/repository/Repository.kt @@ -4,13 +4,15 @@ import fr.postgresjson.connexion.Requester import fr.postgresjson.entity.EntitiesCollections import fr.postgresjson.entity.EntityI import fr.postgresjson.serializer.Serializer +import jdk.jfr.Experimental import kotlin.reflect.KClass -interface RepositoryI> { +interface RepositoryI> { val entityName: KClass } -abstract class Repository>(override val entityName: KClass) : RepositoryI { +@Experimental +abstract class Repository>(override val entityName: KClass): RepositoryI { abstract var requester: Requester abstract fun getClassName(): String diff --git a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt index 75ef5a9..a1d4aa7 100644 --- a/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt +++ b/src/main/kotlin/fr/postgresjson/serializer/Serializer.kt @@ -18,7 +18,6 @@ import fr.postgresjson.entity.UuidEntity import java.io.IOException import java.util.* - class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { var collection: EntitiesCollections = EntitiesCollections() @@ -35,11 +34,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { return mapper.writeValueAsString(source) } - fun > deserialize(json: String, valueTypeRef: TypeReference): E { + fun > deserialize(json: String, valueTypeRef: TypeReference): E { return this.mapper.readValue(json, valueTypeRef) } - inline fun ?> deserialize(json: String): E { + inline fun ?> deserialize(json: String): E { return this.mapper.readValue(json) } @@ -51,19 +50,19 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) { return deserializeList(json, object: TypeReference() {}) } - fun > deserialize(json: String, target: E): E { + fun > deserialize(json: String, target: E): E { return mapper.readerForUpdating(target).readValue(json) } } fun EntityI.serialize() = Serializer().serialize(this) -inline fun > E.deserialize(json: String) = Serializer().deserialize(json, this) +inline fun > E.deserialize(json: String) = Serializer().deserialize(json, this) -class EntityUuidDeserializer @JvmOverloads constructor(vc: Class<*>? = null) : StdDeserializer(vc) { - var collection: EntitiesCollections = EntitiesCollections() +class EntityUuidDeserializer @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer(vc) { + var collection: EntitiesCollections = EntitiesCollections() - constructor(collection: EntitiesCollections) : this() { + constructor(collection: EntitiesCollections): this() { this.collection = collection } @@ -78,10 +77,10 @@ class EntityUuidDeserializer @JvmOverloads constructor(vc: Class } -class EntityIdDeserializer @JvmOverloads constructor(vc: Class<*>? = null) : StdDeserializer(vc) { - var collection: EntitiesCollections = EntitiesCollections() +class EntityIdDeserializer @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer(vc) { + var collection: EntitiesCollections = EntitiesCollections() - constructor(collection: EntitiesCollections) : this() { + constructor(collection: EntitiesCollections): this() { this.collection = collection } diff --git a/src/main/kotlin/fr/postgresjson/stopwatch/Stopwatch.kt b/src/main/kotlin/fr/postgresjson/stopwatch/Stopwatch.kt deleted file mode 100644 index 51244f2..0000000 --- a/src/main/kotlin/fr/postgresjson/stopwatch/Stopwatch.kt +++ /dev/null @@ -1,8 +0,0 @@ -package fr.postgresjson.stopwatch - -fun elapse(callback: (start: Long) -> T, after: (elapse: Long) -> Unit): T { - val start = System.currentTimeMillis() - val result = callback(start) - after(System.currentTimeMillis() - start) - return result -} diff --git a/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt b/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt index a17f510..87083b7 100644 --- a/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt +++ b/src/main/kotlin/fr/postgresjson/utils/LoggerDelegate.kt @@ -5,7 +5,6 @@ import org.slf4j.LoggerFactory import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty -internal class LoggerDelegate : ReadOnlyProperty { - override fun getValue(thisRef: R, property: KProperty<*>) - = LoggerFactory.getLogger(thisRef.javaClass.packageName) +internal class LoggerDelegate: ReadOnlyProperty { + override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName) } \ No newline at end of file