hi guys, I don’t understand how hugsql is working in this case:
I have the following:
-- :name update-user! :>! :1
-- :doc updates an existing user record, sets id=id to make hugsql syntax work
UPDATE users SET
id = id
--~ (when (contains? params :name) ",name = :name")
--~ (when (contains? params :email) ",email = :email")
--~ (when (contains? params :image) ",image = :image")
--~ (when (contains? params :pass) ",pass = :pass")
--~ (when (contains? params :reset-token) ",reset_token = :reset-token")
WHERE id = :id
RETURNING name, email, image, organization_id, role, reset_token
And basically I’m doing the following:
(update-user! {:id "foobar" :name "foo" :email nil :image "kasdasfj"})
And somehow this works when there are the when clauses there but doesn’t when there aren’tyou're not passing an :id field
@lukaszkorecki just fixzed
ok, you're passing it now :-)
my question is (contains? {:foo nil} :foo) is true, so why is this working
in my own code I usually write the conditional part as --~ (when (:pass params) " pass = :pass") instead
and if a field needs to be nullfied then I'd add a dedicated query/function for that - I (and my team) found that relying on this behavior leads to weird bugs sometimes
lastly, you can use the -sqlvec stuff to see the final query that HugSQL generates
this thing:
hugsql/def-sqlvec-fns
are there docs for how to use this?
yep, this section: https://www.hugsql.org/#using-def-sqlvec-fns
(contains? {:foo nil} :foo) is true
but it works
as if in hugsql clojure it is false
yeah, I misread that section sorry!
I just grepped all our source code and we are using contains? in only one update statement, and according to tests it works as expected
so yeah, the sqlvec approach might shed some light