honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
agile_geek 2021-02-19T10:08:11.201400Z

Morning, I am looking for an example of how to add an as alias to a subquery?

dharrigan 2021-02-19T10:20:27.201600Z

Like this?

dharrigan 2021-02-19T10:20:29.201800Z

user=> (sql/format {:with [[:query {:select [:foo] :from [:bar]}]]})
["WITH query AS (SELECT foo FROM bar)"]

dharrigan 2021-02-19T10:20:42.202Z

<https://github.com/seancorfield/honeysql/blob/develop/test/honeysql/format_test.cljc#L62>

dharrigan 2021-02-19T10:21:18.202200Z

If not, maybe some of the tests might give a clue?

borkdude 2021-02-19T10:39:19.202500Z

agile_geek 2021-02-19T10:54:24.205200Z

Thx @dharrigan @borkdude. I think that {:with …}example might work but thru trial and error I found this worked:

{:select [:sub.id]
                 :from
                 [[{:select [:id :name1 :name2]]
                    :from [:table1]
                    :modifiers [:distinct-on :name1 :name2]
                    :order-by [:name1 :name2]} :sub]]}
Suitably anonymised table names and columns plus sub query is simplified.

dharrigan 2021-02-19T11:00:39.205400Z

fantastic! 🙂

borkdude 2021-02-19T11:02:48.205500Z

Sorry, the video wasn't related to your question, I just posted it here because it was about honeysql

😂 1
agile_geek 2021-02-19T11:03:38.205800Z

Information is good, more information is better!

agile_geek 2021-02-19T11:04:30.206Z

I need to look at babashka as I think it may solve a bunch of issues with observability and debugging in my current client.

aratare 2021-02-19T14:34:32.206800Z

Hi there. Quick question about using HoneySQL/next.jdbc: Is there a nice or recommended way to handle conversion between dashes and underscores in keywords? There's :allow-dashed-names true but that's just allowing HoneySQL to include dashes in the formatted string. Thanks in advance.

dharrigan 2021-02-19T14:35:43.207400Z

In my code, I use dashes for keywords, i.e., tenant-id, that will automatically convert to tenant_id on the rendered SQL that goes out to the db.

dharrigan 2021-02-19T14:35:46.207600Z

Not had an issue.

borkdude 2021-02-19T14:37:08.208100Z

I actually showed that in the video earlier today and wondered where this was done. Later I saw this is in the format.clj namespace

aratare 2021-02-19T14:37:36.208400Z

Ah so it's automatic with no configuration I assume?

dharrigan 2021-02-19T14:37:43.208600Z

not had to configure anything 🙂

aratare 2021-02-19T14:38:05.209Z

Splendid! Thanks a lot 🙂

dharrigan 2021-02-19T14:38:42.209200Z

you're most welcome

borkdude 2021-02-19T14:39:49.209700Z

I recommend using v2 (the alpha one): it has cool new features, like passing in maps as values

dharrigan 2021-02-19T14:40:53.210400Z

Yes, I agree. If you're starting off on something new 🙂 I don't think it'll be that long before 2.0.0 Alpha2 becomes a beta, then a final 🙂

dharrigan 2021-02-19T14:41:14.210900Z

I've converted my applications to use honeysql v2 (on branches atm) works wonderfully well.

dharrigan 2021-02-19T14:41:16.211100Z

and easier too!

aratare 2021-02-19T14:44:58.211900Z

> passing in maps as values How do you mean sorry?

borkdude 2021-02-19T14:46:30.212300Z

(helpers/values [{:a 1 :b 2}])

borkdude 2021-02-19T14:47:04.212900Z

you must omit the column names if you are using that though

borkdude 2021-02-19T14:47:10.213100Z

because they will be inferred from the maps

borkdude 2021-02-19T14:47:45.213800Z

check the video above if you want to know how this works :)

aratare 2021-02-19T14:47:53.214100Z

I think that's already available in the current version? At least that's what the doc is telling me:

(-&gt; (insert-into :properties)
    (values [{:name "John" :surname "Smith" :age 34}
             {:name "Andrew" :surname "Cooper" :age 12}
             {:name "Jane" :surname "Daniels" :age 56}])
    sql/format)

borkdude 2021-02-19T14:48:16.214400Z

oh really? I thought that was new, sorry

aratare 2021-02-19T14:48:30.214900Z

but totally agree that it is awesome to use 😄

dharrigan 2021-02-19T14:49:18.215400Z

@seancorfield would it be possible for honeysqlv2 support create/edit/drop materialized view? which is the same as create view for postgresql?

dharrigan 2021-02-19T14:49:24.215600Z

would be supar handy

dharrigan 2021-02-19T14:49:54.215800Z

I would replace this (-&gt; (sql/raw (str "refresh materialized view " view " with data"))

dharrigan 2021-02-19T14:50:07.216100Z

and (-&gt; (sql/raw (str "create materialized view " view " with data"))

dharrigan 2021-02-19T14:50:47.216500Z

I can raise a jira if you are willing, no issues if not 🙂

dharrigan 2021-02-19T14:50:51.216700Z

jira!?

dharrigan 2021-02-19T14:50:53.216900Z

wtf

dharrigan 2021-02-19T14:51:01.217200Z

I'm too "enterprisey"

seancorfield 2021-02-19T15:43:41.218400Z

Sure, create a GH issue so I don't forget.

dharrigan 2021-02-19T15:43:49.218600Z

w00t

dharrigan 2021-02-19T15:51:22.218800Z

done. thank you.

seancorfield 2021-02-19T16:46:50.219200Z

Quick Q @dharrigan Is create view different from create materialized view?

seancorfield 2021-02-19T16:49:14.219600Z

Ah, yes, different syntax. So many options.

dharrigan 2021-02-19T16:51:43.220100Z

:thumbsup:

dharrigan 2021-02-19T16:52:34.220700Z

Not suggesting perhaps to support all the options, maybe even refresh materialized view &lt;blah&gt; with [no] data would be great 🙂