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、マクロであり、2番目の例はリストリテラルをそれに渡し、マクロにフォームを評価する機会を与えません。

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 に答える