This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-26
Channels
- # babashka (5)
- # beginners (21)
- # cider (26)
- # clojure (46)
- # clojure-art (3)
- # clojure-dev (36)
- # clojure-europe (24)
- # clojure-norway (64)
- # conjure (28)
- # cursive (2)
- # datomic (1)
- # honeysql (12)
- # improve-getting-started (5)
- # introduce-yourself (5)
- # kaocha (1)
- # malli (17)
- # polylith (36)
- # reitit (3)
- # shadow-cljs (8)
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?Sorry, I don't understand your question. Is it producing SQL you didn't expect?
Hello Sean, I expect the case be converted to
CASE WHEN (source > destination) THEN sort_order + 1 ELSE sort_order - 1 END
I still don't understand. What does it produce? And why is what it produces not correct for you?
What did you try and what did it produce?
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=>
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=>
I basically typed what you wrote into a REPL @U0YJJPFRA