Skip to content

Commit

Permalink
Fix H2 support; bump to 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed May 16, 2018
1 parent 20f9e79 commit a45e650
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions VERSION-HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
28 changes: 16 additions & 12 deletions src/toucan/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit a45e650

Please sign in to comment.