Fork me on GitHub
#sql
<
2019-10-20
>
murtaza5207:10:47

I want to build the following sql - ["INSERT INTO sample (name) VALUES (?) RETURNING id" "Joe"] Below is my honeysql -

(-> (helpers/insert-into :sample)
      (helpers/values [{:name name}])
      (sql/format))
I am not sure how to add the (sql/raw "RETURNING id) at the end of the query.

dharrigan07:10:26

It has extensions to support postgres stuff, like returning 🙂

dharrigan07:10:53

You can write your own, it's pretty straightforward, i.e.,

dharrigan07:10:56

(defmethod fmt/format-clause :on-conflict-do-nothing [[_ _] _]
  (str "ON CONFLICT DO NOTHING"))

(defhelper on-conflict-do-nothing [m _]
  (assoc m :on-conflict-do-nothing []))

dharrigan07:10:16

(insert-into :foo-bar)
(values (m/convert event))
(db/on-conflict-do-nothing)
sql/format))]

dharrigan07:10:00

Hope that helps! 🙂

murtaza5208:10:47

thanks @dharrigan this worked -

(-> (helpers/insert-into :passengers)
      (helpers/values [{:name name}])
      (psqlh/returning :id)
      (sql/format))

murtaza5213:10:15

a noob question, how do you destructure a map with namespaced key ? execute-one returns a hash-map with namespaced keys.

Vachi14:10:36

e.g, given this map {:product/id 1 :product/name "sample product" :product/reference "sample ref"}

{:product/keys [ id name reference ] }