honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
plexus 2020-10-14T12:47:53.170500Z

hey folks 👋:skin-tone-2: big fan of HoneySQL! most sensible way to write SQL in Clojure in my opinion 😄

plexus 2020-10-14T12:48:14.170900Z

anyone know how I can get honeysql to output something like this:

SELECT CURRENT DATE - 1 DAY FROM sysibm.sysdummy1

plexus 2020-10-14T12:48:20.171100Z

?

2020-10-14T13:04:51.171900Z

I would probably just do {:select [(sql/raw "CURRENT DATE - 1 DAY")] :from [:sysibm.sysdummy1]}.

2020-10-14T13:05:46.172500Z

I shied away from sql/raw for a while, but I find that it often makes expressions like this more readable.

dharrigan 2020-10-14T13:08:50.172800Z

I do something similar [:<= :created (sql/raw ["now() - interval '3 months'"])]

dharrigan 2020-10-14T13:10:11.173800Z

If you do have functions, you can do this (select :jdoc_id [(sql/call :to_char :created "YYYY-MM-DD HH24:MI:SS") :created]) which renders as select jdoc_id, to_char(created, ?) as created.... where YYYY-MM-DD HH24:MI:SS is passed in as a parameter.

plexus 2020-10-14T13:15:27.174300Z

thanks, #sql/raw does the trick!

seancorfield 2020-10-14T17:20:33.175Z

@plexus Which DB is that? I'd be happy to support it better via dialects in HoneySQL v2 if it makes sense to do so...

plexus 2020-10-15T07:17:25.177200Z

yes, Db2 on AS/400 ... pretty gnarly client project 🙂

seancorfield 2020-10-14T18:16:08.175400Z

FYI, two possible ways to do this in v2

user=> (-> (select [[:inline [:current-date :- 1 :day]]]) (from :table) (h/format))
["SELECT CURRENT DATE - 1 DAY FROM table"]
user=> (-> (select [[:- [:raw "CURRENT DATE"] [:inline [1 :day]]]]) (from :table) (h/format))
["SELECT CURRENT DATE - 1 DAY FROM table"]
user=> 
(the exact syntax is still a bit up in the air right now)

dharrigan 2020-10-14T18:56:04.176100Z

I believe it's DB2

seancorfield 2020-10-14T18:59:30.176300Z

Thanks. Will read up and see what's what with that.