Skip to content
Snippets Groups Projects
Commit 48ab0bac authored by Cam Saül's avatar Cam Saül
Browse files

Fix relative date SQL parameters :wrench:

parent 2270c51c
No related branches found
No related tags found
No related merge requests found
(ns metabase.query-processor.parameters
"Code for handling parameter substitution in MBQL & native queries."
"Code for handling parameter substitution in MBQL queries."
(:require [clojure.core.match :refer [match]]
[clojure.string :as s]
(clj-time [core :as t]
......
(ns metabase.query-processor.sql-parameters
"Param substitution for *SQL* drivers."
"Param substitution for *SQL* queries."
(:require [clojure.string :as s]
[honeysql.core :as hsql]
[metabase.db :as db]
......@@ -27,7 +27,7 @@
(defrecord ^:private NumberValue [value])
(defn- dimension-value->sql [dimension-type value]
(if (contains? #{"date/range" "date/month-year" "date/quarter-year"} dimension-type)
(if (contains? #{"date/range" "date/month-year" "date/quarter-year" "date/relative"} dimension-type)
(->sql (map->DateRange ((resolve 'metabase.query-processor.parameters/date->range) value *timezone*))) ; TODO - get timezone from query dict
(str "= " (->sql value))))
......
......@@ -478,7 +478,8 @@
~@body
~'<>))
(def ^String ^{:style/indent 2} format-color
(def ^String ^{:style/indent 2, :arglists '([color-symb x] [color-symb format-str & args])}
format-color
"Like `format`, but uses a function in `colorize.core` to colorize the output.
COLOR-SYMB should be a quoted symbol like `green`, `red`, `yellow`, `blue`,
`cyan`, `magenta`, etc. See the entire list of avaliable colors
......
......@@ -364,3 +364,16 @@
:native {:query (format "SELECT COUNT(*) FROM %s WHERE {{checkin_date}}" (checkins-identifier))
:template_tags {:checkin_date {:name "checkin_date", :display_name "Checkin Date", :type "dimension", :dimension ["field-id" (data/id :checkins :date)]}}}
:parameters []}))))
;; test that relative dates work correctly. It should be enough to try just one type of relative date here,
;; since handling them gets delegated to the functions in `metabase.query-processor.parameters`, which is fully-tested :D
(datasets/expect-with-engines sql-parameters-engines
[0]
(first-row
(format-rows-by [int]
(qp/process-query
{:database (data/id)
:type :native
:native {:query (format "SELECT COUNT(*) FROM %s WHERE {{checkin_date}}" (checkins-identifier))
:template_tags {:checkin_date {:name "checkin_date", :display_name "Checkin Date", :type "dimension", :dimension ["field-id" (data/id :checkins :date)]}}}
:parameters [{:type "date/relative", :target ["dimension" ["template-tag" "checkin_date"]], :value "thismonth"}]}))))
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