Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created December 6, 2010 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/79667390ab7e2887d065 to your computer and use it in GitHub Desktop.
Save jcromartie/79667390ab7e2887d065 to your computer and use it in GitHub Desktop.
(defn compile-where
[record]
(let [clause (apply list 'and (map #(list '= (key %) (val %)) record))]
`(ql/where ~clause)))
(defn match-record
[record]
(eval (compile-where record)))
@LauJensen
Copy link

clojureql.core> (let [rec {:id 5 :name "Fred" :age 22}]
                   (reduce (fn [acc [k v]] (and* acc (=* k v))) (predicate) rec))
#:clojureql.predicates.APredicate{:stmt ["((( AND (id = ?)) AND (name = ?)) AND (age = ?))"], :env [5 "Fred" 22]}

I see that the Predicate protocol doesnt handle empty ANDs gracefully. But if that was fixed, wouldn't this approach be preferred?

@LauJensen
Copy link

Fixed in Master - The above code will generate a working predicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment