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

remove uncastify

parent 44dc9560
No related branches found
No related tags found
No related merge requests found
......@@ -23,10 +23,7 @@
timezone->set-timezone-sql
;; These functions take a string name of a Table and a string name of a Field and return the raw SQL to select it as a DATE
cast-timestamp-seconds-field-to-date-fn
cast-timestamp-milliseconds-field-to-date-fn
;; This should be a regex that will match the column returned by the driver when unix timestamp -> date casting occured
;; e.g. #"CAST\(TIMESTAMPADD\('(?:MILLI)?SECOND', ([^\s]+), DATE '1970-01-01'\) AS DATE\)" for H2
uncastify-timestamp-regex]
cast-timestamp-milliseconds-field-to-date-fn]
IDriver
;; Features
(supports? [_ feature]
......
......@@ -34,6 +34,7 @@
(log/debug "Setting timezone to:" timezone)
(jdbc/db-do-prepared conn (timezone->set-timezone-sql timezone))))
(jdbc/query conn sql :as-arrays? true))]
;; TODO - Why don't we just use annotate?
{:rows rows
:columns columns
:cols (map (fn [column first-value]
......
......@@ -22,17 +22,6 @@
(def ^:dynamic ^:private *query* nil)
(defn- uncastify
"Remove CAST statements from a column name if needed.
(uncastify \"DATE\") -> \"DATE\"
(uncastify \"CAST(DATE AS DATE)\") -> \"DATE\""
[driver column-name]
(let [column-name (name column-name)]
(keyword (or (second (re-find #"CAST\([^.\s]+\.([^.\s]+) AS [\w]+\)" column-name))
(second (re-find (:uncastify-timestamp-regex driver) column-name))
column-name))))
(defn process-structured
"Convert QUERY into a korma `select` form, execute it, and annotate the results."
[{{:keys [source-table]} :query, database :database, :as query}]
......@@ -57,9 +46,7 @@
(when (config/config-bool :mb-db-logging)
(log-korma-form korma-form))
(let [results (eval korma-form)]
{:results results
:uncastify-fn (partial uncastify (:driver query))}))
(eval korma-form))
(catch java.sql.SQLException e
(let [^String message (or (->> (.getMessage e) ; error message comes back like "Error message ... [status-code]" sometimes
......
......@@ -24,10 +24,9 @@
db->korma-db
"Return a Korma database definition for DATABASE.
This does a little bit of smart caching (for 60 seconds) to avoid creating new connections when unneeded."
(let [-db->korma-db (memo/ttl (fn [database]
(log/debug (color/red "Creating a new DB connection..."))
(kdb/create-db (db->connection-spec database)))
:ttl/threshold (* 60 1000))]
(let [-db->korma-db (memoize (fn [database]
(log/debug (color/red "Creating a new DB connection..."))
(kdb/create-db (db->connection-spec database))))]
;; only :engine and :details are needed for driver/connection so just pass those so memoization works as expected
(fn [database]
(-db->korma-db (select-keys database [:engine :details])))))
......
......@@ -89,9 +89,6 @@
(defn- cast-timestamp-milliseconds-field-to-date-fn [table-name field-name]
(format "CAST(TIMESTAMPADD('MILLISECOND', \"%s\".\"%s\", DATE '1970-01-01') AS DATE)" table-name field-name))
(def ^:private ^:const uncastify-timestamp-regex
#"CAST\(TIMESTAMPADD\('(?:MILLI)?SECOND', [^.\s]+\.([^.\s]+), DATE '1970-01-01'\) AS DATE\)")
;; ## DRIVER
(def driver
......@@ -101,5 +98,4 @@
:database->connection-details database->connection-details
:sql-string-length-fn :LENGTH
:cast-timestamp-seconds-field-to-date-fn cast-timestamp-seconds-field-to-date-fn
:cast-timestamp-milliseconds-field-to-date-fn cast-timestamp-milliseconds-field-to-date-fn
:uncastify-timestamp-regex uncastify-timestamp-regex}))
:cast-timestamp-milliseconds-field-to-date-fn cast-timestamp-milliseconds-field-to-date-fn}))
......@@ -45,10 +45,10 @@
(->> generated-query
(walk/postwalk #(if (symbol? %) (symbol (name %)) %)) ; strip namespace qualifiers from Monger form
u/pprint-to-str) "\n"))) ; so it's easier to read
{:results (eval generated-query)})
(eval generated-query))
:native (let [results (eval-raw-command (:query (:native query)))]
{:results (if (sequential? results) results
[results])}))))
(if (sequential? results) results
[results])))))
;; # NATIVE QUERY PROCESSOR
......
......@@ -118,10 +118,6 @@
(string? field-name)]}
(format "(TIMESTAMP WITH TIME ZONE 'epoch' + (\"%s\".\"%s\" * INTERVAL '1 millisecond'))::date" table-name field-name))
(def ^:private ^:const uncastify-timestamp-regex
;; TODO - this doesn't work
#"TO_TIMESTAMP\([^.\s]+\.([^.\s]+)(?: / 1000)?\)::date")
;; ## DRIVER
(def ^:const driver
......@@ -133,5 +129,4 @@
:sql-string-length-fn :CHAR_LENGTH
:timezone->set-timezone-sql timezone->set-timezone-sql
:cast-timestamp-seconds-field-to-date-fn cast-timestamp-seconds-field-to-date-fn
:cast-timestamp-milliseconds-field-to-date-fn cast-timestamp-milliseconds-field-to-date-fn
:uncastify-timestamp-regex uncastify-timestamp-regex}))
:cast-timestamp-milliseconds-field-to-date-fn cast-timestamp-milliseconds-field-to-date-fn}))
......@@ -210,7 +210,7 @@
(defn post-annotate [qp]
(fn [query]
(let [{:keys [results uncastify-fn]} (qp query)
(let [results (qp query)
cols (time (->> (query-add-info (:query query) results)
resolve+order-cols
(map format-col)
......
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