I'm trying to dynamically add WHERE conditions to Korma SQL query
(-> the-query
(where {:archived false})
(add-where-conditions params)
(limit 200)
(select))
I'm trying to dynamically build call to korma's where function. The call would look something like (where query (or (between :freq [100 200]) (between :freq [300 400]) ... ))
. The helper function make-conds makes a list of the arguments for where function like: (or (between :freq [100 200]) ...
I attempted the following approaches to build the dynamic where call. Only the first one, the one with eval
works. Why? Is there a better way to do this?
(defn add-where-conditions [query params]
(eval (list 'where query (make-conds params))))
(defmacro add-where-conditions-2 [query params]
(list 'where query (make-conds params))) ; broken
(defmacro add-where-conditions-3 [query params]
`(where ~query ~(make-conds params))) ; broken
Disclaimer: I'm a newbie to Clojure and Korma