Extending support for jsonb with clojure.jdbc and clojure/data.json
This is my first foray into Clojure (and functional programming altogether) and I'm trying to extend support for JSONB using clojure.jdbc library. I've been using this as a guide:
http://niwibe.github.io/clojure.jdbc/#_extend_sql_types
Also using leinengen so I have my dependencies setup with the following:
[org.clojure/clojure "1.6.0"]
[clj-http "0.9.1"]
[clojure.jdbc "0.3.1"]
[postgresql "9.3-1102.jdbc41"]
[org.clojure/data.json "0.2.5"]
Then I have my code which looks like this:
(require '[jdbc.proto])
(require '[clojure.data.json :as json])
(import 'org.postgresql.util.PGobject)
(extend-protocol jdbc.proto/ISQLType
clojure.lang.IPersistentMap
(set-stmt-parameter! [this conn stmt index]
(let [prepared-value (as-sql-type this conn)]
(.setObject stmt index prepared-value)))
(as-sql-type [this conn]
(doto (PGobject.)
(.setType "jsonb")
(.setValue (json/write-str)))))
When I run the REPL and try to run my load file command like
(load-file "src/db/jdbc-types-jsonb.clj")
The compiler complaines with this error:
CompilerException java.lang.RuntimeException: Unable to resolve symbol: as-sql-type in this context, compiling:(/Users/akmiller/Source/personal/clojure-pg/src/db/jdbc-types-jsonb.clj:14:26)
I'm trying to understand why it doesn't see as-sql-type as 'this' at that point should be the protocol correct? Sorry if this is a noob type issue (I'm sure it is) but I just don't see the issue and I need some Clojure expertise to help me get past this very small hurdle!
I was able to fix the issue by changing this line:
(let [prepared-value (as-sql-type this conn)]
to this:
(let [prepared-value (jdbc.proto/as-sql-type this conn)]
I'm still not sure why, in this case, I'd need to fully qualify the function name since this should reference the type I was extending (or so I thought). If someone has any more clarification on why it needs to be qualified there I'd be glad to hear it.
链接地址: http://www.djcxy.com/p/82574.html上一篇: 高阶函数的Clojure类型提示