I can\'t find a way to insert Postgres\' array type with Clojure.
(sql/insert! db :things {:animals [\"cow\" \"pig\"]})
Didn\'t work which I ki
You can make clojure.java.jdbc automatically convert between Clojure vectors and SQL arrays by extending two protocols. This can be done from your own code:
(extend-protocol clojure.java.jdbc/ISQLParameter
clojure.lang.IPersistentVector
(set-parameter [v ^java.sql.PreparedStatement stmt ^long i]
(let [conn (.getConnection stmt)
meta (.getParameterMetaData stmt)
type-name (.getParameterTypeName meta i)]
(if-let [elem-type (when (= (first type-name) \_) (apply str (rest type-name)))]
(.setObject stmt i (.createArrayOf conn elem-type (to-array v)))
(.setObject stmt i v)))))
(extend-protocol clojure.java.jdbc/IResultSetReadColumn
java.sql.Array
(result-set-read-column [val _ _]
(into [] (.getArray val))))
REPL Example:
user> (def db (clj-postgresql.core/pool :dbname "test"))
#'user/db
user> (clojure.java.jdbc/query db ["SELECT ?::text[], ?::int[]" ["foo" "bar"] [1 2 3]])
({:int4 [1 2 3], :text ["foo" "bar"]})
I'm currently working on a library that will support PostgreSQL, and PostGIS types automatically. It's still very much work in process though https://github.com/remodoy/clj-postgresql