-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
select.clj
266 lines (226 loc) · 10.4 KB
/
select.clj
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
(ns toucan2.select
"Implementation of [[select]] and variations.
The args spec used by [[select]] lives in [[toucan2.query]], specifically `:toucan2.query/default-args`.
Code for building Honey SQL for a SELECT lives in [[toucan2.map-backend.honeysql2]].
### Functions that return primary keys
Functions that return primary keys such as [[select-pks-set]] determine which primary keys to return by
calling [[toucan2.model/select-pks-fn]], which is based on the model's implementation
of [[toucan2.model/primary-keys]]. Models with just a single primary key column will return primary keys 'unwrapped',
i.e., the values of that column will be returned directly. Models with compound primary keys (i.e., primary keys
consisting of more than one column) will be returned in vectors as if by calling `juxt`.
```clj
;; A model with a one-column primary key, :id
(t2/select-pks-vec :models/venues :category \"bar\")
;; => [1 2]
;; A model with a compound primary key, [:id :name]
(t2/select-pks-vec :models/venues.compound-key :category \"bar\")
;; => [[1 \"Tempest\"] [2 \"Ho's Tavern\"]]
```"
(:refer-clojure :exclude [count])
(:require
[clojure.spec.alpha :as s]
[methodical.core :as m]
[toucan2.log :as log]
[toucan2.model :as model]
[toucan2.pipeline :as pipeline]
[toucan2.types :as types]
[toucan2.util :as u]))
(comment s/keep-me
types/keep-me)
(defn reducible-select
"Like [[select]], but returns an `IReduceInit`."
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[& unparsed-args]
(pipeline/reducible-unparsed :toucan.query-type/select.instances unparsed-args))
(defn select
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[& unparsed-args]
(pipeline/transduce-unparsed-with-default-rf :toucan.query-type/select.instances unparsed-args))
(defn select-one
"Like [[select]], but only fetches a single row, and returns only that row."
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[& unparsed-args]
(let [query-type :toucan.query-type/select.instances
rf (pipeline/default-rf query-type)
xform (pipeline/first-result-xform-fn query-type)]
(pipeline/transduce-unparsed (xform rf) query-type unparsed-args)))
(defn select-fn-reducible
"Like [[reducible-select]], but returns a reducible sequence of results of `(f row)`."
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(eduction
(map f)
(pipeline/reducible-unparsed :toucan.query-type/select.instances.fns unparsed-args)))
(defn select-fn-set
"Like [[select]], but returns a *set* of values of `(f instance)` for the results. Returns `nil` if the set is empty.
```clj
(t2/select-fn-set (comp str/upper-case :category) :models/venues :category \"bar\")
;; =>
#{\"BAR\"}
```"
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(let [rf (pipeline/with-init conj #{})
xform (map f)]
(not-empty (pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances.fns unparsed-args))))
(defn select-fn-vec
"Like [[select]], but returns a *vector* of values of `(f instance)` for the results. Returns `nil` if the vector is
empty.
```clj
(t2/select-fn-vec (comp str/upper-case :category) :models/venues :category \"bar\")
;; =>
[\"BAR\" \"BAR\"]
```
NOTE: If your query does not specify an `:order-by` clause (or equivalent), the results are like indeterminate. Keep
this in mind!"
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(let [rf (pipeline/with-init conj [])
xform (map f)]
(not-empty (pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances.fns unparsed-args))))
(defn select-one-fn
"Like [[select-one]], but applies `f` to the result.
```clj
(t2/select-one-fn :id :models/people :name \"Cam\")
;; => 1
```"
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(let [query-type :toucan.query-type/select.instances.fns
rf (pipeline/with-init conj [])
xform (comp (map f)
(pipeline/first-result-xform-fn query-type))]
(pipeline/transduce-unparsed (xform rf) query-type unparsed-args)))
(defn select-pks-reducible
"Returns a reducible sequence of all primary keys
```clj
(into [] (t2/select-pks-reducible :models/venues :category \"bar\"))
;; => [1 2]
```"
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(apply select-fn-reducible (model/select-pks-fn modelable) modelable unparsed-args))
(defn select-pks-set
"Returns a *set* of all primary keys (as determined by [[toucan2.model/primary-keys]]
and [[toucan2.model/select-pks-fn]]) of instances matching the query. Models with just a single primary key columns
will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more
than one column) will be returned in vectors as if by calling `juxt`.
```clj
(t2/select-pks-set :models/venues :category \"bar\")
;; => #{1 2}
```"
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(apply select-fn-set (model/select-pks-fn modelable) modelable unparsed-args))
(defn select-pks-vec
"Returns a *vector* of all primary keys (as determined by [[toucan2.model/primary-keys]]
and [[toucan2.model/select-pks-fn]]) of instances matching the query. Models with just a single primary key columns
will be 'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more
than one column) will be returned in vectors as if by calling `juxt`.
```clj
(t2/select-pks-vec :models/venues :category \"bar\")
;; => [1 2]
```
NOTE: If your query does not specify an `:order-by` clause (or equivalent), the results are like indeterminate. Keep
this in mind!"
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(apply select-fn-vec (model/select-pks-fn modelable) modelable unparsed-args))
(defn select-one-pk
"Return the primary key of the first row matching the query. Models with just a single primary key columns will be
'unwrapped' (i.e., the values of that column will be returned); models with compound primary keys (i.e., more than one
column) will be returned in vectors as if by calling `juxt`.
```clj
(t2/select-one-pk :models/people :name \"Cam\")
;; => 1
```"
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(apply select-one-fn (model/select-pks-fn modelable) modelable unparsed-args))
(defn select-fn->fn
"Return a map of `(f1 instance)` -> `(f2 instance)` for instances matching the query.
```clj
(t2/select-fn->fn :id (comp str/upper-case :name) :models/people)
;; => {1 \"CAM\", 2 \"SAM\", 3 \"PAM\", 4 \"TAM\"}
```"
{:arglists '([f1 f2 modelable-columns & kv-args? query?]
[f1 f2 :conn connectable modelable-columns & kv-args? query?])}
[f1 f2 & unparsed-args]
(let [rf (pipeline/with-init conj {})
xform (map (juxt f1 f2))]
(pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances unparsed-args)))
(defn select-fn->pk
"The inverse of [[select-pk->fn]]. Return a map of `(f instance)` -> *primary key* for instances matching the query.
```clj
(t2/select-fn->pk (comp str/upper-case :name) :models/people)
;; => {\"CAM\" 1, \"SAM\" 2, \"PAM\" 3, \"TAM\" 4}
```"
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f modelable & args]
(let [pks-fn (model/select-pks-fn modelable)]
(apply select-fn->fn f pks-fn modelable args)))
(defn select-pk->fn
"The inverse of [[select-fn->pk]]. Return a map of *primary key* -> `(f instance)` for instances matching the query.
```clj
(t2/select-pk->fn (comp str/upper-case :name) :models/people)
;; => {1 \"CAM\", 2 \"SAM\", 3 \"PAM\", 4 \"TAM\"}
```"
{:arglists '([f modelable-columns & kv-args? query?]
[f :conn connectable modelable-columns & kv-args? query?])}
[f modelable & args]
(let [pks-fn (model/select-pks-fn modelable)]
(apply select-fn->fn pks-fn f modelable args)))
(m/defmulti count*
{:arglists '([model₁ unparsed-args])
:defmethod-arities #{2}
:dispatch-value-spec (s/nonconforming ::types/dispatch-value.model)}
u/dispatch-on-first-arg)
;;; TODO -- the default implementation is evil, we need to do something better.
(m/defmethod count* :default
[model unparsed-args]
(log/debugf :compile "No efficient implementation of count* for %s, doing reducible-select and counting the
instances..." model)
(reduce
(fn [acc _]
(inc acc))
0
(apply reducible-select model unparsed-args)))
(defn count
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(let [model (model/resolve-model modelable)]
(count* model unparsed-args)))
(m/defmulti exists?*
{:arglists '([model₁ unparsed-args]), :defmethod-arities #{2}}
u/dispatch-on-first-arg)
(m/defmethod exists?* :default
[model unparsed-args]
(log/debugf :compile "No efficient implementation of exists?* for %s, doing reducible-select and seeing if it returns a row..." model)
(transduce
(take 1)
(fn
([acc]
acc)
([_ _]
true))
false
(apply reducible-select model unparsed-args)))
(defn exists?
{:arglists '([modelable-columns & kv-args? query?]
[:conn connectable modelable-columns & kv-args? query?])}
[modelable & unparsed-args]
(let [model (model/resolve-model modelable)]
(exists?* model unparsed-args)))