Fork me on GitHub
#honeysql
<
2022-11-26
>
fabrao00:11:31

Hello all, I have a situation that I want to select the update operation with case. How can I do this in honeysql?

:update :table :set {:source [:case [:> source destination] (sql/call :+ :sort-order 1) :else (sql/call :+ :sort-order 1)]} :where ...
how can I use the case in this case?

seancorfield00:11:13

Sorry, I don't understand your question. Is it producing SQL you didn't expect?

fabrao00:11:03

Hello Sean, I expect the case be converted to

CASE WHEN (source > destination) THEN sort_order + 1 ELSE sort_order - 1 END

fabrao00:11:41

I don't how to write it to have the output

seancorfield01:11:35

I still don't understand. What does it produce? And why is what it produces not correct for you?

fabrao01:11:45

Sorry to not be clear. How can I use the :case to produce a case for set sort = ?

fabrao01:11:15

I have the query in string format, and I'm trying to convert it to honeysql

seancorfield01:11:47

What did you try and what did it produce?

seancorfield01:11:59

user=> (sql/format {:update :table :set {:source [:case [:> :source :destination] (sql/call :+ :sort-order 1) :else (sql/call :+ :sort-order 1)]}})
["UPDATE table SET source = CASE WHEN source > destination THEN sort_order + ? ELSE sort_order + ? END" 1 1]
user=>

seancorfield01:11:24

That's with HoneySQL v2 -- although it could be written without sql/call now:

user=> (sql/format {:update :table :set {:source [:case [:> :source :destination] [:+ :sort-order 1] :else [:+ :sort-order 1]]}})
["UPDATE table SET source = CASE WHEN source > destination THEN sort_order + ? ELSE sort_order + ? END" 1 1]
user=>

seancorfield01:11:51

I basically typed what you wrote into a REPL @U0YJJPFRA