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

handle nested fields in MQL

parent ac93c6ba
No related branches found
No related tags found
No related merge requests found
......@@ -140,12 +140,19 @@
with dataset mongo
ag rows
tbl tips
filter = ["." (id :tips :venue) "name"] "Kyle's Low-Carb Grill"
filter = venue...name "Kyle's Low-Carb Grill"
lim 10))
(defn x2 []
(Q run against geographical-tips
using mongo
aggregate rows
of tips
filter = venue...name "Kyle's Low-Carb Grill"
limit 10))
(defn y []
(datasets/with-dataset :mongo
(data/with-temp-db [db (data/dataset-loader) defs/geographical-tips]
(with-mongo-connection [_ db]
;; 61 ms ???
(active-subfield-names->type driver &tips.venue)))))
......@@ -43,7 +43,6 @@
"Run F with a new connection (bound to `*mongo-connection*`) to DATABASE.
Don't use this directly; use `with-mongo-connection`."
[f database]
(println (metabase.util/format-color 'red "<<OPENING A NEW MONGO CONNECTION>>"))
(let [connection-string (cond
(string? database) database
(:dbname (:details database)) (details-map->connection-string (:details database)) ; new-style -- entire Database obj
......@@ -55,10 +54,7 @@
(binding [*mongo-connection* mongo-connection]
(f *mongo-connection*))
(finally
(println (metabase.util/format-color 'red "DISCONNECTING!"))
(assert conn)
(mg/disconnect conn)
(println (metabase.util/format-color 'green "OK."))))))
(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.
......
......@@ -268,4 +268,11 @@
([color-symb x]
((ns-resolve 'colorize.core color-symb) (pprint-to-str x))))
(defmacro cond-let
"Like `if-let` or `when-let`, but for `cond`."
[binding-form then-form & more]
`(if-let ~binding-form ~then-form
~(when (seq more)
`(cond-let ~@more))))
(require-dox-in-this-namespace)
......@@ -5,7 +5,8 @@
[clojure.walk :refer [macroexpand-all]]
[metabase.driver :as driver]
[metabase.test.data :as data]
[metabase.test.data.datasets :as datasets]))
[metabase.test.data.datasets :as datasets]
[metabase.util :as u]))
(defn- partition-tokens [keywords tokens]
(->> (loop [all [], current-split nil, [token & more] tokens]
......@@ -74,14 +75,13 @@
(defmacro Q:field [f]
(or (when (symbol? f)
(let [f (name f)]
(if-let [[_ from to] (re-matches #"^(.*)->(.*)$" f)]
["fk->" `(Q:field ~(symbol from)) `(Q:field ~(symbol to))]
(if-let [[_ ag-field-index] (re-matches #"^ag\.(\d+)$" f)]
["aggregation" (Integer/parseInt ag-field-index)]
(when-let [[_ table field] (re-matches #"^(?:([^\.]+)\.)?([^\.]+)$" f)]
`(~'id ~(if table (keyword table)
'table)
~(keyword field)))))))
(u/cond-let
[[_ from to] (re-matches #"^(.+)->(.+)$" f)] ["fk->" `(Q:field ~(symbol from)) `(Q:field ~(symbol to))]
[[_ f sub] (re-matches #"^(.+)\.\.\.(.+)$" f)] ["." `(Q:field ~(symbol f)) sub #_`(Q:field ~(symbol sub))]
[[_ ag-field-index] (re-matches #"^ag\.(\d+)$" f)] ["aggregation" (Integer/parseInt ag-field-index)]
[[_ table field] (re-matches #"^(?:([^\.]+)\.)?([^\.]+)$" f)] `(~'id ~(if table (keyword table)
'table)
~(keyword field)))))
f))
(defmacro Q [& tokens]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment