gpt4 book ai didi

sql - 为什么 eval 在 Korma 中构建 WHERE 条件时宏不起作用

转载 作者:行者123 更新时间:2023-12-01 05:20:03 25 4
gpt4 key购买 nike

我正在尝试向 Korma SQL 查询动态添加 WHERE 条件

(-> the-query
(where {:archived false})
(add-where-conditions params)
(limit 200)
(select))

我正在尝试动态构建对 korma 的 where 函数的调用。调用看起来像 (where query (or (between :freq [100 200]) (between :freq [300 400]) ... )) .辅助函数 make-conds 列出了 where 函数的参数列表,例如: (or (between :freq [100 200]) ...
我尝试了以下方法来构建动态 where 调用。只有 第一个 , 与 eval 的那个作品。为什么?有一个更好的方法吗?
(defn add-where-conditions [query params]
(eval (list 'where query (make-conds params))))

(defmacro add-where-conditions-2 [query params]
(list 'where query (make-conds params))) ; broken

(defmacro add-where-conditions-3 [query params]
`(where ~query ~(make-conds params))) ; broken

免责声明:我是 Clojure 和 Korma 的新手

最佳答案

宏不起作用的原因是在这两种情况下 params 的值参数是符号 params .这就是为什么在 add-where-conditions-2add-where-conditions-3当宏调用时 (make-conds params) ,函数接收到的值不是你想的列表而是符号params ,显示以下行中的错误:

IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Symbol  clojure.lang.RT.seqFrom (RT.java:505)

第一种情况有效,因为该函数接收列表(而不是符号)作为 params 的值。参数,所以 eval接收列表 (where {:your-query nil} (or (between :freq [100 200]) ,,,)) ,这就是 where宏期望并知道如何处理。

where 宏解析表达式以搜索它用于构建表达式的一些谓词。 where* ,功能替代品,没有那种功能,所以我想不出 eval的替代品吃蛋糕,也吃蛋糕。

关于sql - 为什么 eval 在 Korma 中构建 WHERE 条件时宏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135794/

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