Fork me on GitHub
#honeysql
<
2023-02-26
>
Daniel Shriki13:02:48

hi y’all 🙂 I want to use jdbc/execute-batch! for updating several records with batch. how can I use it with honeysql? honey.sql/format function is working only for 1 record and not batch. tnx!

p-himik13:02:18

A somewhat similar previous discussion: https://clojurians.slack.com/archives/C66EM8D5H/p1625797454111000 That doesn't exactly answer your question, but if you need that to insert multiple items then there's insert-multi!.

Daniel Shriki13:02:22

Unfortunately it’s not the use-case…

Daniel Shriki13:02:03

I need to update values in batch, so I’m searching something with this template:

Daniel Shriki13:02:22

(jdbc/execute-batch! ds
                        "UPDATE agent_event SET seen = TRUE WHERE (agent_id = ?) AND (event_id IN (?))"
                     [["id1" "eid1"] ["id2" "eid2"] ["id3 "eid3"]])
Only that I want to use honey.sql to build the sql statement instead of writing the statement itself

Daniel Shriki13:02:38

trying to figure out what will work on this case

p-himik14:02:25

Not sure whether it's the best approach but you can create a regular sqlmap where all the positional parameters are represented by [:raw "?"].

p-himik14:02:42

(honey.sql/format {:update :agent-event
                   :set    {:seen [:inline true]}
                   :where  [:and
                            [:= :agent-id [:raw "?"]]
                            [:in :event-id [[:raw "?"]]]]})
=> ["UPDATE agent_event SET seen = TRUE WHERE (agent_id = ?) AND (event_id IN (?))"]

💯 1
Daniel Shriki14:02:04

Thanks 🙏 I had [:raw "?"] missing 🙂

seancorfield18:02:23

Perhaps slightly less ugly:

user=> (honey.sql/format {:update :agent-event
  #_=>                    :set    {:seen [:inline true]}
  #_=>                    :where  [:and
  #_=>                             [:= :agent-id :'?]
  #_=>                             [:in :event-id [:nest :'?]]]})
["UPDATE agent_event SET seen = TRUE WHERE (agent_id = ?) AND (event_id IN (?))"]

👍 4
seancorfield18:02:46

:' allows for "literal" symbols