honeysql

kenny 2023-08-06T15:16:16.156209Z

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;

kenny 2023-08-06T15:16:34.940039Z

I currently think I need to resort to :raw.

kenny 2023-08-06T15:17:04.915029Z

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)) ")"]]]]}))

seancorfield 2023-08-06T16:39:18.745539Z

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

kenny 2023-08-06T17:08:03.134039Z

Sure. https://github.com/seancorfield/honeysql/issues/498