This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-09
Channels
- # announcements (1)
- # arachne (1)
- # beginners (34)
- # boot (5)
- # calva (68)
- # cider (34)
- # cljs-dev (1)
- # clojure (36)
- # clojure-italy (8)
- # clojure-spec (16)
- # clojure-uk (58)
- # clojurescript (29)
- # cursive (2)
- # datascript (9)
- # datomic (3)
- # emacs (10)
- # figwheel (1)
- # figwheel-main (11)
- # fulcro (33)
- # luminus (5)
- # mount (2)
- # nrepl (42)
- # off-topic (3)
- # other-languages (2)
- # parinfer (3)
- # perun (4)
- # prelude (3)
- # re-frame (6)
- # reagent (5)
- # shadow-cljs (23)
- # sql (37)
- # testing (1)
Using Sean’s et al. clojure.java.jdbc
, what is the best way to update multiple rows, and receive back number of updates (count) or better, all updated row ids ?
yet, if one update fails, catch it, and keep other entries updated
catch it
sorry
@leontalbot What do you mean by "update fails"? Throws an exception?
If you issue a single update SQL statement that should update multiple rows, and the database finds one or more row updates fail, that whole statement will fail and nothing will be updated. Because it's a single SQL statement.
That's nothing to do with libraries like clojure.java.jdbc
-- it happens lower down.
If you want to have some rows updated and some rows not, you'll need to issue multiple SQL statements.
ok, say you have this
(defn update-item!
[item-id item-attrs]
(try
(sql/update! db-spec
:items
item-attrs
["id = ?" id]
{:return-keys true})
(catch Exception e (.getMessage e))))
(reduce (fn [ids {id :id}]
(conj ids (:id (update-item! id {:color "white" :price 10}))))
[]
items)
Is that bad?
Define "bad".
1. not safe 2. not idiomatic
the where clause could be more complex in the example
this reduce would get me the updated item ids
You will get an exception trying to do (:id "Exception message")
oh, right
What do you mean by "not safe"?
I should print error message and just return nil)
a side effect in a reduce (?)
Is :id
the primary key of the table?
yes it is
Trying to figure out why you are returning keys from an update.
It doesn't look like :id
would be changed by the update? So the result will be the same :id
value you passed in, right?
But you want to know which updates succeeded and which failed and you're after the :id
for that, yes?
to tell the truth, sometimes i may not have the id, but the where clause will match one
so ideally i would return-keys and get the id from there
(or do a select prior to the update)
Ah, OK.
I would probably write that as (into [] (map #(update-item! % attributes)) items)
especially if you're likely to need a more complex pipeline of operations.
Then you'd have a vector of hash maps for success and strings for failures (I'd probably just return e
from the catch
BTW, rather than only the message, so callers have more information available to work with).
So a vector of hash maps or exception objects.
I think that's clearer these days than a reduce
with conj
onto []
-- even tho' it does the same under the hood.
that makes sense! thank you @seancorfield 🙂