Skip to content

Commit

Permalink
rename op/reducible => op/reducible-update to make its purpose clearer.
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Aug 24, 2022
1 parent 7325c98 commit c28ba41
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/toucan2/delete.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(defn reducible-delete
{:arglists '([modelable & conditions? query?])}
[modelable & unparsed-args]
(op/reducible ::delete modelable unparsed-args))
(op/reducible-update* ::delete modelable unparsed-args))

(defn delete!
"Returns number of rows deleted."
Expand Down
4 changes: 2 additions & 2 deletions src/toucan2/insert.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

;;;; [[reducible-insert]] and [[insert!]]

(m/defmethod op/reducible* [::insert :default]
(m/defmethod op/reducible-update* [::insert :default]
[query-type model {:keys [rows], :as parsed-args}]
(if (empty? rows)
(do
Expand All @@ -78,7 +78,7 @@
[modelable k v & more]
[modelable columns row-vectors])}
[modelable & unparsed-args]
(op/reducible ::insert modelable unparsed-args))
(op/reducible-update ::insert modelable unparsed-args))

(defn insert!
"Returns number of rows inserted."
Expand Down
20 changes: 11 additions & 9 deletions src/toucan2/operation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@

;;;; things that return update count

;;; TODO -- give this a name that makes it's clear for 'write' operations, not read ones.
(m/defmulti reducible*
(m/defmulti reducible-update*
"Return a reducible query based on `parsed-args`. This should return either an update count (number of rows updated) or
a sequence of PK values for affected rows; which of these gets returned depends on [[toucan2.jdbc.query/options]] (for
the JDBC backend) or similar."
the JDBC backend) or similar.
This is \"update\" in the sense that it is something that updates the database and returns an update count -- it could
mean a SQL `UPDATE`, `INSERT`, or `DELETE`."
{:arglists '([query-type model parsed-args])}
u/dispatch-on-first-two-args)

(m/defmethod reducible* :default
(m/defmethod reducible-update* :default
[query-type model parsed-args]
(query/with-resolved-query [query [model (:queryable parsed-args)]]
(let [built-query (query/build query-type model (-> parsed-args
(assoc :query query)
(dissoc :queryable)))]
(execute/reducible-query (model/deferred-current-connectable model) model built-query))))

(defn reducible [query-type modelable unparsed-args]
(defn reducible-update [query-type modelable unparsed-args]
(model/with-model [model modelable]
(let [parsed-args (query/parse-args query-type model unparsed-args)]
(reducible* query-type model parsed-args))))
(reducible-update* query-type model parsed-args))))

(defn returning-update-count! [query-type modelable unparsed-args]
(u/with-debug-result ["%s %s returning update count" query-type modelable]
(reduce (fnil + 0 0) 0 (reducible query-type modelable unparsed-args))))
(reduce (fnil + 0 0) 0 (reducible-update query-type modelable unparsed-args))))

;;;; things that return PKs

Expand All @@ -61,13 +63,13 @@
column, or a vector of primary key values in the same order as [[toucan2.model/primary-keys]] for models with
composite primary keys.
The default implementation combines [[reducible*]] with [[return-pks-eduction]]."
The default implementation combines [[reducible-update*]] with [[return-pks-eduction]]."
{:arglists '([query-type model parsed-args])}
u/dispatch-on-first-two-args)

(m/defmethod reducible-returning-pks* :default
[query-type model parsed-args]
(return-pks-eduction model (reducible* query-type model parsed-args)))
(return-pks-eduction model (reducible-update* query-type model parsed-args)))

(defn reducible-returning-pks
"Helper wrapper for [[reducible-returning-pks*]] that also resolves `modelable` and parses `unparsed-args`."
Expand Down
2 changes: 1 addition & 1 deletion src/toucan2/tools/after.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

;;;; reducible update count

(m/defmethod op/reducible* :around [::after ::after]
(m/defmethod op/reducible-update* :around [::after ::after]
[query-type model parsed-args]
(cond
(::doing-after? parsed-args)
Expand Down
4 changes: 2 additions & 2 deletions src/toucan2/tools/before_insert.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
e)))))
rows))

(m/defmethod op/reducible* :before [::insert/insert ::before-insert]
(m/defmethod op/reducible-update* :before [::insert/insert ::before-insert]
[_query-type model parsed-args]
(assert (map? parsed-args))
(u/with-debug-result ["Do before insert for %s" model]
Expand All @@ -33,7 +33,7 @@
;;;
;;; By marking `::before-insert` as preferred over `:toucan2.tools.transformed/transformed` it will be done first (see
;;; https://github.com/camsaul/methodical#before-methods)
(m/prefer-method! #'op/reducible*
(m/prefer-method! #'op/reducible-update*
[::insert/insert ::before-insert]
[::insert/insert :toucan2.tools.transformed/transformed])

Expand Down
2 changes: 1 addition & 1 deletion src/toucan2/tools/before_update.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
pk-map pk-maps]
(assoc parsed-args :changes changes, :kv-args pk-map))))))

(m/defmethod op/reducible* :around [::update/update ::before-update]
(m/defmethod op/reducible-update* :around [::update/update ::before-update]
[query-type model {::keys [doing-before-update?], :keys [changes], :as parsed-args}]
(cond
doing-before-update?
Expand Down
4 changes: 4 additions & 0 deletions src/toucan2/tools/compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@
;; `(binding [query/*with-resolved-query-fn* identity-with-resolved-query]
;; (build
;; ~@body)))

;; (defmacro parsed-args
;; [& body]
;; )
6 changes: 3 additions & 3 deletions src/toucan2/tools/disallow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
[_query-type model _parsed-args]
(throw (UnsupportedOperationException. (format "You cannot select %s." model))))

(m/defmethod op/reducible* [:toucan2.delete/delete ::delete]
(m/defmethod op/reducible-update* [:toucan2.delete/delete ::delete]
[_query-type model _parsed-args]
(throw (UnsupportedOperationException. (format "You cannot delete instances of %s." model))))

(m/defmethod op/reducible* [:toucan2.insert/insert ::insert]
(m/defmethod op/reducible-update* [:toucan2.insert/insert ::insert]
[_query-type model _parsed-args]
(throw (UnsupportedOperationException. (format "You cannot create new instances of %s." model))))

(m/defmethod op/reducible* [:toucan2.update/update ::update]
(m/defmethod op/reducible-update* [:toucan2.update/update ::update]
[_query-type model _parsed-args]
(throw (UnsupportedOperationException. (format "You cannot update a %s after it has been created." model))))
2 changes: 1 addition & 1 deletion src/toucan2/tools/helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
(pretty [_this]
(list `->ReducibleBeforeDelete model parsed-args reducible-delete)))

(m/defmethod op/reducible* :around [::delete/delete ::before-delete]
(m/defmethod op/reducible-update* :around [::delete/delete ::before-delete]
[query-type model parsed-args]
(let [reducible-delete (next-method query-type model parsed-args)]
(->ReducibleBeforeDelete model parsed-args reducible-delete)))
Expand Down
2 changes: 1 addition & 1 deletion src/toucan2/tools/transformed.clj
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
row-xform (apply comp row-xforms)]
(map row-xform rows)))

(m/defmethod op/reducible* :before [::insert/insert ::transformed]
(m/defmethod op/reducible-update* :before [::insert/insert ::transformed]
[_query-type model parsed-args]
(assert (isa? model ::transformed))
(if-let [transforms (in-transforms model)]
Expand Down
4 changes: 2 additions & 2 deletions src/toucan2/update.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:set changes})]
(next-method query-type model parsed-args)))

(m/defmethod op/reducible* [::update :default]
(m/defmethod op/reducible-update* [::update :default]
[query-type model {:keys [changes], :as parsed-args}]
(if (empty? changes)
(do
Expand All @@ -56,7 +56,7 @@
(defn reducible-update
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
[modelable & unparsed-args]
(op/reducible* ::update modelable unparsed-args))
(op/reducible-update* ::update modelable unparsed-args))

(defn update!
{:arglists '([modelable pk? conditions-map-or-query? & conditions-kv-args changes-map])}
Expand Down
5 changes: 5 additions & 0 deletions test/toucan2/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns toucan2.core-test
"This is just a dummy namespace to make sure [[toucan2.core]] loads while we're running tests."
(:require [toucan2.core :as core]))

(comment core/keep-me)

0 comments on commit c28ba41

Please sign in to comment.