5.5 KiB
Migrating from clojure.java.jdbc
This page attempts to list all of the differences between clojure.java.jdbc and next.jdbc. Some of them are large and obvious, some of them are small and subtle -- all of them are deliberate design choices.
Primary API
next.jdbc has a deliberately narrow primary API that has (almost) no direct overlap with clojure.java.jdbc:
get-datasource-- has no equivalent inclojure.java.jdbcbut is intended to emphasizejavax.sql.DataSourceas a starting point,get-connection-- overlaps withclojure.java.jdbc(and returns ajava.sql.Connection) but accepts only a subset of the options (:dbtype/:dbnamehash map,StringJDBC URI);clojure.java.jdbc/get-connectionaccepts{:datasource ds}whereasnext.jdbc/get-connectionaccepts thejavax.sql.DataSourceobject directly,prepare-- somewhat similar toclojure.java.jdbc/prepare-statementbut it accepts a vector of SQL and parameters (compared to just a raw SQL string),reducible!-- somewhat similar toclojure.java.jdbc/reducible-querybut accepts arbitrary SQL statements for execution,execute!-- has no equivalent inclojure.java.jdbc,execute-one!-- has no equivant inclojure.java.jdbc,transact-- similar toclojure.java.jdbc/db-transaction*,with-transaction-- similar toclojure.java.jdbc/with-db-transaction.
If you were using a bare db-spec hash map or JDBC URI string everywhere, that should mostly work with next.jdbc since most functions accept a "connectable", but it would be better to create a datasource first, and then pass that around.
If you were already creating a pooled connection datasource, as a {:datasource ds} hashmap, then passing (:datasource db-spec) to the next.jdbc functions is the simplest migration path.
If you were using other forms of the db-spec hash map, you'll need to adjust to one of the three modes above, since those are the only ones supported in next.jdbc.
The next.jdbc.sql namespace contains several functions with similarities to clojure.java.jdbc's core API:
insert!-- similar toclojure.java.jdbc/insert!but only supports inserting a single map,insert-multi!-- similar toclojure.java.jdbc/insert-multi!but only supports inserting columns and a vector of row values,query-- similar toclojure.java.jdbc/query,find-by-keys-- similar toclojure.java.jdbc/find-by-keysbut also accepts a partial where clause (vector),get-by-id-- similar toclojure.java.jdbc/get-by-id,update!-- similar toclojure.java.jdbc/update!but also accepts a hash map of column name/value pairs,delete!-- similar toclojure.java.jdbc/delete!but also accepts a hash map of column name/value pairs.
If you are using :identifiers and/or :entities, you will need to change to appropriate :gen-fn and/or :table-fn/:column-fn options.
If you are using :result-set-fn and/or :row-fn, you will need to change to explicit calls (to the result set function, or to map the row function), or to use the reducible! approach with reduce or various transducing functions.
Minor differences from Issue #5
(this section needs to be edited/expanded)
- Use of protocols instead of db-spec / hash map
- Several legacy db-spec formats are no longer accepted, including Raw, Existing Connection, DriverManager, Factory, DataSource, JNDI, URI.
- Auto-qualified column names (the qualifier is table from which each column comes, if known)
- Result sets are never lazy now -- either you reduce over them, or you get an eager vector of hash maps; for streaming, reduce the
reducible! - The
:as-arrays?option has been replaced by a result set builder functionnext.jdbc.result-set/as-arrays(the newRowBuilderandResultSetBuilderprotocols allow for a lot more flexibility about how rows and result sets are constructed) - There is only one type of SQL execution, using the generic
.execute-- no worrying about batches and updates and so on - No
?on keyword options - Update counts come back as a "result set" for consistency
- The
:identifiersoption is gone and column names come back as qualified keywords with no additional processing, rather thanclojure.string/lower-case(partly so the default is faster but also so the default behavior is to "not mess with things") -- the result set builder machinery provides an easy way to provide custom column naming if you need it :result-set-fnand:row-fnare not supported -- either wrap the call around the query or handle it via a reduction; for:result-set-fn firstuseexecute-one!instead- The
:entitiesoption has been replaced by:table-fnand:column-fnand thequotedfunction is gone --next.jdbc.quotednow contains specific functions, named for the database/type of quoting:ansi,mysql,sql-server, withoracleandpostgresas aliases foransi with-db-connectionis justwith-openwith a call toget-connectionwith-transactioncan take a:rollback-onlyoption, but there is no way to change a transaction to rollback dynamically; throw an exception instead (all transactions roll back on an exception)- The "sugar" functions --
query,insert!,insert-multi!,update!, anddelete!live innext.jdbc.sqlas they are no longer part of the core API find-by-keysno longer supports:order-by(but this may come back)- Extension points for setting parameters and reading columns are
SettableParameterandReadableColumn
More will be added...