(ns clojure-sql.dsl (:refer-clojure :exclude [sort group take drop]) (:require [clojure.set :as set] [clojure.walk :as walk] [clojure-sql.query :as q] [clojure-sql.util :as u])) ;; ============================================================== ;; The DSL for making query maps ;; ============================================================== ;; { ;; :tables => tablename -> table_alias ;; :fields => (table_alias, fieldname) -> field_alias ;; :joins => [left-table type right-table on?] ;; :where => expression ;; :sort => [(field, direction)] ;; :group => [field] ;; :having => expression ;; } (defn ^:private ambiguous-error [field & [query]] (throw (ex-info (str "Ambiguous field " field " in query with more than one table") {:field field :query query}))) (defn ^:private same-rename-error [alias renames & [query]] (throw (ex-info (str "Cannot rename multiple fields to the same alias: " alias) {:alias alias :renames renames :query query}))) (defn ^:private missing-rename-error [field renames & [query]] (throw (ex-info (str "Cannot rename field not present in query: " field) {:field field :query query :renames renames}))) (defn ^:private common-fields-error [left right common-fields] (throw (ex-info "Cannot join with common fields unless natural join" {:left left :right right :common-fields common-fields}))) (defn ^:private cross-join-duplicate-fields [common-fields left right] (throw (ex-info "Cross join will introduce ambiguous duplicate fields" {:left left :right right :common-fields common-fields}))) (defn ^:private cross-join-condition-error [condition left right] (throw (ex-info "Cannot have a cross join with condition (maybe you wanted either a selection or an inner join)" {:left left :right right :condition condition}))) (defn ^:private invalid-join-type [type left right] (throw (ex-info (str "Invalid join type: " type) {:left left :right right :type type }))) (defn ^:private invalid-union [queries] (throw (ex-info "Cannot union queries with different fields" {:queries queries}))) (defn table "Create a query on a database table. If `arg` is itself a query it will be wrapped, otherwise `arg` will be used as the table name." [arg] (q/map->Query (let [alias (keyword (gensym (if (u/named? arg) (name arg) "table")))] {:tables {alias arg} :joins [alias]}))) (defn ^:private rename-table [query from to] (q/map->Query (walk/prewalk-replace {from to} (into {} query)))) (defn ^:private resolve-field [table aliases field] (let [field-alias-lookup aliases] (or (and field-alias-lookup (field-alias-lookup field)) (if table [table field] (ambiguous-error field))))) (defn ^:private process-expression [table aliases expression] (cond (vector? expression) (list 'quote (mapv (partial process-expression table aliases) expression)) (sequential? expression) (map (partial process-expression table aliases) expression) (keyword? expression) (resolve-field table aliases expression) :else expression)) (defn project "Limit a query's fields to a specified set of fields. `fields` can be either a seq of allowed field names, or a map of field names to aliases. If the query is currently on a single table then any unknown fields will be resolved as columns in that table. If the query is on more than one table then an exception will be thrown for unknown fields." [query fields] (let [table (if (= (count (:tables query)) 1) (-> query :tables first key)) alias-lookup (or (:fields query) {}) get-real-name #(process-expression table alias-lookup %) fields (if (map? fields) fields (zipmap fields fields))] (assoc query :fields (reduce (fn [fields [field alias]] (if (contains? fields alias) (same-rename-error alias fields query) (assoc fields alias (get-real-name field)))) {} fields)))) (defn ^:private rename-with-fn [query field-renames] (update-in query [:fields] (fn [fields] (reduce (fn [fields [old-alias field]] (let [new-alias (or (field-renames old-alias) old-alias)] (if (contains? fields new-alias) (same-rename-error new-alias field-renames query) (assoc fields new-alias field)))) {} fields)))) (defn ^:private rename-with-map [query field-renames] (doseq [[field alias] field-renames] (if-not (contains? (:fields query) field) (missing-rename-error field field-renames query))) (rename-with-fn query field-renames)) (defn rename "Rename fields in a query. All fields must already exist prior to calling rename (no automatic creation like `project`). `field-renames` can be either a function or a map. If a function is provided then it will be applied to each field: the return value will be used as the new alias, with a return of nil indicating no change. If a map is provided then it will behave identically to a function, but will throw errors if you are attempting to rename a non-existent field." [query field-renames] (if (map? field-renames) (rename-with-map query field-renames) (rename-with-fn query field-renames))) (defn prefix-names-matching "Higher-order helper function to use with `rename`." [pred prefix] (fn [alias] (if (pred alias) (keyword (str prefix (name alias))) alias))) (defn prefix-names "Higher-order helper function to use with `rename`." [prefix] (prefix-names-matching (constantly true) prefix)) (defn as-subobject "Prefix all field names with \"{key}.\" to use them as sub-objects in the final result. Can also be useful to disambiguate fields for joins in a regular and predictable manner." [key] (prefix-names (str (name key) \.))) (defn ^:private combine-conjunctions [& wheres] (reduce (fn [acc where] (cond (nil? acc) where (nil? where) acc :else (or (if (and (sequential? where) (= (name (first where)) "and")) `(and ~acc ~@(next where))) (if (and (sequential? acc) (= (name (first acc)) "and")) `(and ~@(next acc) ~where)) `(and ~acc ~where)))) nil wheres)) (defn ^:private joinable? [query] (and (nil? (:group query)) (nil? (:having query)) (nil? (:take query)) (nil? (:drop query)) (nil? (:set-operation query)))) (defn ^:private convert-to-subquery [query] (-> (table query) (project (keys (:fields query))))) (defn ^:private remove-sort [query] (dissoc query :sort)) (defn ^:private make-join-subquery [query] (if (joinable? query) query (if (or (:take query) (:drop query)) (convert-to-subquery query) (convert-to-subquery (remove-sort query))))) (def ^:private valid-join-type? (comp boolean #{:cross :inner :outer :full-outer})) (defn join "Join two queries into one query. The fields of the resultant query will be the union of the argument queries. If `type` is not provided then the join type will be automatically set: if the arguments have any fields in common, or if an `on` is provided, then an inner join will be performed (joining on common attributes if no `on` is provided), otherwise a cross join will be performed. Valid join types are :cross, :inner, :outer and :full-outer. An outer join is considered a LEFT outer join. To achieve a right outer join reverse the query arguments." [left right & {:keys [on type]}] (if (= type :right) (join right left :on on :type :left) (let [type (if (= type :left) :outer type) left (make-join-subquery left) right (make-join-subquery right) common-tables (set/intersection (set (keys (:tables left))) (set (keys (:tables right)))) right (reduce (fn [query [alias table]] (rename-table query alias (keyword (gensym (name table))))) right (:tables right)) merged-tables (merge (:tables left) (:tables right)) common-fields (set/intersection (set (keys (:fields left))) (set (keys (:fields right)))) merged-fields (merge (:fields right) (:fields left)) ;; favour the left name for outer joins join-condition (cond (nil? on) (let [implicit (map (fn [field] `(~'= ~(resolve-field (:table left) (or (:fields left) {}) field) ~(resolve-field (:table right) (or (:fields right) {}) field))) common-fields)] (if (next implicit) (seq (cons `and implicit)) ;; more than one, so add an "and" around them (first implicit))) (seq common-fields) (common-fields-error left right common-fields) :else (process-expression nil merged-fields on)) type (or type (if join-condition :inner) :cross) [join-condition where] (if (contains? #{:outer :full-outer} type) [(combine-conjunctions (:where right) join-condition) (:where left)] [join-condition (combine-conjunctions (:where left) (:where right))])] (if-not (valid-join-type? type) (throw (invalid-join-type type left right))) (if (and (= type :cross) join-condition) (if (seq common-fields) (cross-join-duplicate-fields common-fields left right) (cross-join-condition-error join-condition left right))) (-> left (assoc :fields merged-fields :tables merged-tables :joins {:left (:joins left) :right (:joins right) :type type :on join-condition} :where where :sort (seq (concat (:sort left) (:sort right)))))))) (defn select "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 expression. Example: (select query `(= :id 10)) - filter for an id of 10 (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 (->> (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))) (defn sort "Apply a sort to a query. Replaces any existing sort on the query (ie. is not stable) . `fields` is a sequential collection of fields to sort by. Each element of fields can be either a field name, :field, or a vector of field and direction, [:field :desc]. If a `take` or `drop` has already been applied to this query then the sort will be applied *after* the `take`/`drop` (which results in a subquery being created)." [query fields] (let [query (if (or (:take query) (:drop query)) (convert-to-subquery query) query) table-name (if (= (count (:tables query)) 1) (-> query :tables first key)) fields-seq (if (sequential? fields) fields [fields]) fields-lookup (or (:fields query) {})] (assoc query :sort (for [field fields-seq] (if (vector? field) [(process-expression table-name fields-lookup (first field)) (second field)] [(process-expression table-name fields-lookup field) :asc]))))) (defn group "Apply a grouping to a query. `fields` is a sequential collection of fields to group by. If the query has already been grouped then this will create a subquery." [query fields] (let [query (if (:group query) (convert-to-subquery query) query) table-name (if (= (count (:tables query)) 1) (-> query :tables first key)) fields-seq (if (sequential? fields) fields [fields]) fields-lookup (or (:fields query) {})] (assoc query :group (map (partial resolve-field table-name fields-lookup) fields-seq)))) (defn having "Apply a filter to the groupings of a query. `expression` is the same as a `select`, but may only reference fields by which the query is grouped (see `group`) or other fields within aggregating functions (eg. count). Example: (having query `(< (sum :age) 100)) - select groups with a combined age under 100" [query expression] (let [table-name (if (= (count (:tables query)) 1) (-> query :tables first key)) old-having (:having query) resolved-expression (process-expression table-name (:fields query) expression) new-having (combine-conjunctions old-having resolved-expression)] (assoc query :having new-having))) (defn take "Limit the number of results of a query. Note: take/drop will function as they do on clojure sequences. They will not simply overwrite the previous take/drop value. Example: (-> query (take 10) (drop 2)) = (-> query (drop 2) (take 8))" [query n] (if-let [old-take (:take query)] (assoc query :take (min old-take n)) (assoc query :take n))) (defn drop "Exclude the first `n` results of a query. Note: take/drop will function as they do on clojure sequences. They will not simply overwrite the previous take/drop value. Example: (-> query (take 10) (drop 2)) = (-> query (drop 2) (take 8))" [query n] (let [query (if-let [old-take (:take query)] (assoc query :take (max (- old-take n) 0)) query)] (if-let [old-drop (:drop query)] (assoc query :drop (+ old-drop n)) (assoc query :drop n)))) (defn ^:private union-compatible? [& queries] (and (every? (comp seq keys :fields) queries) (apply = (map (comp set keys :fields) queries)))) (defn union "Combine the results of two queries" [& queries] {:pre [(apply union-compatible? queries)]} (assoc (q/->Query) :set-operation :union :queries queries :fields (zipmap (keys (:fields (first queries))) (repeat nil)))) (defn intersection "Take the common rows in two queries" [& queries] {:pre [(apply union-compatible? queries)]} (assoc (q/->Query) :set-operation :intersect :queries queries :fields (zipmap (keys (:fields (first queries))) (repeat nil))))