我正在尝试用科尔马创建一个非常简单的API。
用户可以像这样查询数据库:
localhost:8080/my_postgres_db/users.json?where[age]=50&limit=1 目前,当试图将where子句应用于现有的可组合查询时,我会收到一个错误。
clojure.lang.ArityException: Wrong number of args (2) passed to: core$where问题中的代码
(defn- comp-query [q [func arg]]
(let [sql-fn (ns-resolve 'korma.core (-> func name symbol))]
(sql-fn q arg)))
(defn compose-query [table col]
(reduce comp-query (select* table) col))使用
(def clauses {:where {:column1 10} :fields "a,b" :limit 10 :offset 500})
(-> (compose-query table clauses) select)除where子句外,一切行为都按预期进行。我可以结合限制,偏移和字段的任何方式,我选择,我得到了预期的结果。只有在地图中有一个:where键时,我才会遇到错误。
我是不是在尝试不该做的事?这是坏克洛尔吗?任何帮助都将不胜感激。
注意:,我读过这个,所以问题
编辑:在lein repl中,我可以以相同的方式手动编写一个查询,并且它可以工作
(where (select* "my_table") {:a 5})编辑:如果我将函数修改为:
(defn compose-query [table col]
; remove where clause to process seperately
(let [base (reduce comp-query (select* table) (dissoc col :where))]
(if-let [where-clause (:where col)]
(-> base (where where-clause))
base)))一切都如期而至。
发布于 2012-08-21 09:34:47
您可以使用where*函数,就像使用select*一样。只需让你的子句映射如下:
(def clauses {:where* {:column1 10} :fields "a,b" :limit 10 :offset 500})发布于 2012-08-21 00:14:27
只是一种预感;扩展一些线程宏会让我们很难看出它们是否正确:
core> (macroexpand-1 '(-> (compose-query table clauses) select))
(select (compose-query table clauses))
core> (macroexpand-1 '(-> func name symbol))
(clojure.core/-> (clojure.core/-> func name) symbol)
core> (macroexpand-1 '(clojure.core/-> func name))
(name func)把漏斗传给名字看上去很可疑。
https://stackoverflow.com/questions/12045825
复制相似问题