kit-clj / kit

Lightweight, modular framework for scalable web development in Clojure
https://kit-clj.github.io/
MIT License
476 stars 44 forks source link

how to log the sql executed #97

Closed vinurs closed 1 year ago

vinurs commented 1 year ago

hello, when i exec a fn like this

(query-fn :get-user-by-id {:id 0})

is there any way to log the final sql string it sends to db?

yogthos commented 1 year ago

here's one approach for inspecting the queries that HugSQL generates:

(defn log-sqlvec [sqlvec] 
  (log/info (->> sqlvec
                 (map #(clojure.string/replace (or % "") #"\n" ""))
                 (clojure.string/join " ; "))))

(defn log-command-fn [this db sqlvec options]
  (log-sqlvec sqlvec)
  (condp contains? (:command options)
    #{:!} (hugsql.adapter/execute this db sqlvec options)
    #{:? :<!} (hugsql.adapter/query this db sqlvec options)))

(defmethod hugsql.core/hugsql-command-fn :! [_sym] `log-command-fn)
(defmethod hugsql.core/hugsql-command-fn :<! [_sym] `log-command-fn)
(defmethod hugsql.core/hugsql-command-fn :? [_sym] `log-command-fn)
vinurs commented 1 year ago

@yogthos wow, thanks for ur help 😄