Fork me on GitHub
#sql
<
2016-09-23
>
richiardiandrea19:09:45

hello folks, I am using honesql and I was wondering if I can avoid honeysql.core/format to transform keyword and instead leave them to the jdbc layer and thread them with my own extend-protocol jdbc/ISQLValue

donaldball19:09:46

You could re-implement honeysql.format.ToSql on clojure.lang.Keyword to be the identity fn I guess

donaldball19:09:14

I'd think pretty hard about introducing my own type tho

richiardiandrea19:09:29

thanks @donaldball from what I am trying at the repl, it looks like the namespaced keyword is not handled

richiardiandrea19:09:37

so I might have to do it

richiardiandrea19:09:27

> (-> (insert-into :events)
           (values [event]) ;; {:events/:type :events/chat-customer ...}
           (returning :*)
           sql/format)
;; => ["INSERT INTO events (type, nonce, payload) VALUES (chat_customer, ?, (    )) RETURNING *" 1142]

richiardiandrea19:09:53

oh sorry about the indentation

richiardiandrea20:09:10

oh now we are talking:

(somewhere)

(defn keyword-sql-str
  [kw]
  (if-let [nmspc (namespace kw)]
    (str nmspc "/" (name kw))
    (name kw)))

(extend-protocol sql/ToSql
  clojure.lang.Keyword
  (to-sql [x] (keyword-sql-str x)))

(-> (insert-into :events)
                                (values [event])
                                (returning :*)
                                sql/format)
;; => ["INSERT INTO events (events/type, events/nonce, events/payload) VALUES (events/chat-customer, ?, (    )) RETURNING *" 1142]

richiardiandrea20:09:02

all transformed a strings, thanks @donaldball for pointing me in the right direction!

richiardiandrea20:09:23

just need to figure out the column names

donaldball20:09:51

Note you’re probably going to lose all of the fancy “convert dotted keywords into function calls” niceties

richiardiandrea20:09:46

oh yes I see that I am already kind of losing the column names...maybe at this point it is better for me to use jdbc directly

richiardiandrea23:09:59

spent some time on this now and there is no real easy way to round-trip, the jdbc/insert! function does not seem to return the correct type for jdbc/result-set-read-column