简体   繁体   English

记录 HugSQL 执行的 sql 语句/查询

[英]Log sql statments/queries executed by HugSQL

I want to log all SQL strings executed by HugSQL.我想记录 HugSQL 执行的所有 SQL 字符串。 I looked through the docs, but couldn't find anything.我查看了文档,但找不到任何内容。 Whats the recommended way?推荐的方式是什么?

I solved it myself by digging around the hugsql source.我自己通过挖掘 hugsql 源代码解决了这个问题。 It works similar to converting the result set of a generated function ( https://github.com/layerware/hugsql/issues/21 ):它的工作原理类似于转换生成函数的结果集 ( 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)

Per the HugSQL doc:根据 HugSQL 文档:

HugSQL generates a format internally known as sqlvec . HugSQL 生成内部称为sqlvec的格式。 The sqlvec format is a vector with an SQL string in the first position containing any ? sqlvec格式是一个向量,在第一个位置包含一个 SQL 字符串,其中包含任何? placeholders, followed by any number of parameter values to be applied to the SQL in positional order.占位符,后跟要按位置顺序应用于 SQL 的任意数量的参数值。

... ...

HugSQL provides the hugsql.core/def-sqlvec-fns macro to create functions returning the sqlvec format. HugSQL 提供了hugsql.core/def-sqlvec-fns宏来创建返回sqlvec格式的函数。 The created functions have an -sqlvec suffix by default, though this is configurable with the :fn-suffix option.默认情况下,创建的函数具有-sqlvec后缀,尽管可以使用:fn-suffix选项进行配置。 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.这些函数在开发/调试期间很有用,并且可以在不使用内置适配器数据库函数执行查询的情况下使用 HugSQL 的参数替换功能。

So you could use the sqlvec version of the functions colocated with where you call your HugSQL functions to log out the SQL that would be executed.因此,您可以使用与您调用 HugSQL 函数的位置共存的函数的sqlvec版本来注销将要执行的 SQL。

The doc actually provides the following example.该文档实际上提供了以下示例。 Given that you've loaded HugSQL queries like so:鉴于您已经像这样加载了 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")

And given the following function call:并给出以下函数调用:

(characters/characters-by-ids-specify-cols db
  {:ids [1 2], :cols ["name" "specialty"]}) 

You can get at the generated sqlvec with the following:您可以使用以下命令获取生成的 sqlvec:

(characters/characters-by-ids-specify-cols-sqlvec
  {:ids [1 2], :cols ["name" "specialty"]})

Which would return something like:这将返回如下内容:

["select name, specialty from characters
  where id in (?,?)",1,2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM