implement SelectOne and multiple
add Fixtures
This commit is contained in:
@@ -26,7 +26,7 @@ class Connection(
|
|||||||
return connection
|
return connection
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T, reified R :EntityI<T?>> execute(sql: String, values: List<Any?> = emptyList()): R? {
|
inline fun <T, reified R : EntityI<T?>> selectOne(sql: String, values: List<Any?> = emptyList()): R? {
|
||||||
val future = connect().sendPreparedStatement(sql, values)
|
val future = connect().sendPreparedStatement(sql, values)
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
if (json === null) {
|
if (json === null) {
|
||||||
@@ -37,4 +37,16 @@ class Connection(
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T, reified R : List<EntityI<T?>>> select(sql: String, values: List<Any?> = emptyList()): R {
|
||||||
|
val future = connect().sendPreparedStatement(sql, values)
|
||||||
|
val json = future.get().rows[0].getString(0)
|
||||||
|
if (json === null) {
|
||||||
|
return listOf<EntityI<T?>>() as R
|
||||||
|
} else {
|
||||||
|
val obj = serializer.deserializeList<R>(json)
|
||||||
|
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -33,13 +33,15 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T, reified E : EntityI<T?>> deserialize(json: String): E {
|
inline fun <T, reified E : EntityI<T?>> deserialize(json: String): E {
|
||||||
val unserialized = this.mapper.readValue<E>(json)
|
return this.mapper.readValue<E>(json)
|
||||||
return collection.get(unserialized.id) ?: unserialized
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T, reified E : EntityI<T?>> deserialize(json: String, target: E): E {
|
inline fun <reified E> deserializeList(json: String): E {
|
||||||
val unserialized = mapper.readerForUpdating(target).readValue<E>(json)
|
return mapper.readValue(json)
|
||||||
return collection.get(unserialized.id) ?: unserialized
|
}
|
||||||
|
|
||||||
|
fun <T, E : EntityI<T?>> deserialize(json: String, target: E): E {
|
||||||
|
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,15 +16,32 @@ class ConnectionTest() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getObject() {
|
fun getObject() {
|
||||||
val obj: ObjTest? = connection.execute<Int?, ObjTest>("select to_json(a) from test a limit 1")
|
val obj: ObjTest? = connection.selectOne<Int?, ObjTest>("select to_json(a) from test a limit 1")
|
||||||
assertTrue(obj is ObjTest)
|
assertTrue(obj is ObjTest)
|
||||||
|
assertTrue(obj!!.id == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test
|
@Test
|
||||||
// fun getExistingObject() {
|
fun getExistingObject() {
|
||||||
// val obj: ObjTest? = connection.execute<Int?, ObjTest>("select to_json(a) from test a limit 1")
|
val objs: List<ObjTest2> = connection.select("""
|
||||||
// assertTrue(obj is ObjTest)
|
select
|
||||||
// }
|
json_agg(j)
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
t.id, t.title,
|
||||||
|
t2 as test
|
||||||
|
from test2 t
|
||||||
|
JOIN test t2 ON t.test_id = t2.id
|
||||||
|
) j;
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
assertTrue(objs !== null)
|
||||||
|
assertTrue(objs is List<ObjTest2>)
|
||||||
|
assertTrue(objs!!.size == 2)
|
||||||
|
assertTrue(objs[0].id == 1)
|
||||||
|
assertTrue(objs[0].test!!.id == 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjTest(var name: String): IdEntity()
|
class ObjTest(var name: String): IdEntity()
|
||||||
|
class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package fr.postgresjson.serializer
|
package fr.postgresjson.serializer
|
||||||
|
|
||||||
import fr.postgresjson.entity.UuidEntity
|
import fr.postgresjson.entity.IdEntity
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
@@ -9,17 +9,18 @@ import org.junit.jupiter.api.TestInstance
|
|||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
internal class SerializerTest {
|
internal class SerializerTest {
|
||||||
class ObjTest(var val1: String, var val2: Int) : UuidEntity()
|
class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||||
|
|
||||||
private val serializer = Serializer()
|
private val serializer = Serializer()
|
||||||
|
|
||||||
private val objSerialized: String = """{"val1":"plop","val2":123,"id":"1362a162-df75-4995-ab46-4ad55fa07de2"}"""
|
private val objSerialized: String = """{"val1":"plop","val2":123,"id":2}"""
|
||||||
private val objSerializedUpdate = """{"val1":"update","val2":123}"""
|
private val objSerializedUpdate = """{"val1":"update","val2":123}"""
|
||||||
private lateinit var obj: ObjTest
|
private lateinit var obj: ObjTest
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun before() {
|
fun before() {
|
||||||
obj = ObjTest("plop", 123)
|
obj = ObjTest("plop", 123)
|
||||||
|
obj.id = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -36,7 +37,7 @@ internal class SerializerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun deserialize() {
|
fun deserialize() {
|
||||||
val objDeserialized: ObjTest = serializer.deserialize(objSerialized)
|
val objDeserialized = serializer.deserialize<Int?, ObjTest>(objSerialized)
|
||||||
assertEquals(obj.val1, objDeserialized.val1)
|
assertEquals(obj.val1, objDeserialized.val1)
|
||||||
assertEquals(obj.val2, objDeserialized.val2)
|
assertEquals(obj.val2, objDeserialized.val2)
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/test/resources/fixtures/init.sql
Normal file
20
src/test/resources/fixtures/init.sql
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
create table if not exists test
|
||||||
|
(
|
||||||
|
id serial not null
|
||||||
|
constraint test_pk
|
||||||
|
primary key,
|
||||||
|
name text
|
||||||
|
);
|
||||||
|
|
||||||
|
create table if not exists test2
|
||||||
|
(
|
||||||
|
id serial not null,
|
||||||
|
title text,
|
||||||
|
test_id integer
|
||||||
|
constraint test2_test_id_fk
|
||||||
|
references test
|
||||||
|
);
|
||||||
|
INSERT INTO public.test (id, name) VALUES (1, 'plop');
|
||||||
|
INSERT INTO public.test2 (id, title, test_id) VALUES (1, 'plop', 1);
|
||||||
|
INSERT INTO public.test2 (id, title, test_id) VALUES (2, 'plip', 1);
|
||||||
|
INSERT INTO public.test2 (id, title, test_id) VALUES (3, 'ttt', null);
|
||||||
Reference in New Issue
Block a user