summaryrefslogtreecommitdiff
path: root/src/clojure_sql/dsl.clj
blob: 8e4a24d290653044609423b35c8bcdb929a571aa (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
(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 ^:private rename-table [query from to]
  (q/map->Query (walk/prewalk-replace {from to} (into {} query))))

(defn ^:private resolve-field [tables aliases field]
  (or (and aliases (aliases field))
      (if (= (count tables) 1)
        [(key (first tables)) field]
        (ambiguous-error field))))

(defn ^:private process-expression [tables aliases expression]
  (cond (vector? expression) (list 'quote (mapv (partial process-expression tables aliases) expression))
        (sequential? expression) (map (partial process-expression tables aliases) expression)
        (keyword? expression) (resolve-field tables aliases expression)
        :else expression))

(defn table
  "Create a query on a database table. If `table` is itself a query it
  will be wrapped, otherwise `table` will be used as the table name."
  [table]
  (q/map->Query (let [name (if (u/named? table) table "table")]
                  {:tables {name table}
                   :joins [name]})))

(defn ^:private into-map-duplicate-error [coll error-fn]
  (reduce (fn [acc [k v]]
            (if (contains? acc k)
              (error-fn k v)
              (assoc acc k v)))
          {} coll))

(defn ^:private rename-with-fn [query field-renames]
  (let [new-name #(or (field-renames %) %)
        new-fields (map (fn [[alias field]]
                          [(new-name alias) field])
                        (:fields query))
        error-fn (fn [k _] (same-rename-error k field-renames query))]
    (assoc query
      :fields (into-map-duplicate-error new-fields error-fn))))

(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 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]
  (if (map? fields)
    (-> (project query (keys fields))
        (rename-with-map fields))
    (let [get-real-name #(process-expression (:tables query) (:fields query) %)
          query (assoc query
                  :fields (->> fields
                               (map (juxt identity get-real-name))
                               (into {})))]
      query)))

(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 conjunction? [clause]
  (or (nil? clause)
      (and (sequential? clause)
           (= (name (first clause)) "and"))))

(defn ^:private clean-conjunction [clause]
  (if (conjunction? clause)
    (next clause)
    [clause]))

(defn ^:private combine-conjunctions [& wheres]
  (let [conjunction (reduce (fn [acc where]
                              `(and ~@(clean-conjunction acc)
                                    ~@(clean-conjunction where)))
                            nil wheres)]
    (case (count conjunction)
      1 nil
      2 (second conjunction)
      (seq conjunction))))

(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]
  (cond (joinable? query) query
        (:take query) (convert-to-subquery query)
        (:drop query) (convert-to-subquery query)
        :else (convert-to-subquery (remove-sort query))))

(defn ^:private rename-all-tables [query]
  (reduce (fn [query [alias table]]
            (rename-table query alias (keyword (gensym "table"))))
          query (:tables 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
  fields 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]}]
  (let [left (make-join-subquery left)
        right (rename-all-tables (make-join-subquery 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) (->> common-fields
                                       (map (fn [field] `(= ~(resolve-field (:tables left) (:fields left) field)
                                                            ~(resolve-field (:tables right) (:fields right) field))))
                                       (reduce combine-conjunctions)) 
                        (seq common-fields) (common-fields-error left right common-fields)
                        :else (process-expression merged-tables merged-fields on))
        type (or type (if join-condition :inner :cross))
        [join-condition where] (if (or (= type :outer) (= type :full-outer))
                                 [(combine-conjunctions (:where right) join-condition) (:where left)]
                                 [join-condition (combine-conjunctions (:where left) (:where right))])]
    (cond
     (not (valid-join-type? type)) (invalid-join-type type left right)
     (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))
     :else (assoc left
             :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.

  If the query being filtered is the result of a previous grouping
  operation then a `select` operation will compile in one of two
  different ways:
    1. If the selection is entirely made on grouped attributes it will
    compile into a `having` clause in the resulting query.
    2. If the selection includes non-grouped attributes then a
    subquery will be introduced.

  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 [expression (if (map? expression)
                     (reduce combine-conjunctions
                             (map #(cons '= %) expression))
                     expression)
        expression-fields (filter keyword? (flatten expression))
        bad-expression-fields (set/difference (set (process-expression nil
                                                                       (:fields query)
                                                                       expression-fields))
                                              (set (:group query)))
        where? (nil? (:group query))
        having? (empty? bad-expression-fields)]
    (if (or where? having?)
      (let [attr (if where? :where :having) 
            resolved-expression (process-expression (:tables query) (:fields query) expression)]
        (update-in query [attr] combine-conjunctions resolved-expression))
      (recur (convert-to-subquery query) expression))))

(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)
        fields-seq (if (sequential? fields)
                     fields
                     [fields])]
    (assoc query
      :sort (for [field fields-seq]
              (if (vector? field)
                [(process-expression (:tables query) (:fields query) (first field)) (second field)]
                [(process-expression (:tables query) (:fields query) 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)
        fields-seq (if (sequential? fields)
                     fields
                     [fields])]
    (assoc query :group (map (partial resolve-field (:tables query) (:fields query))
                             fields-seq))))

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