summaryrefslogtreecommitdiff
path: root/src/clojure_sql/dsl.clj
blob: 10b1e8f3f19c16d0df95631625ab1b37856b33bc (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(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
;; ==============================================================

;; important sections:
;;   -PROJECTION-
;;   -TABLES-
;;   -FILTERS-
;;   -SORTING-
;;   -GROUPING-
;;   -GROUPED FILTERS-


;; {
;;  :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 [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 [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 [query field-renames]
  (if (map? field-renames)
    (rename-with-map query field-renames)
    (rename-with-fn query field-renames)))

(defn prefix-names-matching [pred prefix]
  (fn [alias]
    (if (pred alias)
      (keyword (str prefix (name alias)))
      alias)))

(defn prefix-names [prefix]
  (prefix-names-matching (constantly true) prefix))



(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))))

(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 [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))
          ;;_ (assert (empty? common-tables) "Cannot join two tables with the same name")
          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 [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)
        new-where (combine-conjunctions old-where resolved-expression)]
    (assoc query :where new-where)))

(defn sort [query fields]
  (let [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)
                [(resolve-field table-name fields-lookup (first field)) (second field)]
                [(resolve-field table-name fields-lookup field) :asc])))))

(defn group [query fields]
  (let [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 [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 [query n]
  (if-let [old-take (:take query)]
    (assoc query :take (min old-take n))
    (assoc query :take n)))

(defn drop [query n]
  (let [query (if-let [old-take (:take query)]
                (assoc query :take (- old-take n))
                query)]
    (if-let [old-drop (:drop query)]
      (assoc query :drop (+ old-drop n))
      (assoc query :drop n))))