Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure Korma:如何在函数中使用where宏?_Clojure_Korma - Fatal编程技术网

Clojure Korma:如何在函数中使用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

我尝试在函数中使用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中,
是一个宏,因此,您的第二个示例将一个列表文本传递给它,而没有给宏评估表单的机会

尝试将
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从文件中读取表单并将其传递给上面的宏。你试过了吗?