clean code

This commit is contained in:
2023-04-07 01:23:30 +02:00
parent f8f3273f18
commit c268efb44f
2 changed files with 78 additions and 19 deletions

View File

@@ -227,10 +227,11 @@ class Connection(
} catch (e: Throwable) { } catch (e: Throwable) {
logger?.info( logger?.info(
""" """
Query Error: |Query Error:
${sql.prependIndent()}, |${sql},
${values.joinToString(", ").prependIndent()} |Arguments (${values.length}):
""".trimIndent(), |${values.joinToString(", ").ifBlank { "No arguments" }.prependIndent()}
""".trimMargin().prependIndent(" > "),
e e
) )
throw e throw e

View File

@@ -29,12 +29,12 @@ class ConnectionTest : TestAbstract() {
private class ObjTestWithParameterObject(val id: UUID, var first: ParameterObject, var second: ParameterObject) private class ObjTestWithParameterObject(val id: UUID, var first: ParameterObject, var second: ParameterObject)
@SqlSerializable @SqlSerializable
private class ParameterObject(var third: String) private class ParameterObject(var third: String)
private class ObjTest4(var third: String) private class ObjTest4
@Test @Test
fun serializable() { fun serializable() {
assertTrue(ObjTest("plop")::class.hasAnnotation<SqlSerializable>()) assertTrue(ObjTest("plop")::class.hasAnnotation<SqlSerializable>())
assertFalse(ObjTest4("plop")::class.hasAnnotation<SqlSerializable>()) assertFalse(ObjTest4()::class.hasAnnotation<SqlSerializable>())
} }
@Test @Test
@@ -63,19 +63,26 @@ class ConnectionTest : TestAbstract() {
assertNotNull(objs) assertNotNull(objs)
assertEquals(objs.size, 2) assertEquals(objs.size, 2)
assertEquals(objs[0].id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) assertEquals(objs[0].id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
assertEquals(objs[0].title, "plop")
assertEquals(objs[0].test!!.id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) assertEquals(objs[0].test!!.id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
} }
@Test @Test
fun `test call request with args`() { fun `test call request with args`() {
val result: ObjTest? = connection.execute("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)", listOf("myName")) val result: ObjTest? = connection.execute(
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)",
listOf("myName")
)
assertNotNull(result) assertNotNull(result)
assertEquals("myName", result.name) assertEquals("myName", result.name)
} }
@Test @Test
fun `test call request without args`() { fun `test call request without args`() {
val result: ObjTest? = connection.execute("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', 'myName')", object : TypeReference<ObjTest>() {}) { val result: ObjTest? = connection.execute(
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', 'myName')",
object : TypeReference<ObjTest>() {}
) {
assertEquals("myName", this.deserialize<ObjTest>()?.name) assertEquals("myName", this.deserialize<ObjTest>()?.name)
} }
assertNotNull(result) assertNotNull(result)
@@ -100,7 +107,10 @@ class ConnectionTest : TestAbstract() {
@Test @Test
fun callRequestWithArgsEntity() { fun callRequestWithArgsEntity() {
val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")) val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
val obj: ObjTest? = connection.execute("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);", listOf(o)) val obj: ObjTest? = connection.execute(
"select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);",
listOf(o)
)
assertNotNull(obj) assertNotNull(obj)
assertEquals(UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"), obj.id) assertEquals(UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"), obj.id)
assertEquals("myName", obj.name) assertEquals("myName", obj.name)
@@ -109,7 +119,10 @@ class ConnectionTest : TestAbstract() {
@Test @Test
fun `test update Entity`() { fun `test update Entity`() {
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
val objUpdated: ObjTest? = connection.execute("select ?::jsonb || jsonb_build_object('name', 'after');", obj.toTypeReference(), listOf(obj)) val objUpdated: ObjTest? = connection.execute(
"select ?::jsonb || jsonb_build_object('name', 'after');",
obj.toTypeReference(), listOf(obj)
)
assertNotNull(objUpdated) assertNotNull(objUpdated)
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id) assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id)
assertEquals("after", objUpdated.name) assertEquals("after", objUpdated.name)
@@ -118,7 +131,11 @@ class ConnectionTest : TestAbstract() {
@Test @Test
fun `test update Entity with vararg`() { fun `test update Entity with vararg`() {
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
val objUpdated: ObjTest? = connection.execute("select :obj::jsonb || jsonb_build_object('name', 'after');", obj.toTypeReference(), "obj" to obj) val objUpdated: ObjTest? = connection.execute(
"select :obj::jsonb || jsonb_build_object('name', 'after');",
obj.toTypeReference(),
"obj" to obj
)
assertNotNull(objUpdated) assertNotNull(objUpdated)
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id) assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id)
assertEquals("after", objUpdated.name) assertEquals("after", objUpdated.name)
@@ -127,14 +144,24 @@ class ConnectionTest : TestAbstract() {
@Test @Test
fun callExec() { fun callExec() {
val o = ObjTest("myName") val o = ObjTest("myName")
val result = connection.exec("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')", listOf(o)) val result = connection.exec(
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')",
listOf(o)
)
assertEquals(1, result.rowsAffected) assertEquals(1, result.rowsAffected)
} }
@Test @Test
fun `select one with named parameters`() { fun `select one with named parameters`() {
val result: ObjTest3? = connection.execute( val result: ObjTest3? = connection.execute(
"SELECT json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int)", """
SELECT json_build_object(
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
'first', :first::text,
'second', :second::text,
'third', :third::int
)
""".trimIndent(),
mapOf( mapOf(
"first" to "ff", "first" to "ff",
"second" to "sec", "second" to "sec",
@@ -150,13 +177,20 @@ class ConnectionTest : TestAbstract() {
@Test @Test
fun `select one with named parameters object`() { fun `select one with named parameters object`() {
val result: ObjTestWithParameterObject? = connection.execute( val result: ObjTestWithParameterObject? = connection.execute(
"SELECT json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::json, 'second', :second::json)", """
SELECT json_build_object(
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
'first', :first::json,
'second', :second::json
)
""".trimIndent(),
mapOf( mapOf(
"first" to ParameterObject("one"), "first" to ParameterObject("one"),
"second" to ParameterObject("two") "second" to ParameterObject("two")
) )
) )
assertNotNull(result) assertNotNull(result)
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result.id.toString())
assertEquals("one", result.first.third) assertEquals("one", result.first.third)
assertEquals("two", result.second.third) assertEquals("two", result.second.third)
} }
@@ -166,8 +200,18 @@ class ConnectionTest : TestAbstract() {
val result: List<ObjTest3>? = connection.execute( val result: List<ObjTest3>? = connection.execute(
""" """
SELECT json_build_array( SELECT json_build_array(
json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int), json_build_object(
json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int) 'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
'first', :first::text,
'second', :second::text,
'third', :third::int
),
json_build_object(
'id', 'ce9ae3c9-dc0e-4561-a168-811b996d913e',
'first', :first::text,
'second', :second::text,
'third', :third::int
)
) )
""".trimIndent(), """.trimIndent(),
mapOf( mapOf(
@@ -177,6 +221,7 @@ class ConnectionTest : TestAbstract() {
) )
) )
assertNotNull(result) assertNotNull(result)
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result[0].id.toString())
assertEquals("ff", result[0].first) assertEquals("ff", result[0].first)
assertEquals("sec", result[0].second) assertEquals("sec", result[0].second)
assertEquals(123, result[0].third) assertEquals(123, result[0].third)
@@ -188,7 +233,7 @@ class ConnectionTest : TestAbstract() {
""" """
SELECT json_build_array( SELECT json_build_array(
json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int), json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int),
json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int) json_build_object('id', '0c9d55d2-f69a-4750-a278-fac821774276', 'first', :first::text, 'second', :second::text, 'third', :third::int)
) )
""".trimIndent(), """.trimIndent(),
"first" to "ff", "first" to "ff",
@@ -196,6 +241,7 @@ class ConnectionTest : TestAbstract() {
"second" to "sec" "second" to "sec"
) )
assertNotNull(result) assertNotNull(result)
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result[0].id.toString())
assertEquals("ff", result[0].first) assertEquals("ff", result[0].first)
assertEquals("sec", result[0].second) assertEquals("sec", result[0].second)
assertEquals(123, result[0].third) assertEquals(123, result[0].third)
@@ -210,11 +256,17 @@ class ConnectionTest : TestAbstract() {
) )
val result: ObjTest3? = connection.execute( val result: ObjTest3? = connection.execute(
""" """
SELECT json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int), 'plop'::text as other SELECT json_build_object(
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
'first', :first::text,
'second', :second::text,
'third', :third::int
), 'plop'::text as other
""".trimIndent(), """.trimIndent(),
params params
) { ) {
assertNotNull(it) assertNotNull(it)
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", it.id.toString())
assertEquals("ff", it.first) assertEquals("ff", it.first)
assertEquals("plop", rows[0].getString("other")) assertEquals("plop", rows[0].getString("other"))
} }
@@ -244,7 +296,13 @@ class ConnectionTest : TestAbstract() {
fun `select one in transaction`() { fun `select one in transaction`() {
connection.inTransaction { connection.inTransaction {
execute<ObjTestWithParameterObject>( execute<ObjTestWithParameterObject>(
"SELECT json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::json, 'second', :second::json)", """
SELECT json_build_object(
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
'first', :first::json,
'second', :second::json
)
""".trimIndent(),
mapOf( mapOf(
"first" to ParameterObject("one"), "first" to ParameterObject("one"),
"second" to ParameterObject("two") "second" to ParameterObject("two")