jdbc jsonb support in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns xxxxxx.jsonb-test | |
(:require | |
[clojure.test :refer [deftest is use-fixtures join-fixtures]] | |
[cheshire.core :as json] | |
[clojure.java.jdbc :as jdbc] | |
[xxxxxxx.test-db :refer [*db*] :as test-db]) | |
(:import | |
(org.postgresql.util PGobject) | |
(clojure.lang IPersistentMap))) | |
(use-fixtures :each (join-fixtures [test-db/fixture])) | |
(extend-protocol jdbc/ISQLValue | |
IPersistentMap | |
(sql-value [value] (doto (PGobject.) (.setType "jsonb") (.setValue (json/generate-string value))))) | |
(extend-protocol jdbc/IResultSetReadColumn | |
PGobject | |
(result-set-read-column [pgobj _ _] | |
(let [value (.getValue pgobj)] | |
(case (.getType pgobj) | |
"jsonb" (json/parse-string value true) | |
"json" (json/parse-string value true) | |
value)))) | |
(deftest jsonb-test | |
(jdbc/execute! *db* "create table table_1 (value_1 jsonb)") | |
(jdbc/insert! *db* :table_1 {:value_1 {:some-key [1 2 3]}}) | |
(is (= [{:value_1 {:some-key [1 2 3]}}] (jdbc/query *db* "select * from table_1")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment