refactoring: EntitiesCollections & add EntityI.className
This commit is contained in:
@@ -59,7 +59,9 @@ class Connection(
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is EntityI<*>) {
|
||||
serializer.serialize(it)
|
||||
val json = serializer.serialize(it)
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
||||
json
|
||||
} else {
|
||||
it
|
||||
}
|
||||
|
||||
@@ -2,21 +2,27 @@ package fr.postgresjson.entity
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class EntityCollection {
|
||||
val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
class EntitiesCollections {
|
||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
|
||||
inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
|
||||
val collection = collections[R::class]
|
||||
fun <I, R : EntityI<I?>> get(id: I, className: KClass<R>): R? {
|
||||
val collection = collections[className]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
inline fun <I, reified R : EntityI<I?>> set(entity: R) {
|
||||
if (collections[R::class] == null) {
|
||||
collections[R::class] = EntityCollection()
|
||||
inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
|
||||
return get(id, R::class)
|
||||
}
|
||||
|
||||
collections[R::class]!!.set(entity as EntityI<Any?>)
|
||||
fun <I, R : EntityI<out I?>> set(entity: R): EntitiesCollections {
|
||||
if (collections[entity.className] == null) {
|
||||
collections[entity.className] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[entity.className]!!.set(entity as EntityI<Any?>)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
class EntityCollection<T, E : EntityI<T?>> {
|
||||
@@ -1,11 +1,15 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/* ID */
|
||||
interface EntityI<T> {
|
||||
var id: T?
|
||||
val className: KClass<EntityI<T?>>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI<T?>>
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null) : EntityI<T?>
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.postgresjson.repository
|
||||
|
||||
import fr.postgresjson.Serializer
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntityCollection
|
||||
import fr.postgresjson.entity.EntitiesCollections
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@@ -17,7 +17,7 @@ abstract class Repository<T, E : EntityI<T?>>(override val entityName: KClass<E>
|
||||
|
||||
fun <T> findById(id: T): EntityI<T?>? {
|
||||
val sql = requester.getQuery(entityName.toString())
|
||||
return when (val e = EntityCollection().get(id)) {
|
||||
return when (val e = EntitiesCollections().get(id)) {
|
||||
null -> {
|
||||
// TODO create Request
|
||||
Serializer().deserialize<T, EntityI<T?>>("""{"plop", "plip"}""")
|
||||
|
||||
@@ -58,10 +58,11 @@ class ConnectionTest(): TestAbstract() {
|
||||
@Test
|
||||
fun callRequestWithArgsEntity() {
|
||||
val o = ObjTest("myName")
|
||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
|
||||
o.id = 88
|
||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id int, name text);", listOf(o))
|
||||
assertTrue(obj !== null)
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj!!.id == 1)
|
||||
assertTrue(obj!!.id == 88)
|
||||
assertTrue(obj.name == "myName")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user