Skip to content

Commit

Permalink
Fixes #21: Override Timestamp using from-pg-value.
Browse files Browse the repository at this point in the history
  • Loading branch information
alaisi committed May 3, 2016
1 parent d6f2d2b commit 7ecbf5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/postgres/async.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
[com.github.pgasync.impl.conversion DataConverter]))

(defmulti from-pg-value (fn [oid value] oid))
(defmethod from-pg-value :default [oid value] ::raw-value)

(defprotocol IPgParameter
(to-pg-value [value]))

(defn- create-converter []
(proxy [DataConverter] []
(toConvertable [oid value]
(from-pg-value oid value))
(toObject [oid value]
(when value
(let [val (from-pg-value oid value)]
(if (= ::raw-value val)
(proxy-call-with-super #(.toObject ^DataConverter this oid value)
this "toObject")
val))))
(fromConvertable [value]
(to-pg-value value))))

Expand Down
15 changes: 15 additions & 0 deletions test/postgres/async_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
(is (instance? SqlException (<!! c)))
(is (nil? (<!! c)))))

(deftest query-for-overridden-types

(defmethod from-pg-value com.github.pgasync.impl.Oid/TIMESTAMP [oid ^bytes value]
(String. value "utf-8"))

(testing "timestamp can be returned as string"
(let [c (query-rows! *db* ["select $1::TIMESTAMP as ts"
"2016-05-01T12:00:00.001"])]
(is (= {:ts "2016-05-01 12:00:00.001"} (<!! c)))))

(testing "unsupported type throws an exception"
(let [c (query-rows! *db* ["select '<x/>'::xml"])
^Throwable ex (<!! c)]
(is (= "Unknown conversion source: XML" (.getMessage ex))))))

(deftest inserts

(testing "insert returns row count"
Expand Down

0 comments on commit 7ecbf5c

Please sign in to comment.