Fork me on GitHub
#honeysql
<
2021-08-05
>
greglook18:08:28

I feel like I’m missing something obvious; how would I format an = clause inside of a function call? The approach I expected to work doesn’t do the right thing:

=> (sql/format (sql/call :nullif [:= :id "dsr-000"] false))
["nullif((=, id, ?), FALSE)" "dsr-000"]

seancorfield18:08:56

@greg316 HoneySQL v1 or v2?

greglook18:08:21

good question, looks like [honeysql "1.0.461"]

seancorfield18:08:46

On v2:

dev=> (sql/format-expr (sql/call :nullif [:= :id "dsr-000"] false))
["NULLIF(id = ?, FALSE)" "dsr-000"]
(sorry, didn't paste the right thing)

seancorfield18:08:36

(I'm not sure if you can format expressions in v1 -- you can't in v2, you must use format-expr -- but if you format a statement that includes your expression you should do better)

greglook19:08:19

has the same effect in a full expression:

=> (sql/format (sql/build :update :foos :set {:is_default (sql/call :nullif [:= :id "dsr-000"] false)}))
["UPDATE foos SET is_default = nullif((=, id, ?), FALSE)" "dsr-000"]

seancorfield19:08:08

v1 had some strange behavior in some places with embedded expressions...

greglook19:08:16

hoping I don’t have to fall back to making it a raw string 😅

seancorfield19:08:08

v2: (there's no build function because it really didn't do much)

dev=> (sql/format {:update :foos :set {:is_default (sql/call :nullif [:= :id "dsr-000"] false)}})
["UPDATE foos SET is_default = NULLIF(id = ?, FALSE)" "dsr-000"]

greglook19:08:13

unfortunately I don’t think upgrading the whole app to v2 is tractable right now, but I’ll put it on our backlog - does look like a nicer API

seancorfield19:08:05

You can run v1 and v2 side-by-side and migrate on a query-by-query basis.

seancorfield19:08:14

Different library names and different namespaces.

seancorfield19:08:28

We're running both at work and slowly migrating from v1 to v2.