From 8e52b22d4a1f1088ee35057dc5e1fc2a11f43d21 Mon Sep 17 00:00:00 2001 From: vinurs Date: Thu, 23 May 2024 23:29:48 +0800 Subject: [PATCH] feat: extend SettableParameter protocol for IPersistentVector --- libs/kit-postgres/src/kit/edge/db/postgres.clj | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libs/kit-postgres/src/kit/edge/db/postgres.clj b/libs/kit-postgres/src/kit/edge/db/postgres.clj index 65b42a1..f27d84d 100644 --- a/libs/kit-postgres/src/kit/edge/db/postgres.clj +++ b/libs/kit-postgres/src/kit/edge/db/postgres.clj @@ -5,7 +5,7 @@ [next.jdbc.prepare :as prepare] [next.jdbc.result-set :as result-set]) (:import - [clojure.lang IPersistentMap] + [clojure.lang IPersistentMap IPersistentVector] [java.sql Array PreparedStatement Timestamp] [java.time Instant LocalDate LocalDateTime] [org.postgresql.util PGobject])) @@ -57,4 +57,14 @@ IPersistentMap (set-parameter [m ^PreparedStatement s i] - (.setObject s i (->pgobject m)))) + (.setObject s i (->pgobject m))) + + IPersistentVector + (set-parameter [^clojure.lang.IPersistentVector v ^java.sql.PreparedStatement stmt ^long idx] + (let [conn (.getConnection stmt) + meta (.getParameterMetaData stmt) + type-name (.getParameterTypeName meta idx)] + (if-let [elem-type (when (= (first type-name) \_) + (apply str (rest type-name)))] + (.setObject stmt idx (.createArrayOf conn elem-type (to-array v))) + (.setObject stmt idx (->pgobject v))))))