Skip to content

Commit 7ecbf5c

Browse files
committed
Fixes #21: Override Timestamp using from-pg-value.
1 parent d6f2d2b commit 7ecbf5c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/postgres/async.clj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
[com.github.pgasync.impl.conversion DataConverter]))
77

88
(defmulti from-pg-value (fn [oid value] oid))
9+
(defmethod from-pg-value :default [oid value] ::raw-value)
10+
911
(defprotocol IPgParameter
1012
(to-pg-value [value]))
1113

1214
(defn- create-converter []
1315
(proxy [DataConverter] []
14-
(toConvertable [oid value]
15-
(from-pg-value oid value))
16+
(toObject [oid value]
17+
(when value
18+
(let [val (from-pg-value oid value)]
19+
(if (= ::raw-value val)
20+
(proxy-call-with-super #(.toObject ^DataConverter this oid value)
21+
this "toObject")
22+
val))))
1623
(fromConvertable [value]
1724
(to-pg-value value))))
1825

test/postgres/async_test.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@
6666
(is (instance? SqlException (<!! c)))
6767
(is (nil? (<!! c)))))
6868

69+
(deftest query-for-overridden-types
70+
71+
(defmethod from-pg-value com.github.pgasync.impl.Oid/TIMESTAMP [oid ^bytes value]
72+
(String. value "utf-8"))
73+
74+
(testing "timestamp can be returned as string"
75+
(let [c (query-rows! *db* ["select $1::TIMESTAMP as ts"
76+
"2016-05-01T12:00:00.001"])]
77+
(is (= {:ts "2016-05-01 12:00:00.001"} (<!! c)))))
78+
79+
(testing "unsupported type throws an exception"
80+
(let [c (query-rows! *db* ["select '<x/>'::xml"])
81+
^Throwable ex (<!! c)]
82+
(is (= "Unknown conversion source: XML" (.getMessage ex))))))
83+
6984
(deftest inserts
7085

7186
(testing "insert returns row count"

0 commit comments

Comments
 (0)