From d5b847230456a4103bf7041ee43fb331d253d33a Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 5 Apr 2023 23:13:39 +0200 Subject: [PATCH] add Connection.selectAny() --- .../fr/postgresjson/connexion/Connection.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index 1ffdfea..c5c5a30 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -128,6 +128,26 @@ class Connection( } } + /** + * Select multiple or one [EntityI] with [Map] of parameters + */ + override fun selectAny( + sql: String, + typeReference: TypeReference, + values: Map, + block: QueryResult.(R) -> Unit + ): R { + val result = exec(sql, values) + val json = result.rows[0].getString(0) + return if (json === null) { + null as R + } else { + serializer.deserialize(json, typeReference) + }.also { + block(result, it) + } + } + /* Select Paginated */ /** @@ -152,7 +172,7 @@ class Connection( return line.run { val firstLine = rows.firstOrNull() ?: queryError("The query has no return", sql, newValues) - if (!(firstLine as ArrayRowData).mapping.keys.contains("total")) queryError("""The query not return the "total" column""", sql, newValues, rows) + if (!rows.columnNames().contains("total")) queryError("""The query not return the "total" column""", sql, newValues, rows) val total = try { firstLine.getInt("total") ?: queryError("The query return \"total\" must not be null", sql, newValues, rows) } catch (e: ClassCastException) {