Clojure Korma:如何在函数中使用where宏?
我尝试在函数中使用where宏:Clojure Korma:如何在函数中使用where宏?,clojure,korma,Clojure,Korma,我尝试在函数中使用where宏: (defentity student (pk :id) (table :student) (entity-fields :id :name :age :class) (database prod)) (defn query-student [cond] (select student (where cond))) 我测试它: (select student (where {:age [> 13]})) (query-s
(defentity student
(pk :id)
(table :student)
(entity-fields :id :name :age :class)
(database prod))
(defn query-student [cond]
(select student
(where cond)))
我测试它:
(select student
(where {:age [> 13]}))
(query-student '{:age [> 13]})
看起来不错,但是这个
(select student
(where (or {:age [> 13]} {:class [in ["1" "2" "3"]]})))
(query-student '(or {:age [> 13]} {:class [in ["1" "2" "3"]]}))
不行
Failure to execute query with SQL:
SELECT "student".* FROM "student" WHERE (or {:age [> 13]} {:class [in ["1" "2" "3"]]}) :: []
PSQLException:
Message: ERROR: syntax error at or near "or"
Location:42
SQLState: 42601
Error Code: 0
PSQLException ERROR: syntax error at or near "or"
Location:42 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2101)
我想知道为什么?有什么问题吗?在Korma中,
是一个宏,因此,您的第二个示例将一个列表文本传递给它,而没有给宏评估表单的机会
尝试将querystudent
函数改为宏,如下所示
(defmacro query-student [cond]
`(select student
(where ~cond)))
另外,在使用宏时,您不需要引用表单:
(查询学生(或{:年龄[>13]}{:班级[在[“1”“2”“3”]}))
希望这有帮助。谢谢你的帮助。将函数更改为宏即可。但是我想将{:age[>13]}{:class[in[“1”“2”“3”]}保存在一个文件中,并在执行之前读取它。我可以将数据(或{:age[>13]}{:class[in[“1”“2”“3”]})转换为表单吗?这应该不是问题。您应该能够使用Clojure从文件中读取表单并将其传递给上面的宏。你试过了吗?