Skip to content
Snippets Groups Projects
Commit b94e86c5 authored by Cam Saul's avatar Cam Saul
Browse files

Mongo QP

parent 20c68c03
Branches
Tags
No related merge requests found
......@@ -72,6 +72,7 @@
[lein-instant-cheatsheet "2.1.1"] ; use awesome instant cheatsheet created by yours truly w/ 'lein instant-cheatsheet'
[lein-marginalia "0.8.0"] ; generate documentation with 'lein marg'
[refactor-nrepl "1.0.1"]] ; support for advanced refactoring in Emacs/LightTable
:warn-on-reflection true ; Emit warnings on all reflection calls
:jvm-opts ["-Dlogfile.path=target/log"
"-Xms1024m" ; give JVM a decent heap size to start with
"-Xmx2048m" ; hard limit of 2GB so we stop hitting the 4GB container limit on CircleCI
......
......@@ -11,48 +11,12 @@
[db :as mdb]
[query :as mq])
[metabase.driver :as driver]
(metabase.driver [interface :refer :all])))
[metabase.driver.interface :refer :all]
(metabase.driver.mongo [query-processor :as qp]
[util :refer [*mongo-connection* with-mongo-connection]])))
(declare driver)
;; ## with-mongo-connection
(def ^:dynamic *mongo-connection*
"Bound by top-level `with-mongo-connection` so it may be reused within its body."
nil)
(defn- -with-mongo-connection
"Run F with a new connection (bound to `*mongo-connection*`) to DATABASE.
Don't use this directly; use `with-mongo-connection`."
[f database]
(let [connection-string (if (map? database) (-> database :details :conn_str)
database)
_ (assert (string? connection-string) (str "with-mongo-connection failed: connection string is must be a string, got: " connection-string))
{conn :conn mongo-connection :db} (mg/connect-via-uri connection-string)]
(log/debug (color/cyan "<< OPENED NEW MONGODB CONNECTION >>"))
(try
(binding [*mongo-connection* mongo-connection]
(f *mongo-connection*))
(finally
(mg/disconnect conn)))))
(defmacro with-mongo-connection
"Open a new MongoDB connection to DATABASE-OR-CONNECTION-STRING, bind connection to BINDING, execute BODY, and close the connection.
The DB connection is re-used by subsequent calls to `with-mongo-connection` within BODY.
(We're smart about it: DATABASE isn't even evaluated if `*mongo-connection*` is already bound.)
(with-mongo-connection [conn @(:db (sel :one Table ...))] ; delay isn't derefed if *mongo-connection* is already bound
...)
(with-mongo-connection [conn \"mongodb://127.0.0.1:27017/test\"] ; use a string instead of a DB
...)"
[[binding database] & body]
`(let [f# (fn [~binding]
~@body)]
(if *mongo-connection* (f# *mongo-connection*)
(-with-mongo-connection f# ~database))))
;;; ### Driver Helper Fns
(defn- table->column-names
......@@ -109,7 +73,8 @@
dbname)))
;;; ### QP
(process-query [_ query])
(process-query [_ query]
(qp/process-and-run query))
;;; ### Syncing
(sync-in-context [_ database do-sync-fn]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment