db provided to views can now also be a function that returns a db
This commit is contained in:
parent
06edd82ff5
commit
42b8e57f7c
|
@ -8,11 +8,14 @@
|
||||||
[clojure.java.jdbc :as j]
|
[clojure.java.jdbc :as j]
|
||||||
[clojure.tools.logging :refer [warn]]))
|
[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
|
IView
|
||||||
(id [_] id)
|
(id [_] id)
|
||||||
(data [_ namespace parameters]
|
(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)
|
data (j/query db (hsql/format (apply query-fn parameters)) :row-fn row-fn)
|
||||||
time (- (System/currentTimeMillis) start)]
|
time (- (System/currentTimeMillis) start)]
|
||||||
(when (>= time 1000) (warn id "took" time "msecs"))
|
(when (>= time 1000) (warn id "took" time "msecs"))
|
||||||
|
@ -24,6 +27,8 @@
|
||||||
(boolean (some #(not-empty (intersection (:hint %) tables)) nhints)))))
|
(boolean (some #(not-empty (intersection (:hint %) tables)) nhints)))))
|
||||||
|
|
||||||
(defn view
|
(defn view
|
||||||
"Creates a Honey SQL view that uses a jdbc database configuration"
|
"Creates a Honey SQL view that uses a JDBC database configuration. The db passed in
|
||||||
[id db hsql-fn & {:keys [row-fn]}]
|
can either be a standard database connection map, or it can be a function that gets
|
||||||
(HSQLView. id db hsql-fn (or row-fn identity)))
|
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)))
|
||||||
|
|
Loading…
Reference in a new issue