HugSQL 执行的日志 sql statments/queries
Log sql statments/queries executed by HugSQL
我想记录 HugSQL 执行的所有 SQL 个字符串。我查看了文档,但找不到任何东西。推荐的方法是什么?
根据拥抱SQL 文档:
HugSQL generates a format internally known as sqlvec. The sqlvec format is a vector with an SQL string in the first position containing any ?
placeholders, followed by any number of parameter values to be applied to the SQL in positional order.
...
HugSQL provides the hugsql.core/def-sqlvec-fns
macro to create functions returning the sqlvec format. The created functions have an -sqlvec
suffix by default, though this is configurable with the :fn-suffix
option. These functions are helpful during development/debugging and for the purpose of using the parameter-replacing functionality of HugSQL without using the built-in adapter database functions to execute queries.
因此您可以使用 sqlvec
版本的函数与您调用 HugSQL 函数的位置并置以注销将要执行的 SQL。
该文档实际上提供了以下示例。假设您已经像这样加载了 HugSQL 查询:
(ns princess-bride.db.characters
(:require [hugsql.core :as hugsql]))
(hugsql/def-db-fns "princess_bride/db/sql/characters.sql")
(hugsql/def-sqlvec-fns "princess_bride/db/sql/characters.sql")
并给出以下函数调用:
(characters/characters-by-ids-specify-cols db
{:ids [1 2], :cols ["name" "specialty"]})
您可以通过以下方式获取生成的 sqlvec:
(characters/characters-by-ids-specify-cols-sqlvec
{:ids [1 2], :cols ["name" "specialty"]})
这会 return 类似于:
["select name, specialty from characters
where id in (?,?)",1,2]
我自己通过挖掘 hugsql 源代码解决了这个问题。它的工作原理类似于转换生成函数的结果集 (https://github.com/layerware/hugsql/issues/21):
(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)
我想记录 HugSQL 执行的所有 SQL 个字符串。我查看了文档,但找不到任何东西。推荐的方法是什么?
根据拥抱SQL 文档:
HugSQL generates a format internally known as sqlvec. The sqlvec format is a vector with an SQL string in the first position containing any
?
placeholders, followed by any number of parameter values to be applied to the SQL in positional order....
HugSQL provides the
hugsql.core/def-sqlvec-fns
macro to create functions returning the sqlvec format. The created functions have an-sqlvec
suffix by default, though this is configurable with the:fn-suffix
option. These functions are helpful during development/debugging and for the purpose of using the parameter-replacing functionality of HugSQL without using the built-in adapter database functions to execute queries.
因此您可以使用 sqlvec
版本的函数与您调用 HugSQL 函数的位置并置以注销将要执行的 SQL。
该文档实际上提供了以下示例。假设您已经像这样加载了 HugSQL 查询:
(ns princess-bride.db.characters
(:require [hugsql.core :as hugsql]))
(hugsql/def-db-fns "princess_bride/db/sql/characters.sql")
(hugsql/def-sqlvec-fns "princess_bride/db/sql/characters.sql")
并给出以下函数调用:
(characters/characters-by-ids-specify-cols db
{:ids [1 2], :cols ["name" "specialty"]})
您可以通过以下方式获取生成的 sqlvec:
(characters/characters-by-ids-specify-cols-sqlvec
{:ids [1 2], :cols ["name" "specialty"]})
这会 return 类似于:
["select name, specialty from characters
where id in (?,?)",1,2]
我自己通过挖掘 hugsql 源代码解决了这个问题。它的工作原理类似于转换生成函数的结果集 (https://github.com/layerware/hugsql/issues/21):
(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)