next.jdbc Compendium II
seancorfield/next.jdbc 1.1.610
Updated 2022-09-12 to clarify camel-snake-kebab usage in more recent next.jdbc versions.
I recently released 1.1.610 and since it has been about five months since my last post summarizing advances in this library, I thought another summary post would be helpful.
As before, this post is organized into fixative changes, accretive changes, and documentation improvements, with each section organized by functionality or by database as appropriate.
Bug Fixes
Spec fixes:
next.jdbc/with-options(was missing).next.jdbc.connection/component.next.jdbc.prepare/statement.
next.jdbc.prepare/statementis now type hinted correctly.Result set builder adapters
:colsand:rsmetaaccess are now supported on these builders viaclojure.lang.ILookup.
Enhancements
jdbc.nextexecute!can return multiple result sets (if the:multi-rsoption is set totrue). This is useful when calling stored procedures and, for SQL Server, when working with T-SQL scripts. This was considered a big enough enhancement to bump the library from 1.0.z to 1.1.z because it has long been requested againstclojure.java.jdbc(andnext.jdbc).- Adds
with-optionsthat lets you wrap up a connectable along with default options that should be applied to all operations on that connectable. This provides most of the much-requested "default options" functionality (with caveats about functions that return native Java types). get-connectionnow has arities that accept username and password to support datasources that allow per-connection credentials.- Adds
snake-kebab-opts,unqualified-snake-kebab-opts, for use withwith-options. These are conditionally defined ifcamel-snake-kebabis on your classpath (see alsonext.jdbc.result-setenhancements below). As of 1.2.659, these are included unconditionally andnext.jdbcdepends directly oncamel-snake-kebab. - The result of
planis now foldable, as well as reducible (in theclojure.core.reducerssense). Folding the result of aplancall will use fork/join to provide some degree of parallelization of processing when streaming large result sets. - Inside the reducing function over
plan, you can callnext.jdbc.result-set/metadatato obtain a datafication of theResultSetMetaDataobject from the underlyingResultSetobject.
next.jdbc.connection- Adds
jdbc-urlto turn a "db spec" into a JDBC URL, for use with->poolandcomponent.
- Adds
next.jdbc.datafy(new namespace)- Requiring this namespace causes
datafy/navfunctionality to be extended to several JDBC object types, making it easier to navigate around metadata from the database and to examine schemas and result set metadata.
- Requiring this namespace causes
next.jdbc.plan(new namespace)- Adds
select!andselect-one!functions that simplify common uses (reductions) ofnext.jdbc/plan.select!lets you select just the values from a single column across the result set, or select a subset of columns by name.select-one!does the same but just for the first row (usingreducedto short-circuit the reduction).
- Adds
next.jdbc.prepareexecute-batch!now supports a:return-generated-keysoption so you can get back (all) the generated keys instead of just update counts.
next.jdbc.result-setas-kebab-mapsandas-unqualified-kebab-mapsare two new builders that are conditionally defined ifcamel-snake-kebabis on your classpath. As of 1.2.659, these are included unconditionally andnext.jdbcdepends directly oncamel-snake-kebab.datafiable-result-setnow allows the connectable and the options to be omitted. Passingnilfor the connectable (or omitting it) suppresses foreign key navigation (when usingdatafy/nav), instead of throwing an obscure exception.- Exposes
reducible-result-setandfoldable-result-setfor users who want more control over processing result sets obtained from database metadata. - Adds
with-column-valuetoRowBuilderprotocol and provides a more genericbuilder-adapterthat offers more control over how column values are read.
next.jdbc.sqlfind-by-keyssupports additional options to control how queries are run and results are returned::columnsselects just a subset of columns from the query,:top,:limit,:offset,:fetchprovide DB-specific ways to perform pagination (with all of the caveats of the underlying DB-specific syntax). In addition,:allmay be specified in place of the where clause or example hash map to return all rows of a table (generally, for use with pagination).- The functions in this namespace support connectables built via
next.jdbc/with-options.
next.jdbc.transaction- A new dynamic Var
*nested-tx*allows users to control how attempts to create nested transactions should behave. The defaultnext.jdbcbehavior is:allow(essentially "caveat programmer"). Behavior more compatible withclojure.java.jdbccan be obtained with the:ignorevalue. A:prohibitvalue will cause such attempts to throw exceptions.
- A new dynamic Var
next.jdbc.types(new namespace)- Contains
as-*functions corresponding tojava.sql.Types/*values, so you can "type hint" how values should be handled by the underlying JDBC driver. Note: these initially wrapped values in vectors but in order to provide better compatibility with HoneySQL they now use thunks.
- Contains
Microsoft SQL Server: the jTDS driver is now officially supported.
PostgreSQL 12.2.0 is officially supported (previously only 10.11 was officially supported).
Some (badly-behaved) JDBC drivers do not correctly implement empty result sets, returning
nullinstead of empty collections from some driver methods.next.jdbcis now more tolerant of this -- treating such collections as empty instead of throwing aNullPointerException.
Documentation
- Migration: offers an equivalent for
db-do-commandsfromclojure.java.jdbc. - MySQL Tips & Tricks describes batch statement rewriting (
:rewriteBatchedStatements). - SQLite Tips & Tricks describes the handling of
boolandbitcolumn types. - Clarifies realization actions in the docstrings for
row-number,column-names, andmetadata.
Thanks!
As always, thanks to everyone who uses next.jdbc, thanks to everyone who has provided feedback (especially on the documentation), reported bugs, suggested improvements, or sent pull requests. next.jdbc wouldn't be what it is today without all that input!
See the change log for all the changes in release order and cljdoc.org for overall documentation of the latest release.