-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
update.clj
65 lines (56 loc) · 2.87 KB
/
update.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
(ns toucan2.update
"Implementation of [[update!]]."
(:require
[clojure.spec.alpha :as s]
[methodical.core :as m]
[toucan2.log :as log]
[toucan2.pipeline :as pipeline]
[toucan2.query :as query]))
;;; this is basically the same as the args for `select` and `delete` but the difference is that it has an additional
;;; optional arg, `:pk`, as the second arg, and one additional optional arg, the `changes` map at the end
(s/def ::args
(s/cat
:connectable ::query/default-args.connectable
:modelable ::query/default-args.modelable
:pk (s/? (complement (some-fn keyword? map?)))
;; these are treated as CONDITIONS
:kv-args ::query/default-args.kv-args
;; by default, assuming this resolves to a map query, is treated as a map of conditions.
:queryable ::query/default-args.queryable
;; TODO -- This should support named changes maps too. Also,
:changes map?))
(m/defmethod query/parse-args :toucan.query-type/update.*
[query-type unparsed-args]
(let [parsed (query/parse-args-with-spec query-type ::args unparsed-args)]
(cond-> parsed
(contains? parsed :pk) (-> (dissoc :pk)
(update :kv-args assoc :toucan/pk (:pk parsed))))))
(m/defmethod pipeline/build [#_query-type :toucan.query-type/update.*
#_model :default
#_query :default]
"Default method for building UPDATE queries. Code for building Honey SQL for UPDATE lives
in [[toucan2.map-backend.honeysql2]].
This doesn't really do much, but if the query has no `:changes`, returns the special flag `:toucan2.pipeline/no-op`."
[query-type model {:keys [changes], :as parsed-args} resolved-query]
(if (empty? changes)
(do
(log/debugf :compile "Query has no changes, skipping update")
::pipeline/no-op)
(next-method query-type model parsed-args resolved-query)))
(defn reducible-update
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
[& unparsed-args]
(pipeline/reducible-unparsed :toucan.query-type/update.update-count unparsed-args))
(defn update!
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
[& unparsed-args]
(pipeline/transduce-unparsed-with-default-rf :toucan.query-type/update.update-count unparsed-args))
(defn reducible-update-returning-pks
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
[& unparsed-args]
(pipeline/reducible-unparsed :toucan.query-type/update.pks unparsed-args))
(defn update-returning-pks!
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
[& unparsed-args]
(pipeline/transduce-unparsed-with-default-rf :toucan.query-type/update.pks unparsed-args))
;;; TODO -- add `update-returning-instances!`, similar to [[toucan2.update/insert-returning-instances!]]