-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
select.clj
159 lines (136 loc) · 5.24 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
(ns toucan2.select
(:refer-clojure :exclude [count])
(:require
[methodical.core :as m]
[toucan2.execute :as execute]
[toucan2.model :as model]
[toucan2.operation :as op]
[toucan2.query :as query]
[toucan2.realize :as realize]
[toucan2.util :as u]))
(m/defmethod query/build [::select :default clojure.lang.IPersistentMap]
[query-type model {:keys [columns], :as parsed-args}]
(let [parsed-args (-> parsed-args
(update :query (fn [query]
(merge {:select (or (not-empty columns)
[:*])}
(when model
{:from [[(keyword (model/table-name model))]]})
query)))
(dissoc :columns))]
(next-method query-type model parsed-args)))
(m/defmethod op/reducible-returning-instances* [::select :default]
[query-type model parsed-args]
(query/with-query [query [model (:queryable parsed-args)]]
(let [query (query/build query-type model (assoc parsed-args :query query))]
(execute/reducible-query (model/deferred-current-connectable model)
model
query))))
(defn reducible-select
{:arglists '([modelable & kv-args? query?]
[[modelable & columns] & kv-args? query?])}
[modelable-columns & unparsed-args]
(op/reducible-returning-instances ::select modelable-columns unparsed-args))
(defn select
{:arglists '([modelable & kv-args? query?]
[[modelable & columns] & kv-args? query?])}
[modelable-columns & unparsed-args]
(op/returning-instances! ::select modelable-columns unparsed-args))
(defn select-one {:arglists '([modelable & kv-args? query?]
[[modelable & columns] & kv-args? query?])}
[modelable-columns & unparsed-args]
(realize/reduce-first (apply reducible-select modelable-columns unparsed-args)))
(defn select-fn-reducible
{:arglists '([f modelable & kv-args? query?])}
[f & args]
(eduction
(map f)
(apply reducible-select 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."
{:arglists '([f modelable & kv-args? query?])}
[& args]
(not-empty (reduce conj #{} (apply select-fn-reducible 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."
{:arglists '([f modelable & kv-args? query?])}
[& args]
(not-empty (reduce conj [] (apply select-fn-reducible args))))
(defn select-one-fn
{:arglists '([f modelable & kv-args? query?])}
[& args]
(realize/reduce-first (apply select-fn-reducible args)))
(defn select-pks-reducible
{:arglists '([modelable & kv-args? query?])}
[modelable & args]
(let [f (model/select-pks-fn modelable)]
(apply select-fn-reducible f modelable args)))
(defn select-pks-set
{:arglists '([modelable & kv-args? query?])}
[& args]
(not-empty (reduce conj #{} (apply select-pks-reducible args))))
(defn select-pks-vec
{:arglists '([modelable & kv-args? query?])}
[& args]
(not-empty (reduce conj [] (apply select-pks-reducible args))))
(defn select-one-pk
{:arglists '([modelable & kv-args? query?])}
[& args]
(realize/reduce-first (apply select-pks-reducible args)))
(defn select-fn->fn
{:arglists '([f1 f2 modelable & kv-args? query?])}
[f1 f2 & args]
(not-empty
(into
{}
(map (juxt f1 f2))
(apply reducible-select args))))
(defn select-fn->pk
{:arglists '([f modelable & 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
{:arglists '([f modelable & kv-args? query?])}
[f modelable & args]
(let [pks-fn (model/select-pks-fn modelable)]
(apply select-fn->fn pks-fn f modelable args)))
;;; TODO -- [[count]] and [[exists?]] implementations seem kinda dumb, maybe we should just hand off to
;;; [[reducible-select]] by default so it can handle the parsing and stuff.
(m/defmulti count*
{:arglists '([model unparsed-args])}
u/dispatch-on-first-arg)
(m/defmethod count* :default
[model unparsed-args]
(u/println-debug ["No efficient implementation of count* for %s, doing reducible-select and counting the rows..." model])
(reduce
(fn [acc _]
(inc acc))
0
(apply reducible-select model unparsed-args)))
(defn count
{:arglists '([modelable & kv-args? query?])}
[modelable & unparsed-args]
(model/with-model [model modelable]
(count* model unparsed-args)))
(m/defmulti exists?*
{:arglists '([model unparsed-args])}
u/dispatch-on-first-arg)
(m/defmethod exists?* :default
[model unparsed-args]
(u/println-debug ["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 & kv-args? query?])}
[modelable & unparsed-args]
(model/with-model [model modelable]
(exists?* model unparsed-args)))