Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.9.12.67+pgjdbc-ng #1

Open
wants to merge 4 commits into
base: 2.9.12.67+postgres
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[org.clojure/clojure "1.9.0"]
[org.clojure/tools.cli "0.3.7"]
[org.clojure/tools.logging "0.4.1"]
[org.postgresql/postgresql "42.2.2"]
[com.impossibl.pgjdbc-ng/pgjdbc-ng "0.6"]
[org.webjars.bower/tether "1.4.4"]
[org.webjars/bootstrap "4.1.1"]
[org.webjars/font-awesome "5.1.0"]
Expand Down Expand Up @@ -50,7 +50,7 @@
:dev [:project/dev :profiles/dev]
:test [:project/dev :project/test :profiles/test]

:project/dev {:jvm-opts ["-Dconf=dev-config.edn" "--add-modules" "java.xml.bind"]
:project/dev {:jvm-opts ["-Dconf=dev-config.edn" #_"--add-modules" #_"java.xml.bind"]
:dependencies [[expound "0.7.1"]
[pjstadig/humane-test-output "0.8.3"]
[prone "1.6.0"]
Expand Down
45 changes: 3 additions & 42 deletions src/clj/luminusdiff/db/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
[conman.core :as conman]
[luminusdiff.config :refer [env]]
[mount.core :refer [defstate]])
(:import org.postgresql.util.PGobject
java.sql.Array
clojure.lang.IPersistentMap
clojure.lang.IPersistentVector
[java.sql
BatchUpdateException
PreparedStatement]))
(:import
com.impossibl.postgres.api.jdbc.PGNotificationListener))

(defstate ^:dynamic *db*
:start (if-let [jdbc-url (env :database-url)]
(conman/connect! {:jdbc-url jdbc-url})
Expand All @@ -23,38 +19,3 @@
:stop (conman/disconnect! *db*))

(conman/bind-connection *db* "sql/queries.sql")

(extend-protocol jdbc/IResultSetReadColumn
Array
(result-set-read-column [v _ _] (vec (.getArray v)))

PGobject
(result-set-read-column [pgobj _metadata _index]
(let [type (.getType pgobj)
value (.getValue pgobj)]
(case type
"json" (parse-string value true)
"jsonb" (parse-string value true)
"citext" (str value)
value))))

(defn to-pg-json [value]
(doto (PGobject.)
(.setType "jsonb")
(.setValue (generate-string value))))

(extend-type clojure.lang.IPersistentVector
jdbc/ISQLParameter
(set-parameter [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 (to-pg-json v))))))

(extend-protocol jdbc/ISQLValue
IPersistentMap
(sql-value [value] (to-pg-json value))
IPersistentVector
(sql-value [value] (to-pg-json value)))