sql

All things SQL and JDBC...
2020-10-02T00:29:45.031900Z

Has anyone made use of PGJDBC-NG, https://impossibl.github.io/pgjdbc-ng in Clojure projects? I see that it has much better support for some of Postgres' features, like custom composite types. I've been using Hugsql as a higher level framework, and it supports custom adapters, but a community one hasn't been contributed yet. Maybe someone here has done some lower-level work with it.

seancorfield 2020-10-02T03:33:00.033600Z

clojure.java.jdbc was tested with it against an older version of PostgreSQL. next.jdbc should work with it too, although I only test against the official driver (and use the Embedded PostgreSQL engine for testing).

seancorfield 2020-10-02T03:34:45.034600Z

That said @bdrillard HugSQL has an adapter for next.jdbc so you could use HugSQL with next.jdbc and the Impossibl JDBC driver.

emccue 2020-10-02T19:12:14.035Z

How exactly does :return-keys work in next.jdbc?

emccue 2020-10-02T19:13:06.035600Z

does it map to something standard in jdbc or is it database dependent?

emccue 2020-10-02T19:13:29.036100Z

(to the degree that "standard" means a given database driver won't do a wacky thing)

seancorfield 2020-10-02T19:36:29.036400Z

Same functionality as in clojure.java.jdbc 🙂

seancorfield 2020-10-02T19:36:49.036900Z

Yes, it maps to standard JDBC functionality... just a sec... let me pull up the source...

seancorfield 2020-10-02T19:38:27.038100Z

If it's a vector of column names, it's passed into .prepareStatement as a String[] to suggest that JDBC return those columns after execution. Else Statement/RETURN_GENERATED_KEYS is passed to .prepareStatement

seancorfield 2020-10-02T19:39:26.038500Z

With the caveat that

(str ":concurrency, :cursors, and :result-type "
                          "may not be specified with :return-keys."))))
which is a JDBC restriction.

seancorfield 2020-10-02T19:40:29.039500Z

The ultimate behavior of :return-keys is, of course, vendor-specific -- not all drivers support an array of columns; not all drivers actually return keys even when you ask for them.

seancorfield 2020-10-02T19:40:38.039800Z

@emccue Does that help?

seancorfield 2020-10-02T19:43:02.040900Z

The other thing that next.jdbc does with :return-keys is that it triggers a call to .getGeneratedKeys on any operation that does not automatically return a ResultSet, i.e., when .execute returns false.

emccue 2020-10-02T19:44:56.041300Z

yeah, that makes sense

emccue 2020-10-02T19:46:12.042600Z

so some dbs support Statement/RETURN_GENERATED_KEYS and others only do .getGeneratedKeys and others don't support it at all

emccue 2020-10-02T19:46:22.042900Z

because sql is sql

seancorfield 2020-10-02T20:29:17.044300Z

Well, .getGeneratedKeys only works if either Statement/RETURN_GENERATED_KEYS or a String[] of columns was accepted in .prepareStatement -- I only mention them separately because you can create a prepared statement independently of executing it and you need to pass :return-keys in both contexts if you do that.

seancorfield 2020-10-02T20:29:49.045Z

If you rely on next.jdbc (or clojure.java.jdbc) creating the prepared statement for you behind the scenes, it automatically does both things.

seancorfield 2020-10-02T20:31:04.046200Z

For example, in PostgreSQL, insert gives you back a result set with all columns in it (so the .getGeneratedKeys path never gets executed because .execute returns true and we call .getResultSet instead I believe).

seancorfield 2020-10-02T20:31:47.047100Z

(I haven't confirmed that -- it may be that all columns is the default from .getGeneratedKeys but the effect is the same: a result set with all columns in it)

seancorfield 2020-10-02T20:32:51.048400Z

And if you say insert ... returning * in PostgreSQL I think that returns you all the columns as well (but as a result set in the first place rather than via .getGeneratedKeys -- again, I haven't specifically confirmed that, but I suspect you don't even need :return-keys in such a case?)

seancorfield 2020-10-02T20:32:58.048600Z

"because sql is sql"