Fork me on GitHub
#honeysql
<
2023-08-03
>
nbardiuk17:08:40

Is there a way to reference a quoted alias without :raw? I want to use quoted alias in ORDER BY clause

SELECT column_name AS "some-alias" FROM b ORDER BY "some-alias" ASC
I would like to avoid using :raw because the alias value comes from 3d party
(honey.sql/format {:select [[:column-name "some-alias"]]
                   :from :b
                   :order-by [[[:raw "\"some-alias\""]]]})
Quoting everything doesn't work because it doesn't convert kebab column names to underscores
(honey.sql/format {:select [[:column-name "some-alias"]]
                   :from :b
                   :order-by [:some-alias]}
                  {:quoted true})
SELECT "column-name" AS "some-alias" FROM "b" ORDER BY "some-alias" ASC
And quoted-snake corrupts dashes in aliases
(honey.sql/format {:select [[:column-name "some-alias"]]
                   :from :b
                   :order-by [:some-alias]}
                  {:quoted-snake true})
SELECT column_name AS "some_alias" FROM b ORDER BY some_alias ASC

p-himik17:08:42

Oh, huh. There's stuff like :'x but apparently it works only when it's in a function-calling context.

👍 2
nbardiuk17:08:53

thank you for the :'x didn't know it exists, it can cover some cases

seancorfield18:08:57

@U076FM90B Feel free to create an issue on GitHub for me to look at. AS aliases have special treatment so maybe I'll need to add special syntax like [:alias "some-alias"] so you can get that treatment elsewhere...

👍 2
p-himik18:08:43

@U04V70XH6 Wouldn't it make more sense to support :'x everywhere? A breaking change, I suppose, but maybe could be put behind an opt-in flag?

seancorfield18:08:06

That's something I'll take a look at as part of this issue.

seancorfield18:08:45

I think the behavior of :'x may already be context-dependent but I'd have to refresh my memory...