2

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!

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure if this answers your question, because it might be obvious. as-sql-type needs to be qualified because it is a var in the jdbc.proto namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.