diff options
Diffstat (limited to 'src/clojure_sql')
-rw-r--r-- | src/clojure_sql/dsl.clj | 17 |
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))) |