-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtables.js
416 lines (352 loc) · 11.5 KB
/
tables.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
api.factory('Table', ["SQLClient", "columns", "constraints", "indexes", "records", "triggers", function (SQLClient, columns, constraints, indexes, records, triggers) {
return function (tableFromDB) {
let self = this;
angular.extend(this, tableFromDB);
this.api = new SQLClient();
this.columns = null;
this.constraints = null;
this.indexes = null;
this.records = null;
this.triggers = null;
this.orders = {};
this.orderByJS = function (items, parameter, orderName) {
if (self.orders[orderName] == "asc") {
self.orders[orderName] = "desc";
items.sort(function (a, b) {
if (a[parameter] > b[parameter]) return -1;
if (a[parameter] < b[parameter]) return 1;
return 0;
});
} else {
self.orders[orderName] = "asc";
items.sort(function (a, b) {
if (a[parameter] < b[parameter]) return -1;
if (a[parameter] > b[parameter]) return 1;
return 0;
});
}
};
this.getColumns = function (action, error) {
let _action = function () {
self.columns = columns.api.items;
if (action) {
action();
}
};
columns.get(self, _action, error);
};
this.orderColumns = function (parameter) {
self.orderByJS(self.columns, parameter, "columns");
};
this.getConstraints = function (action, error) {
let _action = function () {
self.constraints = constraints.api.items;
if (action) {
action();
}
};
constraints.get(self, _action, error);
};
this.orderConstraints = function (parameter) {
self.orderByJS(self.constraints, parameter, "constraints");
};
this.getIndexes = function (action, error) {
let _action = function () {
self.indexes = indexes.api.items;
if (action) {
action();
}
};
indexes.get(self, _action, error);
};
this.orderIndexes = function (parameter) {
self.orderByJS(self.indexes, parameter, "indexes");
};
this.getRecords = function (limit, offset, action, error, extraQuery) {
let _action = function () {
self.records = records.api.items;
if (action) {
action();
}
};
records.get(self, limit, offset, _action, error, extraQuery);
};
this.orderRecords = function (parameter, limit) {
if (self.orders.records == "asc") {
self.orders.records = "desc";
self.getRecords(limit, 0, null, null, "order by " + parameter + " desc");
} else {
self.orders.records = "asc";
self.getRecords(limit, 0, null, null, "order by " + parameter);
}
};
this.getTriggers = function (action, error) {
let _action = function () {
self.triggers = triggers.api.items;
if (action) {
action();
}
};
triggers.get(self, _action, error);
};
this.orderTriggers = function (parameter) {
self.orderByJS(self.triggers, parameter, "triggers");
};
};
}]);
cdbmanager.service("tables", ["SQLClient", "Table", "settings", function (SQLClient, Table, settings) {
let self = this;
this.api = new SQLClient();
this.current = null;
let order = null;
this.get = function (action, error, extraQuery) {
let query = `
select
pg_class.oid as _oid,
pg_class.relname,
pg_class.reltuples,
pg_namespace.nspname,
pg_class.relkind
from pg_class, pg_roles, pg_namespace
where
pg_roles.oid = pg_class.relowner and
pg_roles.rolname = current_user and
pg_namespace.oid = pg_class.relnamespace and
pg_class.relkind IN ('r', 'm', 'v', 'p') and
nspname = current_schema
`;
if (!settings.showAnalysisTables) {
query += " and pg_class.relname not like 'analysis_%'";
}
if (!settings.showOverviewTables) {
query += " and pg_class.relname not like '_vovw_%'";
}
let _action = function () {
order = null;
if (self.api && self.api.items) {
for (let i = 0; i < self.api.items.length; i++) {
self.api.items[i] = new Table(self.api.items[i], self);
}
}
if (action) {
action();
}
};
if (extraQuery) {
query += " " + extraQuery;
}
self.api.send(query, _action, error);
};
this.order = function (parameter, pageSize, newOrder) {
// pageSize needs to be there because it might be sent from a result table, but we don't really need it here
if (self.api && self.api.items) {
if (newOrder == "desc" || (!newOrder && order == "asc")) {
order = "desc";
self.api.items.sort(function (a, b) {
if (a[parameter] > b[parameter]) return -1;
if (a[parameter] < b[parameter]) return 1;
return 0;
});
} else {
order = "asc";
self.api.items.sort(function (a, b) {
if (a[parameter] < b[parameter]) return -1;
if (a[parameter] > b[parameter]) return 1;
return 0;
});
}
}
};
}]);
cdbmanager.controller('tableSelectorCtrl', ["$scope", "tables", "endpoints", "nav", "settings", function ($scope, tables, endpoints, nav, settings) {
$scope.nav = nav;
$scope.currentTable = null;
$scope.showTable = function (table) {
tables.current = table;
};
$scope.refreshList = function () {
tables.get(function () {
tables.order("relname", null, "asc");
});
};
// update table list in scope when current endpoint changes
$scope.$watch(function () {
return endpoints.current;
}, function () {
$scope.tables = $scope.refreshList();
}, true);
// update table list in scope when show analysis tables settings change
$scope.$watch(function () {
return settings.showAnalysisTables;
}, function () {
$scope.tables = $scope.refreshList();
});
// update table list in scope when show overview tables settings change
$scope.$watch(function () {
return settings.showOverviewTables;
}, function () {
$scope.tables = $scope.refreshList();
});
// update table list in scope when actual table list changes
$scope.$watch(function () {
return tables.api.items;
}, function (tableList) {
$scope.tables = tableList;
});
// update current table pointer in scope when a new table is selected
$scope.$watch(function () {
return tables.current;
}, function (currentTable) {
$scope.currentTable = currentTable;
});
}]);
cdbmanager.controller('tablesCtrl', ["$scope", "tables", "endpoints", "nav", "settings", function ($scope, tables, endpoints, nav, settings) {
$scope.nav = nav;
// Config result table
$scope.cdbrt = {
id: "tables",
rowsPerPage: settings.rowsPerPage,
headers: [{
name: 'nspname',
title: 'Schema'
},
{
name: 'relname',
title: 'Name'
},
{
name: 'reltuples',
title: 'Estimated row count'
}
],
skip: ["columns", "constraints", "triggers", "records", "indexes", "orders"],
actions: [{
text: "Details",
onClick: function (table) {
nav.setCurrentView("table.columns");
tables.current = table;
}
}],
orderBy: tables.order
};
// update number of rows per page when settings changes
$scope.$watch(function () {
return settings.rowsPerPage;
}, function () {
$scope.cdbrt.rowsPerPage = settings.rowsPerPage;
});
// update table list in scope when actual table list changes
$scope.$watch(function () {
return tables.api.items;
}, function (tableList) {
$scope.tables = tableList;
});
}]);
cdbmanager.controller('tableCtrl', ["$scope", "nav", "tables", "columns", "indexes", "triggers", "constraints", "records", "endpoints", "settings", function ($scope, nav, tables, columns, indexes, triggers, constraints, records, endpoints, settings) {
$scope.nav = nav;
$scope.currentTable = null;
// Settings for the result tables
$scope.cdbrt4Columns = {
id: "columns",
rowsPerPage: settings.rowsPerPage
};
$scope.cdbrt4Indexes = {
id: "indexes",
rowsPerPage: settings.rowsPerPage
};
$scope.cdbrt4Triggers = {
id: "triggers",
rowsPerPage: settings.rowsPerPage
};
$scope.cdbrt4Constraints = {
id: "constraints",
rowsPerPage: settings.rowsPerPage
};
$scope.cdbrt4Records = {
id: "records",
rowsPerPage: settings.rowsPerPage,
async: function (limit, offset) {
if ($scope.currentTable) {
$scope.currentTable.getRecords(limit, (offset - limit));
}
}
};
// update number of rows per page when settings changes
$scope.$watch(function () {
return settings.rowsPerPage;
}, function () {
$scope.cdbrt4Columns.rowsPerPage = settings.rowsPerPage;
$scope.cdbrt4Indexes.rowsPerPage = settings.rowsPerPage;
$scope.cdbrt4Triggers.rowsPerPage = settings.rowsPerPage;
$scope.cdbrt4Constraints.rowsPerPage = settings.rowsPerPage;
$scope.cdbrt4Records.rowsPerPage = settings.rowsPerPage;
});
// update current table pointer in scope when a new table is selected
$scope.$watch(function () {
return tables.current;
}, function (currentTable) {
$scope.currentTable = currentTable;
if ($scope.currentTable) {
nav.setCurrentView("table.columns");
$scope.currentTable.getColumns();
$scope.pagination = {
current: 1
};
// Update settings for the result tables
$scope.cdbrt4Columns.orderBy = currentTable.orderColumns;
$scope.cdbrt4Indexes.orderBy = currentTable.orderIndexes;
$scope.cdbrt4Triggers.orderBy = currentTable.orderTriggers;
$scope.cdbrt4Constraints.orderBy = currentTable.orderConstraints;
$scope.cdbrt4Records.orderBy = currentTable.orderRecords;
}
});
// Synchronize columns in scope with actual columns
$scope.$watch(function () {
return $scope.currentTable ? $scope.currentTable.columns : null;
}, function (columns) {
$scope.columns = columns;
});
// Synchronize constraints in scope with actual constraints
$scope.$watch(function () {
return $scope.currentTable ? $scope.currentTable.constraints : null;
}, function (constraints) {
$scope.constraints = constraints;
});
// Synchronize triggers in scope with actual triggers
$scope.$watch(function () {
return $scope.currentTable ? $scope.currentTable.triggers : null;
}, function (triggers) {
$scope.triggers = triggers;
});
// Synchronize records in scope with actual records
$scope.$watch(function () {
return $scope.currentTable ? $scope.currentTable.records : null;
}, function (records) {
$scope.records = records;
});
// Synchronize indexes in scope with actual indexes
$scope.$watch(function () {
return $scope.currentTable ? $scope.currentTable.indexes : null;
}, function (indexes) {
$scope.indexes = indexes;
});
// Force data refresh when a tab is selected
$scope.$watch(function () {
return nav.current;
}, function () {
$scope.pagination = {
current: 1
};
if (nav.isCurrentView("table.columns")) {
$scope.currentTable.getColumns();
} else if (nav.isCurrentView("table.indexes")) {
$scope.currentTable.getIndexes();
} else if (nav.isCurrentView("table.records")) {
$scope.currentTable.getRecords($scope.cdbrt4Records.rowsPerPage);
} else if (nav.isCurrentView("table.constraints")) {
$scope.currentTable.getConstraints();
} else if (nav.isCurrentView("table.triggers")) {
$scope.currentTable.getTriggers();
}
});
}]);