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

remove old util functions from generic sql

parent ffc6e31f
Branches
Tags
No related merge requests found
......@@ -84,44 +84,3 @@
:post [(map? %)]}
(korma-entity (or (sel :one Table :id table-id)
(throw (Exception. (format "Table with ID %d doesn't exist!" table-id))))))
(defn castify-field
"Wrap Field in a SQL `CAST` statement if needed (i.e., it's a `:DateTimeField`).
(castify :name :TextField nil) -> :name
(castify :date :DateTimeField nil) -> (raw \"CAST(\"date\" AS DATE)
(castify :timestamp :IntegerField :timestamp_seconds) -> (raw \"CAST(TO_TIMESTAMP(\"timestamp\") AS DATE))"
[field-name base-type special-type]
{:pre [(string? field-name)
(keyword? base-type)]}
(cond
(contains? #{:DateField :DateTimeField} base-type) `(korma/raw ~(format "CAST(\"%s\" AS DATE)" field-name))
(= special-type :timestamp_seconds) `(korma/raw ~((:cast-timestamp-seconds-field-to-date-fn qp/*driver*) field-name))
(= special-type :timestamp_milliseconds) `(korma/raw ~((:cast-timestamp-milliseconds-field-to-date-fn qp/*driver*) field-name))
:else (keyword field-name)))
(defn field-name+base-type->castified-key
"Like `castify-field`, but returns a keyword that should match the one returned in results."
[field-name field-base-type special-type]
{:pre [(string? field-name)
(keyword? field-base-type)]
:post [(keyword? %)]}
(keyword
(cond
(contains? #{:DateField :DateTimeField} field-base-type) (format "CAST(%s AS DATE)" field-name)
:else field-name)))
(defn field-id->kw
"Given a metabase `Field` ID, return a keyword for use in the Korma form (or a casted raw string for date fields)."
[field-id]
{:pre [(integer? field-id)]}
(if-let [{field-name :name, field-type :base_type, special-type :special_type} (sel :one [Field :name :base_type :special_type] :id field-id)]
(castify-field field-name field-type special-type)
(throw (Exception. (format "Field with ID %d doesn't exist!" field-id)))))
(def date-field-id?
"Does FIELD-ID correspond to a field that is a Date?"
(memoize ; memoize since the base_type of a Field isn't going to change
(fn [field-id]
(contains? #{:DateField :DateTimeField}
(sel :one :field [Field :base_type] :id field-id)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment