honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
seancorfield 2020-09-28T18:31:20.067800Z

@viesti FYI, the V2 version of HoneySQL supports on conflict and returning per the PG docs https://www.postgresqltutorial.com/postgresql-upsert/ (although I need to add all those examples as test cases on the v2 branch). :do-update-set will handle both cases that are separate in the nilenso extension library: {:on-conflict :col, :do-update-set :foo} => ON CONFLICT (col) DO UPDATE SET foo = EXCLUDED.foo and {:on-conflict :col, :do-update-set {:foo :excluded.foo, :bar 42}} => ON CONFLICT (col) DO UPDATE SET foo = excluded.foo, bar = 42 -- and it supports {:do-nothing true} as well (being a map means that it needs something for a value), and also {:on-constraint :foo_key} or a :where clause for :on-conflict instead of just a column keyword.

viesti 2020-09-28T19:19:46.068400Z

awesome! 🙂

viesti 2020-09-28T19:26:55.069100Z

the postgresql docs also have some examples of WHERE condition used in the conflict action: https://www.postgresql.org/docs/13/sql-insert.html

seancorfield 2020-09-28T19:36:13.069700Z

Yup, that works too, but I don't have a test for it yet. I'll look at that example and add that as a test too.

dharrigan 2020-09-28T19:42:19.070Z

that's pretty wicked

dharrigan 2020-09-28T19:42:40.070500Z

I'm playing around with honeysql right now (v1!) and I'm so happy how easy it is to work with functions (psql functions)

dharrigan 2020-09-28T19:42:53.070700Z

(def pg_lock (-> (helpers/select :%pg_try_advisory_lock.1) core/format))

dharrigan 2020-09-28T19:43:24.071Z

(s/query db pg_lock)

dharrigan 2020-09-28T19:43:25.071200Z

etc...

dharrigan 2020-09-28T19:44:10.071600Z

Do you think window functions, such as over() would be supported in v2?

seancorfield 2020-09-28T19:46:37.072300Z

Yes, over is on my list. Just slowly working through everything in the nilenso library and the PG docs and the ANSI SQL standard...

dharrigan 2020-09-28T19:47:17.072900Z

I don't envy you 🙂

😂 1
dharrigan 2020-09-28T19:47:28.073300Z

Although I'm happy to test out the fruits of your labo(u)r

seancorfield 2020-09-28T19:47:29.073400Z

The page @viesti linked to above has DEFAULT syntax that I hadn't seen before... so that's going in now.

dharrigan 2020-09-28T19:48:10.074Z

I'm happy too that in v2, there will be "native" postgis function calls, rather than me doing sql/call

dharrigan 2020-09-28T19:48:20.074300Z

you're on my christmas card list this year sean!

seancorfield 2020-09-28T19:52:24.076500Z

My goal is that [:f arg1 arg2] will always be treated as a function call if it is valid. With :select that means you need an "empty alias" form to disambiguate: :select [ :foo, [:foo :bar], [ [:foo 42] ], [ [:foo 42] :bar ] ] => SELECT foo, foo AS bar, foo(42), foo(42) AS bar

dharrigan 2020-09-28T19:53:26.076700Z

interesting

dharrigan 2020-09-28T19:54:17.077200Z

so, would :select [:pg_try_advisory_lock 1] be the new normal?

seancorfield 2020-09-28T19:55:14.077700Z

That would be SELECT pg_try_advisory_lock, 1

seancorfield 2020-09-28T19:55:33.078200Z

What is the syntax you would want to generate there?

dharrigan 2020-09-28T19:55:50.078800Z

select pg_try_advisory_lock(1)

seancorfield 2020-09-28T19:56:11.079500Z

:select [[[:pg_try_advisory_lock 1]]] -- see my example above.

seancorfield 2020-09-28T19:56:35.080300Z

It's basically :select [[:something :alias]] without the alias.

seancorfield 2020-09-28T19:57:04.081100Z

[:pg_try_advisory_lock 1] is the :something in that.

dharrigan 2020-09-28T19:57:44.082100Z

would (def pg_lock (-> (helpers/select :%pg_try_advisory_lock.1) core/format)) i.e., the helpers + the format do the right-thang?

seancorfield 2020-09-28T19:58:56.083400Z

(format {:select :%pg_try_advisory_lock.1})
=> ["SELECT pg_try_advisory_lock(1)"]

seancorfield 2020-09-28T19:59:11.083900Z

So, yeah, you can use the shorthand with only a single argument.

dharrigan 2020-09-28T19:59:21.084400Z

yes, that works now 🙂 but with your new way of using : does that mean that :% is deprecated?

seancorfield 2020-09-28T19:59:27.084600Z

(I hate that syntax but I decided to support it anyway)

seancorfield 2020-09-28T19:59:45.085Z

That eval was from the V2 branch.

dharrigan 2020-09-28T20:00:08.085700Z

right, so no change then, I can continue to use :%, even 'tho you dislike it

seancorfield 2020-09-28T20:00:15.086Z

In V1 [:foo 42] is sometimes a function call and sometimes not. That's why you need sql/call sometimes in V1.

seancorfield 2020-09-28T20:01:16.087700Z

You can't use :%... with more than one argument right now I believe (and that won't change in V2).

dharrigan 2020-09-28T20:01:17.087800Z

If you recommend another way, other than using :% then I'm keen to go along, but if you think :% is okay__, then I'll continue to use

seancorfield 2020-09-28T20:01:58.088700Z

It will be supported and documented for 0-arity and 1-arity functions with very simple arguments (absolutely no formatting is done on the part after the dot)

dharrigan 2020-09-28T20:02:33.089500Z

right, so for simple functions such as the one above, it's okay.

seancorfield 2020-09-28T20:02:35.089600Z

:%now is valid in V1 and V2. [:now] would be the preferred syntax in V2 (in my opinion 🙂 )

dharrigan 2020-09-28T20:03:16.090400Z

I would use the new-improved version. It would be worthwhile to show an example of using that (perhaps using the example I used, of acquiring a lock, which calls a function with only one argument)?

dharrigan 2020-09-28T20:03:24.090600Z

(in your docs)

seancorfield 2020-09-28T20:06:33.091Z

Yup, this is exactly the sort of thing that will go into the all new documentation I'm writing.

dharrigan 2020-09-28T20:07:08.091300Z

you rock 🙂

seancorfield 2020-09-28T20:07:29.091800Z

There will also be extensive "differences from V1" docs -- the start of them is here https://github.com/seancorfield/honeysql/blob/v2/doc/differences-from-1-x.md

seancorfield 2020-09-28T20:08:04.092600Z

I don't have any helper functions yet so I can't fully update the README yet (so that it will pass the seancorfield/readme tests)

seancorfield 2020-09-28T20:09:30.094300Z

I want to auto-generate helpers from the clause definitions but that's difficult for ClojureScript so I may implement all the built-in helpers and then have a Clojure-only mechanism for auto-generating helpers for any new clauses registered but in cljs you'll have to either write those yourself or just use the data structure stuff.

dharrigan 2020-09-28T20:19:50.094800Z

magic!

dharrigan 2020-09-28T20:24:24.095Z

I'm offski. ttfn! 🙂