Add Serializable Parameter
This commit is contained in:
7
.idea/runConfigurations/Check.xml
generated
Normal file
7
.idea/runConfigurations/Check.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Check" type="CompoundRunConfigurationType">
|
||||||
|
<toRun name="tests" type="JUnit" />
|
||||||
|
<toRun name="Lint" type="GradleRunConfiguration" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
31
.idea/runConfigurations/Lint.xml
generated
Normal file
31
.idea/runConfigurations/Lint.xml
generated
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Lint" type="GradleRunConfiguration" factoryName="Gradle" singleton="false">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="ktlintCheck" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<extension name="net.ashald.envfile">
|
||||||
|
<option name="IS_ENABLED" value="false" />
|
||||||
|
<option name="IS_SUBST" value="false" />
|
||||||
|
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||||
|
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||||
|
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||||
|
<ENTRIES>
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||||
|
</ENTRIES>
|
||||||
|
</extension>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -7,6 +7,7 @@ import com.github.jasync.sql.db.pool.ConnectionPool
|
|||||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
||||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
|
import fr.postgresjson.entity.Serializable
|
||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
import fr.postgresjson.utils.LoggerDelegate
|
import fr.postgresjson.utils.LoggerDelegate
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
@@ -174,8 +175,9 @@ class Connection(
|
|||||||
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 {
|
override fun exec(sql: String, values: List<Any?>): QueryResult {
|
||||||
return stopwatchQuery(sql, values) {
|
val compiledValues = compileArgs(values)
|
||||||
connect().sendPreparedStatement(sql, compileArgs(values)).join()
|
return stopwatchQuery(sql, compiledValues) {
|
||||||
|
connect().sendPreparedStatement(sql, compiledValues).join()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,8 +188,9 @@ class Connection(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun sendQuery(sql: String, values: List<Any?>): Int {
|
override fun sendQuery(sql: String, values: List<Any?>): Int {
|
||||||
return stopwatchQuery(sql, values) {
|
val compiledValues = compileArgs(values)
|
||||||
replaceArgsIntoSql(sql, compileArgs(values)) {
|
return stopwatchQuery(sql, compiledValues) {
|
||||||
|
replaceArgsIntoSql(sql, compiledValues) {
|
||||||
connect().sendQuery(it).join().rowsAffected.toInt()
|
connect().sendQuery(it).join().rowsAffected.toInt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +204,7 @@ 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 Serializable) {
|
||||||
serializer.serialize(it)
|
serializer.serialize(it)
|
||||||
} else {
|
} else {
|
||||||
it
|
it
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ package fr.postgresjson.entity
|
|||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/* ID */
|
interface Serializable
|
||||||
interface EntityI
|
interface EntityI : Serializable
|
||||||
|
interface Parameter : Serializable
|
||||||
|
|
||||||
abstract class Entity<T>(open var id: T? = null) : 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 UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID>(id)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule
|
|||||||
import com.fasterxml.jackson.datatype.joda.JodaModule
|
import com.fasterxml.jackson.datatype.joda.JodaModule
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.fasterxml.jackson.module.kotlin.readValue
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.Serializable
|
||||||
|
|
||||||
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||||
init {
|
init {
|
||||||
@@ -22,16 +22,16 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun serialize(source: EntityI, pretty: Boolean = false): String {
|
fun serialize(source: Any, pretty: Boolean = false): String {
|
||||||
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
||||||
else mapper.writeValueAsString(source)
|
else mapper.writeValueAsString(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <E : EntityI> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
fun <E> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||||
return this.mapper.readValue(json, valueTypeRef)
|
return this.mapper.readValue(json, valueTypeRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified E : EntityI> deserialize(json: String): E? {
|
inline fun <reified E> deserialize(json: String): E? {
|
||||||
return this.mapper.readValue(json)
|
return this.mapper.readValue(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,11 +43,11 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
return deserializeList(json, object : TypeReference<E>() {})
|
return deserializeList(json, object : TypeReference<E>() {})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <E : EntityI> deserialize(json: String, target: E): E {
|
fun <E> deserialize(json: String, target: E): E {
|
||||||
return mapper.readerForUpdating(target).readValue<E>(json)
|
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun EntityI.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
fun Serializable.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 : Serializable> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||||
inline fun <reified E : EntityI> String.deserialize() = Serializer().deserialize<E>(this)
|
inline fun <reified E : Serializable> String.deserialize() = Serializer().deserialize<E>(this)
|
||||||
@@ -2,6 +2,7 @@ package fr.postgresjson
|
|||||||
|
|
||||||
import fr.postgresjson.connexion.Paginated
|
import fr.postgresjson.connexion.Paginated
|
||||||
import fr.postgresjson.entity.IdEntity
|
import fr.postgresjson.entity.IdEntity
|
||||||
|
import fr.postgresjson.entity.Parameter
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.*
|
||||||
import org.junit.jupiter.api.Assertions
|
import org.junit.jupiter.api.Assertions
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
@@ -12,6 +13,8 @@ class ConnectionTest() : TestAbstract() {
|
|||||||
private class ObjTest(var name: String) : IdEntity()
|
private class ObjTest(var name: String) : IdEntity()
|
||||||
private class ObjTest2(var title: String, var test: ObjTest?) : IdEntity()
|
private class ObjTest2(var title: String, var test: ObjTest?) : IdEntity()
|
||||||
private class ObjTest3(var first: String, var seconde: String, var third: Int) : IdEntity()
|
private class ObjTest3(var first: String, var seconde: String, var third: Int) : IdEntity()
|
||||||
|
private class ObjTestWithParameterObject(var first: ParameterObject, var seconde: ParameterObject) : IdEntity()
|
||||||
|
private class ParameterObject(var third: String) : Parameter
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getObject() {
|
fun getObject() {
|
||||||
@@ -81,6 +84,19 @@ class ConnectionTest() : TestAbstract() {
|
|||||||
assertEquals(result.third, 123)
|
assertEquals(result.third, 123)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `select one with named parameters object`() {
|
||||||
|
val result: ObjTestWithParameterObject? = connection.selectOne(
|
||||||
|
"SELECT json_build_object('first', :first::json, 'seconde', :seconde::json)",
|
||||||
|
mapOf(
|
||||||
|
"first" to ParameterObject("one"),
|
||||||
|
"seconde" to ParameterObject("two")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals("one", result!!.first.third)
|
||||||
|
assertEquals("two", result.seconde.third)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `select with named parameters`() {
|
fun `select with named parameters`() {
|
||||||
val params: Map<String, Any?> = mapOf(
|
val params: Map<String, Any?> = mapOf(
|
||||||
|
|||||||
@@ -133,10 +133,10 @@ class RequesterTest : TestAbstract() {
|
|||||||
.getQuery("Test/selectPaginated")
|
.getQuery("Test/selectPaginated")
|
||||||
.select(1, 2, mapOf("name" to "ff"))
|
.select(1, 2, mapOf("name" to "ff"))
|
||||||
Assert.assertNotNull(result)
|
Assert.assertNotNull(result)
|
||||||
Assert.assertEquals(result.result[0].name, "ff")
|
Assert.assertEquals("ff", result.result[0].name)
|
||||||
Assert.assertEquals(result.result[1].name, "ff-2")
|
Assert.assertEquals("ff-2", result.result[1].name)
|
||||||
Assert.assertEquals(result.total, 10)
|
Assert.assertEquals(10, result.total)
|
||||||
Assert.assertEquals(result.offset, 0)
|
Assert.assertEquals(0, result.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -147,10 +147,10 @@ class RequesterTest : TestAbstract() {
|
|||||||
.getFunction("test_function_paginated")
|
.getFunction("test_function_paginated")
|
||||||
.select(1, 2, mapOf("name" to "ff"))
|
.select(1, 2, mapOf("name" to "ff"))
|
||||||
Assert.assertNotNull(result)
|
Assert.assertNotNull(result)
|
||||||
Assert.assertEquals(result.result[0].name, "ff")
|
Assert.assertEquals("ff", result.result[0].name)
|
||||||
Assert.assertEquals(result.result[1].name, "ff-2")
|
Assert.assertEquals("ff-2", result.result[1].name)
|
||||||
Assert.assertEquals(result.total, 10)
|
Assert.assertEquals(10, result.total)
|
||||||
Assert.assertEquals(result.offset, 0)
|
Assert.assertEquals(0, result.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user