Fork me on GitHub
#honeysql
<
2023-08-06
>
kenny15:08:16

Morning 🙂 Is it possible to write the values + as clause in this pg query?

update users as u set -- postgres FTW
  email = u2.email,
  first_name = u2.first_name,
  last_name = u2.last_name
from (values
  (1, '', 'Hollis', 'Connell'),
  (2, '', 'Robert', 'Duncan')
) as u2(id, email, first_name, last_name)
where u2.id = u.id;

kenny15:08:34

I currently think I need to resort to :raw.

kenny15:08:04

e.g.,

(defn update-many-sql
  [ms]
  (let [ks (reduce (fn [acc m] (apply conj acc (keys m))) #{} ms)]
    {:from [[[{:values (map (fn [m] (map (fn [k] (m k)) ks)) ms)}] [:v [:raw "(" (str/join ", " (map name ks)) ")"]]]]}))

seancorfield16:08:18

Create a GitHub issue and I'll have a think about this.