hugsql

valerauko 2018-07-07T03:52:05.000046Z

@alice you can do it like

alice 2018-07-07T03:52:11.000047Z

Always happens, I think something's too niche for a channel and it exists

alice 2018-07-07T03:52:17.000022Z

Sorry for polluting beginners

valerauko 2018-07-07T03:52:32.000065Z

it's fine

valerauko 2018-07-07T03:52:52.000002Z

i'll show you how i did it and you decide if it's good for your use case

valerauko 2018-07-07T03:53:10.000033Z

-- :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 hugsql

alice 2018-07-07T03:53:58.000002Z

This 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 😞

valerauko 2018-07-07T03:54:58.000051Z

oh. well for an update statement you can write it like

UPDATE table
SET column1 = value1, column2 = value2, column3 = value3
WHERE condition

alice 2018-07-07T03:55:33.000061Z

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

valerauko 2018-07-07T03:56:31.000021Z

actually hugsql would throw an error about a missing key

alice 2018-07-07T03:57:13.000003Z

Hmm, well is there a way to get what I want?

valerauko 2018-07-07T03:58:49.000037Z

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

valerauko 2018-07-07T03:58:50.000018Z

correct?

alice 2018-07-07T03:59:10.000042Z

Correct

valerauko 2018-07-07T04:00:24.000019Z

i never did that myself with hugsql yet, but there is something promising in the docs

valerauko 2018-07-07T04:00:33.000035Z

you need to mix the clojure and the sql https://www.hugsql.org/#using-expressions

valerauko 2018-07-07T04:01:34.000027Z

i'm still trying to figure it out just a mo

alice 2018-07-07T04:01:46.000003Z

Thanks so much for taking the time

alice 2018-07-07T04:02:00.000060Z

Oh that's an interesting solution

valerauko 2018-07-07T04:04:36.000019Z

-- :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 work

valerauko 2018-07-07T04:06:18.000024Z

i don't exactly understand myself how parameters are passed around in hugsql so i just tweaked that example to include a colum whitelist

valerauko 2018-07-07T04:09:27.000055Z

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 in

alice 2018-07-07T04:10:00.000056Z

oh wow

alice 2018-07-07T04:10:05.000021Z

well thanks that's fairly elegant

alice 2018-07-07T04:10:15.000069Z

and a lot better than what I was doing lol, thanks so much

valerauko 2018-07-07T04:10:34.000064Z

✌️

alice 2018-07-07T04:10:47.000099Z

❤️