This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-06
Channels
- # adventofcode (10)
- # ai (2)
- # aleph (2)
- # announcements (21)
- # beginners (25)
- # calva (7)
- # cider (19)
- # clj-kondo (28)
- # clj-on-windows (3)
- # cljdoc (6)
- # clojure (80)
- # clojure-dev (15)
- # clojure-europe (29)
- # clojure-italy (3)
- # clojure-nl (37)
- # clojure-uk (4)
- # clojurescript (3)
- # cloverage (1)
- # conjure (6)
- # core-async (2)
- # cursive (17)
- # datalevin (9)
- # datomic (7)
- # deps-new (23)
- # emacs (4)
- # figwheel-main (6)
- # fulcro (6)
- # honeysql (19)
- # improve-getting-started (4)
- # inf-clojure (2)
- # introduce-yourself (5)
- # jobs (1)
- # leiningen (6)
- # lsp (73)
- # malli (1)
- # nrepl (2)
- # off-topic (37)
- # polylith (9)
- # quil (2)
- # reitit (16)
- # releases (2)
- # remote-jobs (6)
- # rewrite-clj (38)
- # shadow-cljs (1)
- # tools-build (1)
How can I do a CASE
statement in a SELECT
column? I.e, how would I write something like SELECT CASE WHEN 1 > 2 THEN 1 ELSE 2 END foo FROM bar
?
@wombawomba https://cljdoc.org/d/com.github.seancorfield/honeysql/2.2.840/doc/getting-started/sql-special-syntax-#case
Just remember that expressions (function call-like stuff) has to have an extra [
.. ]
around it in :select
to avoid confusion with aliased columns.
dev=> (sql/format {:select [ [ [:case [:> 1 2] 1 :else 2] :foo ] ] :from :bar})
["SELECT CASE WHEN ? > ? THEN ? ELSE ? END AS foo FROM bar" 1 2 1 2]
@seancorfield copy/pasting that code causes an exception on my end:
java.lang.AssertionError: Assert failed: Alias should have two parts[:case [:> 1 2] 1 :else 2]
(= 2 (count x))
I'm on 1.0.461
FWIWIt does work in Honeysql 2.x, but the actual code I'm formatting contains a bunch of #honeysql.types.SqlCall
, and I don't have the time to translate all of the associated code to 2.x at the moment.
@wombawomba Ah, I don't remember how you do it with 1.x, sorry, and I don't think it was documented.
Alright. @seancorfield can you think of any 'wrong' way to do it?
I kind of need to get it working and I'm out of ideas
if there's a hacky way to do it I can go with that and then just make sure to do it properly when I migrate to 2.x
It's been so long since I've used 1.x -- you know you can use both side-by-side and could change the code on a per-query basis?
the problem here though is that other parts of the query contain a bunch of #honeysql.types.SqlCall
and those give me an error when I give them to honey.sql/format
perhaps there's some easyish way to translate those other parts of the query to the equivalent hsql2?
You don't need that SqlCall
stuff in 2.x -- calls are just syntax [:func arg1 arg2]
etc.
@wombawomba The 1.x tests have examples of case/when: https://github.com/seancorfield/honeysql/blob/v1/test/honeysql/core_test.cljc#L150
@seancorfield thanks! (h/format {:select [[(h/call :case [:> 1 2] 1 :else 2) :foo]] :from [:bar]})
did the trick 🙂
The tests are pretty much the best "documentation" for 1.x. I'm trying hard to make that not the case for 2.x which has "proper" documentation on http://cljdoc.org 🙂
@seancorfield copy/pasting that code causes an exception on my end:
java.lang.AssertionError: Assert failed: Alias should have two parts[:case [:> 1 2] 1 :else 2]
(= 2 (count x))
I'm on 1.0.461
FWIWIt does work in Honeysql 2.x, but the actual code I'm formatting contains a bunch of #honeysql.types.SqlCall
, and I don't have the time to translate all of the associated code to 2.x at the moment.