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

fully qualify the names of fields in QP korma forms

parent 44d00c04
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@
database->connection-details
sql-string-length-fn
timezone->set-timezone-sql
;; These functions take a string name of a Field and return the raw SQL to select it as a DATE
;; 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
......
......@@ -87,13 +87,13 @@
(extend-protocol IGenericSQLFormattable
Field
(formatted [{:keys [field-name base-type special-type]}]
(formatted [{:keys [table-name field-name base-type special-type]}]
;; TODO - add Table names
(cond
(contains? #{:DateField :DateTimeField} base-type) `(raw ~(format "CAST(\"%s\" AS DATE)" field-name))
(= special-type :timestamp_seconds) `(raw ~((:cast-timestamp-seconds-field-to-date-fn qp/*driver*) field-name))
(= special-type :timestamp_milliseconds) `(raw ~((:cast-timestamp-milliseconds-field-to-date-fn qp/*driver*) field-name))
:else (keyword field-name)))
(contains? #{:DateField :DateTimeField} base-type) `(raw ~(format "CAST(\"%s\".\"%s\" AS DATE)" table-name field-name))
(= special-type :timestamp_seconds) `(raw ~((:cast-timestamp-seconds-field-to-date-fn qp/*driver*) table-name field-name))
(= special-type :timestamp_milliseconds) `(raw ~((:cast-timestamp-milliseconds-field-to-date-fn qp/*driver*) table-name field-name))
:else (keyword (format "%s.%s" table-name field-name))))
;; e.g. the ["aggregation" 0] fields we allow in order-by
OrderByAggregateField
......
......@@ -83,14 +83,14 @@
;; ## QP Functions
(defn- cast-timestamp-seconds-field-to-date-fn [field-name]
(format "CAST(TIMESTAMPADD('SECOND', \"%s\", DATE '1970-01-01') AS DATE)" field-name))
(defn- cast-timestamp-seconds-field-to-date-fn [table-name field-name]
(format "CAST(TIMESTAMPADD('SECOND', \"%s\".\"%s\", DATE '1970-01-01') AS DATE)" table-name field-name))
(defn- cast-timestamp-milliseconds-field-to-date-fn [field-name]
(format "CAST(TIMESTAMPADD('MILLISECOND', \"%s\", DATE '1970-01-01') AS DATE)" field-name))
(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]+), DATE '1970-01-01'\) AS DATE\)")
#"CAST\(TIMESTAMPADD\('(?:MILLI)?SECOND', [^.\s]+\.([^.\s]+), DATE '1970-01-01'\) AS DATE\)")
;; ## DRIVER
......
......@@ -108,16 +108,18 @@
(defn- timezone->set-timezone-sql [timezone]
(format "SET LOCAL timezone TO '%s';" timezone))
(defn- cast-timestamp-seconds-field-to-date-fn [field-name]
{:pre [(string? field-name)]}
(format "CAST(TO_TIMESTAMP(\"%s\") AS DATE)" field-name))
(defn- cast-timestamp-seconds-field-to-date-fn [table-name field-name]
{:pre [(string? table-name)
(string? field-name)]}
(format "CAST(TO_TIMESTAMP(\"%s\".\"%s\") AS DATE)" table-name field-name))
(defn- cast-timestamp-milliseconds-field-to-date-fn [field-name]
{:pre [(string? field-name)]}
(format "CAST(TO_TIMESTAMP(\"%s\" / 1000) AS DATE)" field-name))
(defn- cast-timestamp-milliseconds-field-to-date-fn [table-name field-name]
{:pre [(string? table-name)
(string? field-name)]}
(format "CAST(TO_TIMESTAMP(\"%s\".\"%s\" / 1000) AS DATE)" table-name field-name))
(def ^:private ^:const uncastify-timestamp-regex
#"CAST\(TO_TIMESTAMP\(([^\s+])(?: / 1000)?\) AS DATE\)")
#"CAST\(TO_TIMESTAMP\([^.\s]+\.([^.\s]+)(?: / 1000)?\) AS DATE\)")
;; ## DRIVER
......
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