gpt4 book ai didi

clojure:使用 korma 动态组合查询

转载 作者:行者123 更新时间:2023-12-01 00:40:50 26 4
gpt4 key购买 nike

我正在尝试使用 korma 创建一个非常简单的 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 子句外,一切都按预期运行。我可以以我选择的任何方式组合限制、偏移和字段,并获得预期的结果。只有当我的 map 中有 :where 键时,我才会遇到错误。

我是否尝试了一些我不应该做的事情?这是糟糕的 clojure 吗?任何帮助将不胜感激。

注意:我已阅读此 SO question

编辑:来自 lein repl 我可以以相同的方式手动编写查询并且它有效

(where (select* "my_table") {:a 5})

编辑:如果我将我的 compose-query 函数修改为:

(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)))

一切都按预期进行。

最佳答案

这里的问题是korma.core/where不是函数,需要特殊处理。 Where 不能作为函数实现,但仍能正确处理诸如 (where query (or (= :hits 1) (> :hits 5)))

关于clojure:使用 korma 动态组合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12045825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com