summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-23 02:13:53 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-23 02:13:53 +1000
commit9083622a4a41052492c693baa7429309f4628317 (patch)
tree9c2456d211f99fbb43336c176d63ef1fbf3f2810 /src
parent900d7f614bef4250046d829264ae0d9ad571494a (diff)
Make select take a map and compile to equality tests.
Diffstat (limited to 'src')
-rw-r--r--src/clojure_sql/dsl.clj17
1 files changed, 13 insertions, 4 deletions
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)))