繁体   English   中英

Clojure Korma:如何在函数中使用where宏?

[英]Clojure Korma: How to use where macro in a function?

我正在尝试在函数中使用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-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中,宏在where ,因此您的第二个示例向其传递列表文字,而不是给宏提供评估表单的机会。

尝试按照以下步骤将您的query-student函数更改为宏

(defmacro query-student [cond]
  `(select student
    (where ~cond))) 

另外,使用宏时,您无需引用表格:

(query-student (or {:age [> 13]} {:class [in ["1" "2" "3"]]}))

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM