From 9083622a4a41052492c693baa7429309f4628317 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sun, 23 Jun 2013 02:13:53 +1000 Subject: Make select take a map and compile to equality tests. --- src/clojure_sql/dsl.clj | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/clojure_sql/dsl.clj b/src/clojure_sql/dsl.clj index 25ae6f8..0053cbb 100644 --- a/src/clojure_sql/dsl.clj +++ b/src/clojure_sql/dsl.clj @@ -260,8 +260,11 @@ (:sort right)))))))) (defn select - "Apply a filter to a query. The expression is an unevaluated expression - which is compiled by the clojure-sql compiler. + "Apply a filter to a query. The expression is an unevaluated + expression which is compiled by the clojure-sql + compiler. Alternatively a map can be provided instead of an + expression, in which case the keys and values will be compiled as an + equality test in the resulting query. Any keywords present in the query are interpreted as field names. A quote can be used to suppress evaluation of terms in the @@ -269,12 +272,18 @@ Example: (select query `(= :id 10)) - filter for an id of 10 - (select query `(in :id '(1 2 3)) - filter for an id of 1, 2 or 3" + (select query {:id 10}) - equivalent to the above + (select query `(in :id '(1 2 3)) - filter for an id of 1, 2 or 3 " [query expression] (let [table-name (if (= (count (:tables query)) 1) (-> query :tables first key)) old-where (:where query) - resolved-expression (process-expression table-name (:fields query) expression) + resolved-expression (->> (if (map? expression) + (->> (for [[key val] expression] + `(= ~key ~val)) + (reduce combine-conjunctions)) + expression) + (process-expression table-name (:fields query))) new-where (combine-conjunctions old-where resolved-expression)] (assoc query :where new-where))) -- cgit v1.2.3