我正在尝试使用函数中的宏:
(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)
我想知道为什么?有什么问题?
答案 0 :(得分:2)
在Korma中,where
是一个宏,因此您的第二个示例将列表文字传递给它,而不是让宏有机会评估表单。
尝试将query-student
功能更改为宏,而不是沿着这些行
(defmacro query-student [cond]
`(select student
(where ~cond)))
作为额外奖励,您在使用宏时无需引用表单:
(query-student (or {:age [> 13]} {:class [in ["1" "2" "3"]]}))
希望这有帮助。