-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
table.clj
164 lines (137 loc) · 5.72 KB
/
table.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns metabase.models.table
(:require [metabase
[db :as mdb]
[util :as u]]
[metabase.models
[database :refer [Database]]
[field :refer [Field]]
[field-values :refer [FieldValues]]
[humanization :as humanization]
[interface :as i]
[metric :refer [Metric retrieve-metrics]]
[permissions :as perms :refer [Permissions]]
[segment :refer [retrieve-segments Segment]]]
[toucan
[db :as db]
[models :as models]]))
;;; ----------------------------------------------- Constants + Entity -----------------------------------------------
(def ^:const visibility-types
"Valid values for `Table.visibility_type` (field may also be `nil`).
(Basically any non-nil value is a reason for hiding the table.)"
#{:hidden :technical :cruft})
(models/defmodel Table :metabase_table)
;;; --------------------------------------------------- Lifecycle ----------------------------------------------------
(defn- pre-insert [table]
(let [defaults {:display_name (humanization/name->human-readable-name (:name table))}]
(merge defaults table)))
(defn- pre-delete [{:keys [db_id schema id]}]
(db/delete! Segment :table_id id)
(db/delete! Metric :table_id id)
(db/delete! Field :table_id id)
(db/delete! 'Card :table_id id)
(db/delete! Permissions :object [:like (str (perms/object-path db_id schema id) "%")]))
(defn- perms-objects-set [table _]
#{(perms/object-path (:db_id table) (:schema table) (:id table))})
(u/strict-extend (class Table)
models/IModel
(merge models/IModelDefaults
{:hydration-keys (constantly [:table])
:types (constantly {:entity_type :keyword,
:visibility_type :keyword,
:description :clob,
:has_field_values :clob,
:fields_hash :clob})
:properties (constantly {:timestamped? true})
:pre-insert pre-insert
:pre-delete pre-delete})
i/IObjectPermissions
(merge i/IObjectPermissionsDefaults
{:can-read? (partial i/current-user-has-full-permissions? :read)
:can-write? i/superuser?
:perms-objects-set perms-objects-set}))
;;; --------------------------------------------------- Hydration ----------------------------------------------------
(defn fields
"Return the `FIELDS` belonging to a single TABLE."
[{:keys [id]}]
(db/select Field
:table_id id
:active true
:visibility_type [:not= "retired"]
{:order-by [[:position :asc] [:name :asc]]}))
(defn metrics
"Retrieve the `Metrics` for a single TABLE."
[{:keys [id]}]
(retrieve-metrics id :all))
(defn segments
"Retrieve the `Segments` for a single TABLE."
[{:keys [id]}]
(retrieve-segments id :all))
(defn field-values
"Return the `FieldValues` for all `Fields` belonging to a single TABLE."
{:hydrate :field_values, :arglists '([table])}
[{:keys [id]}]
(let [field-ids (db/select-ids Field
:table_id id
:visibility_type "normal"
{:order-by [[:position :asc] [:name :asc]]})]
(when (seq field-ids)
(db/select-field->field :field_id :values FieldValues, :field_id [:in field-ids]))))
(defn pk-field-id
"Return the ID of the primary key `Field` for TABLE."
{:hydrate :pk_field, :arglists '([table])}
[{:keys [id]}]
(db/select-one-id Field
:table_id id
:special_type (mdb/isa :type/PK)
:visibility_type [:not-in ["sensitive" "retired"]]))
(defn- with-objects [hydration-key fetch-objects-fn tables]
(let [table-ids (set (map :id tables))
table-id->objects (group-by :table_id (when (seq table-ids)
(fetch-objects-fn table-ids)))]
(for [table tables]
(assoc table hydration-key (get table-id->objects (:id table) [])))))
(defn with-segments
"Efficiently hydrate the `Segments` for a collection of TABLES."
{:batched-hydrate :segments}
[tables]
(with-objects :segments
(fn [table-ids]
(db/select Segment :table_id [:in table-ids], :archived false, {:order-by [[:name :asc]]}))
tables))
(defn with-metrics
"Efficiently hydrate the `Metrics` for a collection of TABLES."
{:batched-hydrate :metrics}
[tables]
(with-objects :metrics
(fn [table-ids]
(db/select Metric :table_id [:in table-ids], :archived false, {:order-by [[:name :asc]]}))
tables))
(defn with-fields
"Efficiently hydrate the `Fields` for a collection of TABLES."
{:batched-hydrate :fields}
[tables]
(with-objects :fields
(fn [table-ids]
(db/select Field
:active true
:table_id [:in table-ids]
:visibility_type [:not= "retired"]
{:order-by [[:position :asc] [:name :asc]]}))
tables))
;;; ------------------------------------------------ Convenience Fns -------------------------------------------------
(defn qualified-identifier
"Return a keyword identifier for TABLE in the form `:schema.table-name` (if the Table has a non-empty `:schema` field)
or `:table-name` (if the Table has no `:schema`)."
^clojure.lang.Keyword [{schema :schema, table-name :name}]
(keyword (str (when (seq schema)
(str schema \.))
table-name)))
(defn database
"Return the `Database` associated with this `Table`."
[table]
(Database (:db_id table)))
(defn table-id->database-id
"Retrieve the `Database` ID for the given table-id."
[table-id]
{:pre [(integer? table-id)]}
(db/select-one-field :db_id Table, :id table-id))