From 42b8e57f7c4bc280b9374af9b058212671f9f6b6 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 20 May 2016 18:30:36 -0400 Subject: [PATCH] db provided to views can now also be a function that returns a db --- src/views/honeysql/view.clj | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/views/honeysql/view.clj b/src/views/honeysql/view.clj index 7453ab8..3c28e6e 100644 --- a/src/views/honeysql/view.clj +++ b/src/views/honeysql/view.clj @@ -8,11 +8,14 @@ [clojure.java.jdbc :as j] [clojure.tools.logging :refer [warn]])) -(defrecord HSQLView [id db query-fn row-fn] +(defrecord HSQLView [id db-or-db-fn query-fn row-fn] IView (id [_] id) (data [_ namespace parameters] - (let [start (System/currentTimeMillis) + (let [db (if (fn? db-or-db-fn) + (db-or-db-fn namespace) + db-or-db-fn) + start (System/currentTimeMillis) data (j/query db (hsql/format (apply query-fn parameters)) :row-fn row-fn) time (- (System/currentTimeMillis) start)] (when (>= time 1000) (warn id "took" time "msecs")) @@ -24,6 +27,8 @@ (boolean (some #(not-empty (intersection (:hint %) tables)) nhints))))) (defn view - "Creates a Honey SQL view that uses a jdbc database configuration" - [id db hsql-fn & {:keys [row-fn]}] - (HSQLView. id db hsql-fn (or row-fn identity))) + "Creates a Honey SQL view that uses a JDBC database configuration. The db passed in + can either be a standard database connection map, or it can be a function that gets + passed a namespace and returns a database connection map." + [id db-or-db-fn hsql-fn & {:keys [row-fn]}] + (HSQLView. id db-or-db-fn hsql-fn (or row-fn identity)))