refactoring: EntitiesCollections & add EntityI.className

This commit is contained in:
2019-06-24 15:47:18 +02:00
parent b3d9e7624b
commit 96b335875d
5 changed files with 26 additions and 13 deletions

View File

@@ -59,7 +59,9 @@ class Connection(
private fun compileArgs(values: List<Any?>): List<Any?> { private fun compileArgs(values: List<Any?>): List<Any?> {
return values.map { return values.map {
if (it is EntityI<*>) { if (it is EntityI<*>) {
serializer.serialize(it) val json = serializer.serialize(it)
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
json
} else { } else {
it it
} }

View File

@@ -2,21 +2,27 @@ package fr.postgresjson.entity
import kotlin.reflect.KClass import kotlin.reflect.KClass
class EntityCollection { class EntitiesCollections {
val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf() private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
inline fun <I, reified R : EntityI<I?>> get(id: I): R? { fun <I, R : EntityI<I?>> get(id: I, className: KClass<R>): R? {
val collection = collections[R::class] val collection = collections[className]
val entity = collection?.get(id!!) val entity = collection?.get(id!!)
return entity as R? return entity as R?
} }
inline fun <I, reified R : EntityI<I?>> set(entity: R) { inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
if (collections[R::class] == null) { return get(id, R::class)
collections[R::class] = EntityCollection() }
fun <I, R : EntityI<out I?>> set(entity: R): EntitiesCollections {
if (collections[entity.className] == null) {
collections[entity.className] = EntityCollection()
} }
collections[R::class]!!.set(entity as EntityI<Any?>) collections[entity.className]!!.set(entity as EntityI<Any?>)
return this
} }
class EntityCollection<T, E : EntityI<T?>> { class EntityCollection<T, E : EntityI<T?>> {

View File

@@ -1,11 +1,15 @@
package fr.postgresjson.entity package fr.postgresjson.entity
import com.fasterxml.jackson.annotation.JsonIgnore
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.* import java.util.*
import kotlin.reflect.KClass
/* ID */ /* ID */
interface EntityI<T> { interface EntityI<T> {
var id: 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?> abstract class Entity<T>(override var id: T? = null) : EntityI<T?>

View File

@@ -2,7 +2,7 @@ package fr.postgresjson.repository
import fr.postgresjson.Serializer import fr.postgresjson.Serializer
import fr.postgresjson.connexion.Requester import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.EntityCollection import fr.postgresjson.entity.EntitiesCollections
import fr.postgresjson.entity.EntityI import fr.postgresjson.entity.EntityI
import kotlin.reflect.KClass 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?>? { fun <T> findById(id: T): EntityI<T?>? {
val sql = requester.getQuery(entityName.toString()) val sql = requester.getQuery(entityName.toString())
return when (val e = EntityCollection().get(id)) { return when (val e = EntitiesCollections().get(id)) {
null -> { null -> {
// TODO create Request // TODO create Request
Serializer().deserialize<T, EntityI<T?>>("""{"plop", "plip"}""") Serializer().deserialize<T, EntityI<T?>>("""{"plop", "plip"}""")

View File

@@ -58,10 +58,11 @@ class ConnectionTest(): TestAbstract() {
@Test @Test
fun callRequestWithArgsEntity() { fun callRequestWithArgsEntity() {
val o = ObjTest("myName") 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 !== null)
assertTrue(obj is ObjTest) assertTrue(obj is ObjTest)
assertTrue(obj!!.id == 1) assertTrue(obj!!.id == 88)
assertTrue(obj.name == "myName") assertTrue(obj.name == "myName")
} }