sql 2024-04-10

Can we write this expression {:select [:*] :from [:with_count [[:jsonb_to_record :derived] [:x [:raw "(work_days integer)"]]]]} that results to SELECT * FROM with_count AS wc, JSONB_TO_RECORD(derived) AS x (work_days integer) more natively without :raw using com.github.seancorfield/honeysql?

It almost fits. But unfortunately it sets unneded comma between field and its type.

(sql/format {:select [:*]
             :from   [:with_count
                      [[:jsonb_to_record :derived]
                       [[:x :work-days :!integer]]]]})
=> ["SELECT * FROM with_count, JSONB_TO_RECORD(derived) AS X(work_days INTEGER)"]

A bit of a hack, since :x is treated as a function call and hence will be using different quoting rules.

Not sure if there's a better way. But you can always add your own clause, it's trivial.

Thanks for the tip. I guess we need to update honeysql version to make it work the same.

Found a better way:

(sql/format {:select [:*]
             :from   [:with_count
                      [[:jsonb_to_record :derived]
                       [:x {:with-columns [[:work-days :integer]]}]]]})
=> ["SELECT * FROM with_count, JSONB_TO_RECORD(derived) AS x (work_days INTEGER)"]

🎉 1

Oh, and also - there's #honeysql.

Oh, great! I love it, works like a charm. Thanks! Yes, I didn't notice this channel.

👍 1