diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd2cca..faed912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Changes made on master since 1.0.462: * Add tests for `"jtds"` database driver (against MS SQL Server), making it officially supported. * Switch from OpenTable Embedded PostgreSQL to Zonky's version, so that testing can move forward from PostgreSQL 10.11 to 12.2.0. * Address #119 by clarifying realization actions in the docstrings for `row-number`, `column-names`, and `metadata`. +* Address #115 by adding equivalent of `db-do-commands` in the `clojure.java.jdbc` migration guide. * Add log4j2 as a test dependency so that I have better control over logging (which makes debugging easier!). ## Stable Builds diff --git a/doc/migration-from-clojure-java-jdbc.md b/doc/migration-from-clojure-java-jdbc.md index c22f69b..a3b7f6f 100644 --- a/doc/migration-from-clojure-java-jdbc.md +++ b/doc/migration-from-clojure-java-jdbc.md @@ -55,6 +55,18 @@ The `next.jdbc.sql` namespace contains several functions with similarities to `c * `update!` -- similar to `clojure.java.jdbc/update!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector), * `delete!` -- similar to `clojure.java.jdbc/delete!` but will also accept a hash map of column name/value pairs instead of a partial where clause (vector). +If you were using `db-do-commands` in `clojure.java.jdbc` to execute DDL, the following is the equivalent in `next.jdbc`: + +```clojure +(defn do-commands [connectable commands] + (if (instance? java.sql.Connection connectable) + (with-open [stmt (next.jdbc.prepare/statement connectable)] + (run! #(.addBatch stmt %) commands) + (into [] (.executeBatch stmt))) + (with-open [conn (next.jdbc/get-connection connectable)] + (do-commands conn commands)))) +``` + ### `:identifiers` and `:qualifier` If you are using `:identifiers`, you will need to change to the appropriate `:builder-fn` option with one of `next.jdbc.result-set`'s `as-*` functions. diff --git a/test/next/jdbc/test_fixtures.clj b/test/next/jdbc/test_fixtures.clj index 9b3da86..7d07df9 100644 --- a/test/next/jdbc/test_fixtures.clj +++ b/test/next/jdbc/test_fixtures.clj @@ -4,6 +4,7 @@ "Multi-database testing fixtures." (:require [clojure.string :as str] [next.jdbc :as jdbc] + [next.jdbc.prepare :as prep] [next.jdbc.sql :as sql]) (:import (io.zonky.test.db.postgres.embedded EmbeddedPostgres))) @@ -92,6 +93,16 @@ [] @test-datasource) +(defn- do-commands + "Example from migration docs: this serves as a test for it." + [connectable commands] + (if (instance? java.sql.Connection connectable) + (with-open [stmt (prep/statement connectable)] + (run! #(.addBatch stmt %) commands) + (into [] (.executeBatch stmt))) + (with-open [conn (jdbc/get-connection connectable)] + (do-commands conn commands)))) + (defn with-test-db "Given a test function (or suite), run it in the context of an in-memory H2 database set up with a simple fruit table containing four rows of data. @@ -122,9 +133,9 @@ "AUTO_INCREMENT PRIMARY KEY")] (with-open [con (jdbc/get-connection (ds))] (try - (jdbc/execute-one! con [(str "DROP TABLE " fruit)]) + (do-commands con [(str "DROP TABLE " fruit)]) (catch Exception _)) - (jdbc/execute-one! con [(str " + (do-commands con [(str " CREATE TABLE " fruit " ( ID INTEGER " auto-inc-pk ", NAME VARCHAR(32),