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

rework generic sql native QP to handle expanded DB

parent e8758fb8
Branches
Tags
No related merge requests found
......@@ -5,10 +5,8 @@
[clojure.tools.logging :as log]
(korma [core :as korma]
db)
[metabase.db :refer [sel]]
[metabase.driver :as driver]
[metabase.driver.generic-sql.util :refer :all]
[metabase.models.database :refer [Database]]))
[metabase.driver.generic-sql.util :refer :all]))
(defn- value->base-type
"Attempt to match a value we get back from the DB with the corresponding base-type`."
......@@ -22,30 +20,27 @@
(defn process-and-run
"Process and run a native (raw SQL) QUERY."
{:arglists '([query])}
[{{sql :query} :native
database-id :database :as query}]
{:pre [(string? sql)
(integer? database-id)]}
[{{sql :query} :native, database :database, :as query}]
{:pre [(string? sql)]}
(log/debug "QUERY: \n"
(with-out-str (clojure.pprint/pprint query)))
(try (let [database (sel :one Database :id database-id)
db (-> database
(try (let [db (-> database
db->korma-db
korma.db/get-connection)
[columns & [first-row :as rows]] (jdbc/with-db-transaction [conn db :read-only? true]
;; 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)
(driver/report-timezone))]
(when-let [timezone->set-timezone-sql (:timezone->set-timezone-sql (driver/database-id->driver database-id))]
(when-let [timezone->set-timezone-sql (:timezone->set-timezone-sql (driver/engine->driver (:engine database)))]
(log/debug "Setting timezone to:" timezone)
(jdbc/db-do-prepared conn (timezone->set-timezone-sql timezone))))
(jdbc/query conn sql :as-arrays? true))]
{:rows rows
{:rows rows
:columns columns
:cols (map (fn [column first-value]
{:name column
:base_type (value->base-type first-value)})
columns first-row)})
:cols (map (fn [column first-value]
{:name column
:base_type (value->base-type first-value)})
columns first-row)})
(catch java.sql.SQLException e
(let [^String message (or (->> (.getMessage e) ; error message comes back like 'Column "ZID" not found; SQL statement: ... [error-code]' sometimes
(re-find #"^(.*);") ; the user already knows the SQL, and error code is meaningless
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment