From b652824a33acded05ece2c70e83915e0f21eec86 Mon Sep 17 00:00:00 2001
From: Cam Saul <cam@geotip.com>
Date: Tue, 2 Jun 2015 17:42:09 -0700
Subject: [PATCH] fix setting the timezone in Native SQL queries :smirk:

---
 src/metabase/driver/generic_sql.clj        |  3 ++-
 src/metabase/driver/generic_sql/native.clj | 16 ++--------------
 2 files changed, 4 insertions(+), 15 deletions(-)

diff --git a/src/metabase/driver/generic_sql.clj b/src/metabase/driver/generic_sql.clj
index f796639ca50..103e673651e 100644
--- a/src/metabase/driver/generic_sql.clj
+++ b/src/metabase/driver/generic_sql.clj
@@ -11,7 +11,8 @@
 (defrecord SqlDriver [column->base-type
                       connection-details->connection-spec
                       database->connection-details
-                      sql-string-length-fn]
+                      sql-string-length-fn
+                      timezone->set-timezone-sql]
   IDriver
   ;; Connection
   (can-connect? [this database]
diff --git a/src/metabase/driver/generic_sql/native.clj b/src/metabase/driver/generic_sql/native.clj
index 6c141553f4d..6e6f1045bda 100644
--- a/src/metabase/driver/generic_sql/native.clj
+++ b/src/metabase/driver/generic_sql/native.clj
@@ -10,18 +10,6 @@
             [metabase.driver.generic-sql.util :refer :all]
             [metabase.models.database :refer [Database]]))
 
-(def ^:dynamic *timezone->set-timezone-sql*
-  " This function is called whenever `timezone` is specified in a native query, at the beginning of
-   the DB transaction.
-
-   If implemented, it should take a timestamp like `\"US/Pacific\"` and return a SQL
-   string that can be executed to set the timezone within the context of a transaction,
-   e.g. `\"SET LOCAL timezone to 'US/Pacific'\"`.
-
-   Because not all DB engines support timestamps (e.g., H2), the default implementation is a no-op.
-   Engines that *do* support timestamps (e.g., Postgres) should override this function."
-  (fn [_]))
-
 (defn- value->base-type
   "Attempt to match a value we get back from the DB with the corresponding base-type`."
   [v]
@@ -48,9 +36,9 @@
                                                 ;; If timezone is specified in the Query and the driver supports setting the timezone then execute SQL to set it
                                                 (when-let [timezone (or (-> query :native :timezone)
                                                                         (-> @(:organization database) :report_timezone))]
-                                                  (when-let [set-timezone-sql (*timezone->set-timezone-sql* timezone)]
+                                                  (when-let [timezone->set-timezone-sql (:timezone->set-timezone-sql (driver/database-id->driver database-id))]
                                                     (log/debug "Setting timezone to:" timezone)
-                                                    (jdbc/db-do-prepared conn set-timezone-sql)))
+                                                    (jdbc/db-do-prepared conn (timezone->set-timezone-sql timezone))))
                                                 (jdbc/query conn sql :as-arrays? true))]
          {:rows rows
           :columns columns
-- 
GitLab