Helpers for working with | (ns metabase.lib.metadata.ident (:require [clojure.string :as str])) |
Given the DB name and a | (defn table-prefix [db-name {tbl-name :name, :keys [schema] :as _table}] (str/join "__" ["field" db-name (or schema "!noschema!") tbl-name])) |
Given a database name, table with | (defn ident-for-field ([prefix {col-name :name :as _column}] (str prefix "__" col-name)) ([db-name table column] (ident-for-field (table-prefix db-name table) column))) |
Generates and attaches an Either takes a prefix for the database, schema and table, or the DB name and table. | (defn attach-ident ([prefix-or-fn column] (if (:ident column) column (let [prefix (if (ifn? prefix-or-fn) (prefix-or-fn column) prefix-or-fn)] (assoc column :ident (ident-for-field prefix column))))) ([db-name table column] (cond-> column (not (:ident column)) (assoc :ident (ident-for-field db-name table column))))) |
Generates and attaches an 2-arity version takes a function from table IDs to the 1-arity version takes the same function and returns a transducer. If you want to memoize or cache the calls to | (defn attach-idents ([prefix-fn] (map #(attach-ident (prefix-fn (:table-id %)) %))) ([prefix-fn columns] (sequence (attach-idents prefix-fn) columns))) |
Returns the ident for an implicitly joined column, given the idents of the foreign key column and the target column. Remember that | (defn implicitly-joined-ident [fk-ident target-ident] (str "implicit_via__" fk-ident "__->__" target-ident)) |
Returns the ident for an explicitly joined column, given the idents of the join clause and the target column. Remember that | (defn explicitly-joined-ident [join-ident target-ident] (str "join__" join-ident "__" target-ident)) |