@alice you can do it like
Always happens, I think something's too niche for a channel and it exists
Sorry for polluting beginners
it's fine
i'll show you how i did it and you decide if it's good for your use case
-- :name use-authz! :<! :1
update oauth_auths
set used = true
where
(not used) and
client_id = :client-id and
auth_code = :auth-code and
expires_at > now()
returning user_id, app_id, scopes
this is the hugsqlThis is my first time using sql for a project only I am working on, all the other times in the past I had others help me with that, so I'm not sure I really know what that means 😞
oh. well for an update statement you can write it like
UPDATE table
SET column1 = value1, column2 = value2, column3 = value3
WHERE condition
The ideal function I want is one that will take a user map and update all the existing fields in the map, but I think doing it like that would lead to nils if the values weren't supplied right
actually hugsql would throw an error about a missing key
Hmm, well is there a way to get what I want?
so it's like, you have a white list of updatable columns, and if any of those are present in the hashmap you want them updated and untouched otherwise
correct?
Correct
i never did that myself with hugsql yet, but there is something promising in the docs
you need to mix the clojure and the sql https://www.hugsql.org/#using-expressions
i'm still trying to figure it out just a mo
Thanks so much for taking the time
Oh that's an interesting solution
-- :name alice-update :! :1
/* :require [clojure.string :as string]
[your-namespace :refer [column-whitelist]]
[hugsql.parameters :refer [identifier-param-quote]] */
update :i:table set
/*~
(string/join ","
(for [[field _] (select-keys (:updates params) column-whitelist)]
(str (identifier-param-quote (name field) options)
" = :v:updates." (name field))))
~*/
where id = :id
i haven't tested it but something like this might worki don't exactly understand myself how parameters are passed around in hugsql so i just tweaked that example to include a colum whitelist
maybe this way:
-- :name alice-update :! :1
/* :require [clojure.string :as string]
[your-namespace :refer [column-whitelist]]
[hugsql.parameters :refer [identifier-param-quote]] */
update your-table set
/*~
(string/join ","
(for [[field _] (select-keys params column-whitelist)]
(str (identifier-param-quote (name field) options)
" = :v:" (name field))))
~*/
where id = :id
you can then call it
(def column-whitelist [:field_1 :field_2])
(update-thing {:id 42 :field_1 "value" :field_2 "value" :invalid_field "value"})
and that should ignore the :invalid_field
setting even if it's passed inoh wow
well thanks that's fairly elegant
and a lot better than what I was doing lol, thanks so much
✌️
❤️