jac*_*dbd 6 clojure hugsql vscode-calva clj-kondo
我使用 VS Code Calva扩展编写 Clojure ,它使用clj-kondo对我的代码执行静态分析。
我正在使用HugSQL从 SQL 查询和语句创建 Clojure 函数。
我知道我可以使用像conman这样的库来处理数据库连接和 HugSQL 集成,事实上我过去使用过它并且我喜欢它,但这次我想保持原样并自己与 HugSQL 交谈。
HugSQL 的def-db-fns宏接受一个 SQL 文件,并根据该文件中包含的 SQL 查询和语句创建 Clojure 函数。
我下面的代码有效,但 clj-kondo 抱怨这seed-mytable!是一个未解析的符号。
(ns my-app.db
"This namespace represents the bridge between the database world and the clojure world."
(:require [environ.core :refer [env]]
[hugsql.core :as hugsql]
[nano-id.core :refer [nano-id]]))
;; This create the function seed-mytable!, but clj-kondo doesn't (cannot?) know it.
(hugsql/def-db-fns "sql/mytable.sql")
;; The functions created by HugSQL can accept a db-spec, a connection, a connection pool,
;; or a transaction object. Let's keep it simple and use a db-spec for a SQLite database.
(def db-spec {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname (env :database-subname)})
(defn db-seed
"Populate the table with some fakes."
[]
(let [fakes [[(nano-id) "First fake title" "First fake content"]
[(nano-id) "Second fake title" "Second fake content"]]]
;; clj-kondo complains that seed-my-table! is an unresolved symbol
(seed-mytable! db-spec {:fakes fakes})))
Run Code Online (Sandbox Code Playgroud)
我明白为什么 clj-kondo 抱怨:seed-mytable!没有在任何地方定义,它在调用def-db-fns宏时“注入”在这个命名空间中。
有没有办法告诉 clj-kondo 在调用hugsql/def-db-fns宏后该符号确实存在?
可能它没有那么有用,但这是我用 HugSQL 加载的 SQL 文件。
-- :name seed-mytable!
-- :command :execute
-- :result :affected
-- :doc Seed the `mytable` table with some fakes.
INSERT INTO mytable (id, title, content)
VALUES :t*:fakes;
Run Code Online (Sandbox Code Playgroud)
Mic*_*ent 10
从 clj-kondo文档:
有时通过执行宏来引入变量,例如在使用HugSQL的def-db-fns. 您可以使用declare. 例子:
(ns hugsql-example
(:require [hugsql.core :as hugsql]))
(declare select-things)
;; this will define a var #'select-things:
(hugsql/def-db-fns "select_things.sql")
(defn get-my-things [conn params]
(select-things conn params))
Run Code Online (Sandbox Code Playgroud)
如果 HugSQL 引入的符号数量变得过于笨拙,请考虑引入一个单独的命名空间,HugSQL 在其中生成 vars:
foo.db.hugsql。然后你可以从foo.dbwith
引用这个命名空间(require '[foo.db.hugsql :as sql]) (sql/insert! ...),clj-kondo 不会抱怨这个。
| 归档时间: |
|
| 查看次数: |
998 次 |
| 最近记录: |