From a45e650eaf604d0802c30d2b4d71cf032dc77f60 Mon Sep 17 00:00:00 2001 From: Cam Saul Date: Tue, 15 May 2018 18:50:20 -0700 Subject: [PATCH] Fix H2 support; bump to 1.1.5 --- ISSUE_TEMPLATE.md | 3 +++ VERSION-HISTORY.md | 4 ++++ project.clj | 10 +++++----- src/toucan/db.clj | 28 ++++++++++++++++------------ 4 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 ISSUE_TEMPLATE.md diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..d803fb9 --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,3 @@ +Thanks for your interest in Toucan! + +Make sure to at-mention @camsaul in the Issue description. Otherwise I won't get an email about it and might not respond to it right away. diff --git a/VERSION-HISTORY.md b/VERSION-HISTORY.md index 545d948..a433186 100644 --- a/VERSION-HISTORY.md +++ b/VERSION-HISTORY.md @@ -1,5 +1,9 @@ # Toucan Version History & Release Notes +### [1.1.5](https://github.com/metabase/toucan/compare/1.1.4...1.1.5) (May 15th, 2018) + +* Add support for newer versions of H2 database. + ### [1.1.4](https://github.com/metabase/toucan/compare/1.1.3...1.1.4) (January 17th, 2018) * Add new [`select-reducible` function](https://github.com/metabase/toucan/blob/master/docs/db-functions.md#select-reducible) to fetch streams of results diff --git a/project.clj b/project.clj index 655af37..21547a9 100644 --- a/project.clj +++ b/project.clj @@ -1,14 +1,14 @@ -(defproject toucan "1.1.5-SNAPSHOT" +(defproject toucan "1.1.5" :description "Functionality for defining your application's models and querying the database." :url "https://github.com/metabase/toucan" :license {:name "Eclipse Public License" :url "https://raw.githubusercontent.com/metabase/toucan/master/LICENSE.txt"} :min-lein-version "2.5.0" - :dependencies [[org.clojure/java.classpath "0.2.3"] - [org.clojure/java.jdbc "0.7.5"] - [org.clojure/tools.logging "0.4.0"] + :dependencies [[org.clojure/java.classpath "0.3.0"] + [org.clojure/java.jdbc "0.7.6"] + [org.clojure/tools.logging "0.4.1"] [org.clojure/tools.namespace "0.2.10"] - [honeysql "0.9.1"]] + [honeysql "0.9.2"]] :javac-options ["-target" "1.7", "-source" "1.7"] :aliases {"bikeshed" ["bikeshed" "--max-line-length" "118" "--var-redefs" "false"] "lint" ["do" ["eastwood"] "bikeshed" "docstring-checker"] diff --git a/src/toucan/db.clj b/src/toucan/db.clj index be9d045..694d40c 100644 --- a/src/toucan/db.clj +++ b/src/toucan/db.clj @@ -514,14 +514,16 @@ ;;; ### INSERT! -(defn- insert-id-key - "The keyword name of the ID column of a newly inserted row returned by `jdbc/insert!`." - ^clojure.lang.Keyword [] - (case (quoting-style) - :ansi :id - :sqlserver :id ; not sure this is correct :/ - :mysql :generated_key - :h2 (keyword "scope_identity()"))) +(defn get-inserted-id + "Get the ID of a row inserted by `jdbc/db-do-prepared-return-keys`." + [insert-result] + (or + ;; Postgres, newer H2, and most others return :id + (:id insert-result) + ;; :generated_key is returned by MySQL + (:generated_key insert-result) + ;; scope_identity() returned by older versions of H2 + ((keyword "scope_identity()") insert-result))) (defn simple-insert-many! "Do a simple JDBC `insert!` of multiple objects into the database. @@ -535,10 +537,12 @@ [model row-maps] {:pre [(sequential? row-maps) (every? map? row-maps)]} (when (seq row-maps) - (let [model (resolve-model model) - to-sql (fn [row] (honeysql->sql {:insert-into model :values [row]})) - do-query (fn [qry] (jdbc/db-do-prepared-return-keys (connection) false qry {}))] - (doall (map (comp (insert-id-key) do-query to-sql) row-maps))))) + (let [model (resolve-model model)] + (doall + (for [row-map row-maps + :let [sql (honeysql->sql {:insert-into model, :values [row-map]})]] + (-> (jdbc/db-do-prepared-return-keys (connection) false sql {}) ; false = don't use a transaction + get-inserted-id)))))) (defn insert-many! "Insert several new rows into the Database. Resolves ENTITY, and calls `pre-insert` on each of the ROW-MAPS.