1

我正在尝试在函数中使用 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)

我想知道为什么?有问题?

4

1 回答 1

2

在 Korma 中,where是一个宏,因此您的第二个示例将一个列表文字传递给它,而不是让宏有机会评估表单。

尝试将您的query-student函数更改为宏,沿着这些行

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

作为额外的好处,您在使用宏时不需要引用表单:

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

希望这可以帮助。

于 2013-04-08T12:15:54.323 回答