dbgate-datalib 5.0.8 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,959 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getTableChildPerspectiveNodes = exports.PerspectiveCustomJoinTreeNode = exports.PerspectiveTableReferenceNode = exports.PerspectiveTableNode = exports.PerspectiveTableColumnNode = exports.PerspectiveTreeNode = void 0;
7
+ const dbgate_tools_1 = require("dbgate-tools");
8
+ const PerspectiveConfig_1 = require("./PerspectiveConfig");
9
+ const isEqual_1 = __importDefault(require("lodash/isEqual"));
10
+ const compact_1 = __importDefault(require("lodash/compact"));
11
+ const uniq_1 = __importDefault(require("lodash/uniq"));
12
+ const flatten_1 = __importDefault(require("lodash/flatten"));
13
+ const uniqBy_1 = __importDefault(require("lodash/uniqBy"));
14
+ const sortBy_1 = __importDefault(require("lodash/sortBy"));
15
+ const cloneDeepWith_1 = __importDefault(require("lodash/cloneDeepWith"));
16
+ const findIndex_1 = __importDefault(require("lodash/findIndex"));
17
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
18
+ const dbgate_filterparser_1 = require("dbgate-filterparser");
19
+ // import { getPerspectiveDefaultColumns } from './getPerspectiveDefaultColumns';
20
+ const v1_1 = __importDefault(require("uuid/v1"));
21
+ // export function groupPerspectiveLoadProps(
22
+ // ...list: PerspectiveDataLoadPropsWithNode[]
23
+ // ): PerspectiveDataLoadPropsWithNode[] {
24
+ // const res: PerspectiveDataLoadPropsWithNode[] = [];
25
+ // for (const item of list) {
26
+ // const existing = res.find(
27
+ // x =>
28
+ // x.node == item.node &&
29
+ // x.props.schemaName == item.props.schemaName &&
30
+ // x.props.pureName == item.props.pureName &&
31
+ // _isEqual(x.props.bindingColumns, item.props.bindingColumns)
32
+ // );
33
+ // if (existing) {
34
+ // existing.props.bindingValues.push(...item.props.bindingValues);
35
+ // } else {
36
+ // res.push(_cloneDeep(item));
37
+ // }
38
+ // }
39
+ // return res;
40
+ // }
41
+ class PerspectiveTreeNode {
42
+ constructor(dbs, config, setConfig, parentNode, dataProvider, databaseConfig, designerId) {
43
+ this.dbs = dbs;
44
+ this.config = config;
45
+ this.setConfig = setConfig;
46
+ this.parentNode = parentNode;
47
+ this.dataProvider = dataProvider;
48
+ this.databaseConfig = databaseConfig;
49
+ this.designerId = designerId;
50
+ this.childNodesCache = null;
51
+ this.nodeConfig = config.nodes.find(x => x.designerId == designerId);
52
+ this.parentNodeConfig = parentNode === null || parentNode === void 0 ? void 0 : parentNode.nodeConfig;
53
+ }
54
+ get childNodes() {
55
+ if (!this.childNodesCache) {
56
+ this.childNodesCache = this.generateChildNodes();
57
+ }
58
+ return this.childNodesCache;
59
+ }
60
+ get fieldName() {
61
+ return this.codeName;
62
+ }
63
+ get headerTableAttributes() {
64
+ return null;
65
+ }
66
+ get dataField() {
67
+ return this.codeName;
68
+ }
69
+ get tableCode() {
70
+ return null;
71
+ }
72
+ get namedObject() {
73
+ return null;
74
+ }
75
+ get isRoot() {
76
+ return this.parentNode == null;
77
+ }
78
+ get rootNode() {
79
+ var _a;
80
+ if (this.isRoot)
81
+ return this;
82
+ return (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.rootNode;
83
+ }
84
+ get isSortable() {
85
+ return false;
86
+ }
87
+ matchChildRow(parentRow, childRow) {
88
+ return true;
89
+ }
90
+ hasTableCode(code) {
91
+ var _a;
92
+ return code == this.tableCode || ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.hasTableCode(code));
93
+ }
94
+ // get uniqueName() {
95
+ // if (this.parentNode) return `${this.parentNode.uniqueName}::${this.codeName}`;
96
+ // return this.codeName;
97
+ // }
98
+ get level() {
99
+ if (this.parentNode)
100
+ return this.parentNode.level + 1;
101
+ return 0;
102
+ }
103
+ get isExpanded() {
104
+ var _a, _b;
105
+ return (_b = (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.expandedColumns) === null || _b === void 0 ? void 0 : _b.includes(this.codeName);
106
+ }
107
+ get isCheckedColumn() {
108
+ var _a, _b;
109
+ if ((_b = (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.checkedColumns) === null || _b === void 0 ? void 0 : _b.includes(this.codeName))
110
+ return true;
111
+ return false;
112
+ }
113
+ get isChecked() {
114
+ return this.isCheckedColumn;
115
+ }
116
+ get isCheckedNode() {
117
+ var _a, _b;
118
+ return !!this.designerId && !!((_b = (_a = this.config.nodes) === null || _a === void 0 ? void 0 : _a.find(x => x.designerId == this.designerId)) === null || _b === void 0 ? void 0 : _b.isNodeChecked);
119
+ }
120
+ get isSecondaryChecked() {
121
+ return false;
122
+ }
123
+ get secondaryCheckable() {
124
+ return false;
125
+ }
126
+ get columnTitle() {
127
+ return this.title;
128
+ }
129
+ get filterType() {
130
+ return 'string';
131
+ }
132
+ get columnName() {
133
+ return null;
134
+ }
135
+ get customJoinConfig() {
136
+ return null;
137
+ }
138
+ get db() {
139
+ var _a, _b;
140
+ return (_b = (_a = this.dbs) === null || _a === void 0 ? void 0 : _a[this.databaseConfig.conid]) === null || _b === void 0 ? void 0 : _b[this.databaseConfig.database];
141
+ }
142
+ get isCircular() {
143
+ return false;
144
+ }
145
+ hasDesignerIdInIncestors(designerId) {
146
+ var _a;
147
+ if (designerId == this.designerId)
148
+ return true;
149
+ return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.hasDesignerIdInIncestors(designerId)) || false;
150
+ }
151
+ getChildMatchColumns() {
152
+ return [];
153
+ }
154
+ getParentMatchColumns() {
155
+ return [];
156
+ }
157
+ parseFilterCondition(source = null) {
158
+ return null;
159
+ }
160
+ get hasUncheckedNodeInPath() {
161
+ if (!this.parentNode)
162
+ return false;
163
+ if (!this.isCheckedNode)
164
+ return true;
165
+ return this.parentNode.hasUncheckedNodeInPath;
166
+ }
167
+ get childDataColumn() {
168
+ if (this.isCheckedColumn) {
169
+ return this.codeName;
170
+ }
171
+ return null;
172
+ }
173
+ toggleExpanded(value) {
174
+ this.includeInNodeSet('expandedColumns', value == null ? !this.isExpanded : value);
175
+ }
176
+ toggleChecked(value) {
177
+ this.includeInNodeSet('checkedColumns', value == null ? !this.isChecked : value);
178
+ }
179
+ toggleCheckedNode(value) {
180
+ this.setConfig(cfg => {
181
+ var _a;
182
+ const oldCheckedValue = (_a = cfg.nodes.find(x => x.designerId == this.designerId)) === null || _a === void 0 ? void 0 : _a.isNodeChecked;
183
+ const [cfgChanged, nodeCfg] = this.ensureNodeConfig(cfg);
184
+ const res = Object.assign(Object.assign({}, cfgChanged), { nodes: cfgChanged.nodes.map(node => node.designerId == (this.designerId || nodeCfg.designerId)
185
+ ? Object.assign(Object.assign({}, node), { isNodeChecked: value == null ? !oldCheckedValue : value }) : node) });
186
+ return res;
187
+ });
188
+ }
189
+ toggleSecondaryChecked(value) { }
190
+ createReferenceConfigColumns() {
191
+ return null;
192
+ }
193
+ ensureNodeConfig(cfg) {
194
+ var _a, _b;
195
+ let node = cfg.nodes.find(x => x.designerId == this.designerId);
196
+ if (!node) {
197
+ const nodeConfig = Object.assign(Object.assign({}, (0, PerspectiveConfig_1.createPerspectiveNodeConfig)(this.namedObject)), { isAutoGenerated: true, conid: (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.conid, database: (_b = this.parentNodeConfig) === null || _b === void 0 ? void 0 : _b.database });
198
+ const refConfig = {
199
+ designerId: (0, v1_1.default)(),
200
+ sourceId: this.parentNode.designerId,
201
+ targetId: nodeConfig.designerId,
202
+ isAutoGenerated: true,
203
+ columns: this.createReferenceConfigColumns(),
204
+ };
205
+ return [
206
+ Object.assign(Object.assign({}, cfg), { nodes: [...cfg.nodes, nodeConfig], references: [...cfg.references, refConfig] }),
207
+ nodeConfig,
208
+ ];
209
+ }
210
+ return [cfg, node];
211
+ }
212
+ includeInNodeSet(field, isIncluded) {
213
+ this.setConfig(cfg => {
214
+ var _a;
215
+ const changedFields = n => (Object.assign(Object.assign({}, n), { [field]: isIncluded ? [...(n[field] || []), this.codeName] : (n[field] || []).filter(x => x != this.codeName) }));
216
+ const [cfgChanged, nodeCfg] = (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.ensureNodeConfig(cfg);
217
+ return Object.assign(Object.assign({}, cfgChanged), { nodes: cfgChanged.nodes.map(n => { var _a; return n.designerId == (((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.designerId) || (nodeCfg === null || nodeCfg === void 0 ? void 0 : nodeCfg.designerId)) ? changedFields(n) : n; }) });
218
+ });
219
+ }
220
+ getFilter() {
221
+ var _a, _b;
222
+ return (_b = (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.filters) === null || _b === void 0 ? void 0 : _b[this.codeName];
223
+ }
224
+ getDataLoadColumns() {
225
+ return (0, compact_1.default)((0, uniq_1.default)([
226
+ ...this.childNodes.map(x => x.childDataColumn),
227
+ ...(0, flatten_1.default)(this.childNodes.filter(x => x.isExpandable && x.isChecked).map(x => x.getChildMatchColumns())),
228
+ ...this.getParentMatchColumns(),
229
+ ]));
230
+ }
231
+ getChildrenCondition(source = null) {
232
+ const conditions = (0, compact_1.default)([
233
+ ...this.childNodes.map(x => x.parseFilterCondition(source)),
234
+ ...this.buildParentFilterConditions(),
235
+ ]);
236
+ if (conditions.length == 0) {
237
+ return null;
238
+ }
239
+ if (conditions.length == 1) {
240
+ return conditions[0];
241
+ }
242
+ return {
243
+ conditionType: 'and',
244
+ conditions,
245
+ };
246
+ }
247
+ getOrderBy(table) {
248
+ var _a;
249
+ const res = (0, compact_1.default)(this.childNodes.map(node => {
250
+ var _a, _b;
251
+ const sort = (_b = (_a = this.nodeConfig) === null || _a === void 0 ? void 0 : _a.sort) === null || _b === void 0 ? void 0 : _b.find(x => x.columnName == node.columnName);
252
+ if (sort) {
253
+ return {
254
+ columnName: node.columnName,
255
+ order: sort.order,
256
+ };
257
+ }
258
+ }));
259
+ return res.length > 0
260
+ ? res
261
+ : ((_a = table === null || table === void 0 ? void 0 : table.primaryKey) === null || _a === void 0 ? void 0 : _a.columns.map(x => ({ columnName: x.columnName, order: 'ASC' }))) || [
262
+ { columnName: table === null || table === void 0 ? void 0 : table.columns[0].columnName, order: 'ASC' },
263
+ ];
264
+ }
265
+ getBaseTables() {
266
+ const res = [];
267
+ const table = this.getBaseTableFromThis();
268
+ if (table)
269
+ res.push({ table, node: this });
270
+ for (const child of this.childNodes) {
271
+ if (!child.isChecked)
272
+ continue;
273
+ res.push(...child.getBaseTables());
274
+ }
275
+ return res;
276
+ }
277
+ getBaseTableFromThis() {
278
+ return null;
279
+ }
280
+ get filterInfo() {
281
+ return null;
282
+ }
283
+ findChildNodeByUniquePath(uniquePath) {
284
+ if (uniquePath.length == 0) {
285
+ return this;
286
+ }
287
+ const child = this.childNodes.find(x => x.codeName == uniquePath[0]);
288
+ return child === null || child === void 0 ? void 0 : child.findChildNodeByUniquePath(uniquePath.slice(1));
289
+ }
290
+ // findNodeByUniqueName(uniqueName: string): PerspectiveTreeNode {
291
+ // if (!uniqueName) return null;
292
+ // const uniquePath = uniqueName.split('::');
293
+ // if (uniquePath[0] != this.codeName) return null;
294
+ // return this.findChildNodeByUniquePath(uniquePath.slice(1));
295
+ // }
296
+ findNodeByDesignerId(designerId) {
297
+ if (!this.designerId) {
298
+ return null;
299
+ }
300
+ if (!designerId) {
301
+ return null;
302
+ }
303
+ if (designerId == this.designerId) {
304
+ return this;
305
+ }
306
+ for (const child of this.childNodes) {
307
+ const res = child.findNodeByDesignerId(designerId);
308
+ if (res) {
309
+ return res;
310
+ }
311
+ }
312
+ return null;
313
+ }
314
+ get supportsParentFilter() {
315
+ var _a, _b, _c, _d, _e, _f, _g, _h;
316
+ return ((((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.isRoot) || ((_b = this.parentNode) === null || _b === void 0 ? void 0 : _b.supportsParentFilter)) &&
317
+ ((_d = (_c = this.parentNode) === null || _c === void 0 ? void 0 : _c.databaseConfig) === null || _d === void 0 ? void 0 : _d.conid) == ((_e = this.databaseConfig) === null || _e === void 0 ? void 0 : _e.conid) &&
318
+ ((_g = (_f = this.parentNode) === null || _f === void 0 ? void 0 : _f.databaseConfig) === null || _g === void 0 ? void 0 : _g.database) == ((_h = this.databaseConfig) === null || _h === void 0 ? void 0 : _h.database));
319
+ }
320
+ get isParentFilter() {
321
+ var _a;
322
+ return !!((_a = this.nodeConfig) === null || _a === void 0 ? void 0 : _a.isParentFilter);
323
+ }
324
+ buildParentFilterConditions() {
325
+ var _a;
326
+ const leafNodes = (0, compact_1.default)((((_a = this.config) === null || _a === void 0 ? void 0 : _a.nodes) || [])
327
+ .filter(x => x.isParentFilter)
328
+ .map(x => this.rootNode.findNodeByDesignerId(x.designerId)));
329
+ const conditions = (0, compact_1.default)(leafNodes.map(leafNode => {
330
+ var _a, _b;
331
+ if (leafNode == this)
332
+ return null;
333
+ const select = {
334
+ commandType: 'select',
335
+ from: {
336
+ name: leafNode.namedObject,
337
+ alias: 'pert_0',
338
+ relations: [],
339
+ },
340
+ selectAll: true,
341
+ };
342
+ let lastNode = leafNode;
343
+ let node = leafNode;
344
+ let index = 1;
345
+ let lastAlias = 'pert_0';
346
+ while ((node === null || node === void 0 ? void 0 : node.parentNode) && ((_a = node === null || node === void 0 ? void 0 : node.parentNode) === null || _a === void 0 ? void 0 : _a.designerId) != (this === null || this === void 0 ? void 0 : this.designerId)) {
347
+ node = node.parentNode;
348
+ let alias = `pert_${index}`;
349
+ select.from.relations.push({
350
+ joinType: 'INNER JOIN',
351
+ alias,
352
+ name: node.namedObject,
353
+ conditions: lastNode.getParentJoinCondition(lastAlias, alias),
354
+ });
355
+ lastAlias = alias;
356
+ lastNode = node;
357
+ index += 1;
358
+ }
359
+ if (((_b = node === null || node === void 0 ? void 0 : node.parentNode) === null || _b === void 0 ? void 0 : _b.designerId) != (this === null || this === void 0 ? void 0 : this.designerId))
360
+ return null;
361
+ select.where = {
362
+ conditionType: 'and',
363
+ conditions: (0, compact_1.default)([
364
+ ...lastNode.getParentJoinCondition(lastAlias, this.namedObject.pureName),
365
+ leafNode.getChildrenCondition({ alias: 'pert_0' }),
366
+ ]),
367
+ };
368
+ return {
369
+ conditionType: 'exists',
370
+ subQuery: select,
371
+ };
372
+ }));
373
+ return conditions;
374
+ }
375
+ getParentJoinCondition(alias, parentAlias) {
376
+ return [];
377
+ }
378
+ get sortOrder() {
379
+ var _a, _b, _c;
380
+ return (_c = (_b = (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.sort) === null || _b === void 0 ? void 0 : _b.find(x => x.columnName == this.columnName)) === null || _c === void 0 ? void 0 : _c.order;
381
+ }
382
+ get sortOrderIndex() {
383
+ var _a, _b, _c;
384
+ return ((_b = (_a = this.parentNodeConfig) === null || _a === void 0 ? void 0 : _a.sort) === null || _b === void 0 ? void 0 : _b.length) > 1
385
+ ? (0, findIndex_1.default)((_c = this.parentNodeConfig) === null || _c === void 0 ? void 0 : _c.sort, x => x.columnName == this.columnName)
386
+ : -1;
387
+ }
388
+ }
389
+ exports.PerspectiveTreeNode = PerspectiveTreeNode;
390
+ class PerspectiveTableColumnNode extends PerspectiveTreeNode {
391
+ constructor(column, table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId) {
392
+ var _a, _b, _c, _d, _e;
393
+ super(dbs, config, setConfig, parentNode, dataProvider, databaseConfig, designerId);
394
+ this.column = column;
395
+ this.table = table;
396
+ this.isTable = !!((_b = (_a = this.db) === null || _a === void 0 ? void 0 : _a.tables) === null || _b === void 0 ? void 0 : _b.find(x => x.schemaName == table.schemaName && x.pureName == table.pureName));
397
+ this.isView = !!((_d = (_c = this.db) === null || _c === void 0 ? void 0 : _c.views) === null || _d === void 0 ? void 0 : _d.find(x => x.schemaName == table.schemaName && x.pureName == table.pureName));
398
+ this.foreignKey = (_e = table === null || table === void 0 ? void 0 : table.foreignKeys) === null || _e === void 0 ? void 0 : _e.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == column.columnName);
399
+ this.refTable = this.db.tables.find(x => { var _a, _b; return x.pureName == ((_a = this.foreignKey) === null || _a === void 0 ? void 0 : _a.refTableName) && x.schemaName == ((_b = this.foreignKey) === null || _b === void 0 ? void 0 : _b.refSchemaName); });
400
+ }
401
+ matchChildRow(parentRow, childRow) {
402
+ if (!this.foreignKey)
403
+ return false;
404
+ return parentRow[this.foreignKey.columns[0].columnName] == childRow[this.foreignKey.columns[0].refColumnName];
405
+ }
406
+ getChildMatchColumns() {
407
+ if (!this.foreignKey)
408
+ return [];
409
+ return [this.foreignKey.columns[0].columnName];
410
+ }
411
+ getParentMatchColumns() {
412
+ if (!this.foreignKey)
413
+ return [];
414
+ return [this.foreignKey.columns[0].refColumnName];
415
+ }
416
+ getParentJoinCondition(alias, parentAlias) {
417
+ if (!this.foreignKey)
418
+ return [];
419
+ return this.foreignKey.columns.map(column => {
420
+ const res = {
421
+ conditionType: 'binary',
422
+ operator: '=',
423
+ left: {
424
+ exprType: 'column',
425
+ columnName: column.columnName,
426
+ source: { alias: parentAlias },
427
+ },
428
+ right: {
429
+ exprType: 'column',
430
+ columnName: column.refColumnName,
431
+ source: { alias },
432
+ },
433
+ };
434
+ return res;
435
+ });
436
+ }
437
+ createReferenceConfigColumns() {
438
+ var _a, _b;
439
+ return (_b = (_a = this.foreignKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(col => ({
440
+ source: col.columnName,
441
+ target: col.refColumnName,
442
+ }));
443
+ }
444
+ getNodeLoadProps(parentRows) {
445
+ if (!this.foreignKey)
446
+ return null;
447
+ return {
448
+ schemaName: this.foreignKey.refSchemaName,
449
+ pureName: this.foreignKey.refTableName,
450
+ bindingColumns: [this.foreignKey.columns[0].refColumnName],
451
+ bindingValues: (0, uniqBy_1.default)(parentRows.map(row => [row[this.foreignKey.columns[0].columnName]]), json_stable_stringify_1.default),
452
+ dataColumns: this.getDataLoadColumns(),
453
+ databaseConfig: this.databaseConfig,
454
+ orderBy: this.getOrderBy(this.refTable),
455
+ condition: this.getChildrenCondition(),
456
+ };
457
+ }
458
+ get icon() {
459
+ if (this.isCircular)
460
+ return 'img circular';
461
+ if (this.column.autoIncrement)
462
+ return 'img autoincrement';
463
+ if (this.foreignKey)
464
+ return 'img foreign-key';
465
+ return 'img column';
466
+ }
467
+ get codeName() {
468
+ return this.column.columnName;
469
+ }
470
+ get columnName() {
471
+ return this.column.columnName;
472
+ }
473
+ get fieldName() {
474
+ return this.codeName + 'Ref';
475
+ }
476
+ get title() {
477
+ return this.column.columnName;
478
+ }
479
+ get isExpandable() {
480
+ return !!this.foreignKey;
481
+ }
482
+ get isSortable() {
483
+ return true;
484
+ }
485
+ get filterType() {
486
+ return (0, dbgate_filterparser_1.getFilterType)(this.column.dataType);
487
+ }
488
+ get isCircular() {
489
+ var _a, _b;
490
+ return !!((_b = (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.hasTableCode(this.tableCode));
491
+ }
492
+ get isSecondaryChecked() {
493
+ return super.isCheckedColumn;
494
+ }
495
+ get isChecked() {
496
+ if (this.foreignKey)
497
+ return this.isCheckedNode;
498
+ return super.isCheckedColumn;
499
+ }
500
+ get secondaryCheckable() {
501
+ return !!this.foreignKey;
502
+ }
503
+ toggleChecked(value) {
504
+ if (this.foreignKey) {
505
+ this.toggleCheckedNode(value);
506
+ }
507
+ else {
508
+ super.toggleChecked(value);
509
+ }
510
+ }
511
+ toggleSecondaryChecked(value) {
512
+ super.toggleChecked(value == null ? !this.isSecondaryChecked : value);
513
+ }
514
+ generateChildNodes() {
515
+ var _a, _b;
516
+ if (!this.foreignKey)
517
+ return [];
518
+ const tbl = (_b = (_a = this === null || this === void 0 ? void 0 : this.db) === null || _a === void 0 ? void 0 : _a.tables) === null || _b === void 0 ? void 0 : _b.find(x => { var _a, _b; return x.pureName == ((_a = this.foreignKey) === null || _a === void 0 ? void 0 : _a.refTableName) && x.schemaName == ((_b = this.foreignKey) === null || _b === void 0 ? void 0 : _b.refSchemaName); });
519
+ return getTableChildPerspectiveNodes(tbl, this.dbs, this.config, this.setConfig, this.dataProvider, this.databaseConfig, this);
520
+ }
521
+ getBaseTableFromThis() {
522
+ return this.refTable;
523
+ }
524
+ get filterInfo() {
525
+ return {
526
+ columnName: this.columnName,
527
+ filterType: this.filterType,
528
+ pureName: this.column.pureName,
529
+ schemaName: this.column.schemaName,
530
+ foreignKey: this.foreignKey,
531
+ };
532
+ }
533
+ parseFilterCondition(source = null) {
534
+ const filter = this.getFilter();
535
+ if (!filter)
536
+ return null;
537
+ const condition = (0, dbgate_filterparser_1.parseFilter)(filter, this.filterType);
538
+ if (!condition)
539
+ return null;
540
+ return (0, cloneDeepWith_1.default)(condition, (expr) => {
541
+ if (expr.exprType == 'placeholder') {
542
+ return {
543
+ exprType: 'column',
544
+ columnName: this.column.columnName,
545
+ source,
546
+ };
547
+ }
548
+ });
549
+ }
550
+ get headerTableAttributes() {
551
+ if (this.foreignKey) {
552
+ return {
553
+ schemaName: this.foreignKey.refSchemaName,
554
+ pureName: this.foreignKey.refTableName,
555
+ conid: this.databaseConfig.conid,
556
+ database: this.databaseConfig.database,
557
+ };
558
+ }
559
+ return null;
560
+ }
561
+ get tableCode() {
562
+ if (this.foreignKey) {
563
+ return `${this.foreignKey.refSchemaName}|${this.foreignKey.refTableName}`;
564
+ }
565
+ return `${this.table.schemaName}|${this.table.pureName}`;
566
+ }
567
+ get namedObject() {
568
+ if (this.foreignKey) {
569
+ return {
570
+ schemaName: this.foreignKey.refSchemaName,
571
+ pureName: this.foreignKey.refTableName,
572
+ };
573
+ }
574
+ return null;
575
+ }
576
+ }
577
+ exports.PerspectiveTableColumnNode = PerspectiveTableColumnNode;
578
+ class PerspectiveTableNode extends PerspectiveTreeNode {
579
+ constructor(table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId) {
580
+ super(dbs, config, setConfig, parentNode, dataProvider, databaseConfig, designerId);
581
+ this.table = table;
582
+ this.dataProvider = dataProvider;
583
+ }
584
+ getNodeLoadProps(parentRows) {
585
+ return {
586
+ schemaName: this.table.schemaName,
587
+ pureName: this.table.pureName,
588
+ dataColumns: this.getDataLoadColumns(),
589
+ databaseConfig: this.databaseConfig,
590
+ orderBy: this.getOrderBy(this.table),
591
+ condition: this.getChildrenCondition(),
592
+ };
593
+ }
594
+ get codeName() {
595
+ return this.table.schemaName ? `${this.table.schemaName}:${this.table.pureName}` : this.table.pureName;
596
+ }
597
+ get title() {
598
+ var _a;
599
+ return ((_a = this.nodeConfig) === null || _a === void 0 ? void 0 : _a.alias) || this.table.pureName;
600
+ }
601
+ get isExpandable() {
602
+ return true;
603
+ }
604
+ generateChildNodes() {
605
+ return getTableChildPerspectiveNodes(this.table, this.dbs, this.config, this.setConfig, this.dataProvider, this.databaseConfig, this);
606
+ }
607
+ get icon() {
608
+ return 'img table';
609
+ }
610
+ getBaseTableFromThis() {
611
+ return this.table;
612
+ }
613
+ get headerTableAttributes() {
614
+ return {
615
+ schemaName: this.table.schemaName,
616
+ pureName: this.table.pureName,
617
+ conid: this.databaseConfig.conid,
618
+ database: this.databaseConfig.database,
619
+ };
620
+ }
621
+ get tableCode() {
622
+ return `${this.table.schemaName}|${this.table.pureName}`;
623
+ }
624
+ get namedObject() {
625
+ return {
626
+ schemaName: this.table.schemaName,
627
+ pureName: this.table.pureName,
628
+ };
629
+ }
630
+ }
631
+ exports.PerspectiveTableNode = PerspectiveTableNode;
632
+ // export class PerspectiveViewNode extends PerspectiveTreeNode {
633
+ // constructor(
634
+ // public view: ViewInfo,
635
+ // dbs: MultipleDatabaseInfo,
636
+ // config: PerspectiveConfig,
637
+ // setConfig: ChangePerspectiveConfigFunc,
638
+ // public dataProvider: PerspectiveDataProvider,
639
+ // databaseConfig: PerspectiveDatabaseConfig,
640
+ // parentNode: PerspectiveTreeNode
641
+ // ) {
642
+ // super(dbs, config, setConfig, parentNode, dataProvider, databaseConfig);
643
+ // }
644
+ // getNodeLoadProps(parentRows: any[]): PerspectiveDataLoadProps {
645
+ // return {
646
+ // schemaName: this.view.schemaName,
647
+ // pureName: this.view.pureName,
648
+ // dataColumns: this.getDataLoadColumns(),
649
+ // databaseConfig: this.databaseConfig,
650
+ // orderBy: this.getOrderBy(this.view),
651
+ // condition: this.getChildrenCondition(),
652
+ // };
653
+ // }
654
+ // get codeName() {
655
+ // return this.view.schemaName ? `${this.view.schemaName}:${this.view.pureName}` : this.view.pureName;
656
+ // }
657
+ // get title() {
658
+ // return this.view.pureName;
659
+ // }
660
+ // get isExpandable() {
661
+ // return true;
662
+ // }
663
+ // get childNodes(): PerspectiveTreeNode[] {
664
+ // return getTableChildPerspectiveNodes(
665
+ // this.view,
666
+ // this.dbs,
667
+ // this.config,
668
+ // this.setConfig,
669
+ // this.dataProvider,
670
+ // this.databaseConfig,
671
+ // this
672
+ // );
673
+ // }
674
+ // get icon() {
675
+ // return 'img table';
676
+ // }
677
+ // getBaseTableFromThis() {
678
+ // return this.view;
679
+ // }
680
+ // }
681
+ class PerspectiveTableReferenceNode extends PerspectiveTableNode {
682
+ constructor(foreignKey, table, dbs, config, setConfig, dataProvider, databaseConfig, isMultiple, parentNode, designerId) {
683
+ super(table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId);
684
+ this.foreignKey = foreignKey;
685
+ this.dataProvider = dataProvider;
686
+ this.isMultiple = isMultiple;
687
+ }
688
+ matchChildRow(parentRow, childRow) {
689
+ if (!this.foreignKey)
690
+ return false;
691
+ return parentRow[this.foreignKey.columns[0].refColumnName] == childRow[this.foreignKey.columns[0].columnName];
692
+ }
693
+ getChildMatchColumns() {
694
+ if (!this.foreignKey)
695
+ return [];
696
+ return [this.foreignKey.columns[0].refColumnName];
697
+ }
698
+ getParentMatchColumns() {
699
+ if (!this.foreignKey)
700
+ return [];
701
+ return [this.foreignKey.columns[0].columnName];
702
+ }
703
+ getNodeLoadProps(parentRows) {
704
+ if (!this.foreignKey)
705
+ return null;
706
+ return {
707
+ schemaName: this.table.schemaName,
708
+ pureName: this.table.pureName,
709
+ bindingColumns: [this.foreignKey.columns[0].columnName],
710
+ bindingValues: (0, uniqBy_1.default)(parentRows.map(row => [row[this.foreignKey.columns[0].refColumnName]]), json_stable_stringify_1.default),
711
+ dataColumns: this.getDataLoadColumns(),
712
+ databaseConfig: this.databaseConfig,
713
+ orderBy: this.getOrderBy(this.table),
714
+ condition: this.getChildrenCondition(),
715
+ };
716
+ }
717
+ createReferenceConfigColumns() {
718
+ var _a, _b;
719
+ return (_b = (_a = this.foreignKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(col => ({
720
+ source: col.refColumnName,
721
+ target: col.columnName,
722
+ }));
723
+ }
724
+ get columnTitle() {
725
+ return this.table.pureName;
726
+ }
727
+ get title() {
728
+ if (this.isMultiple) {
729
+ return `${super.title} (${this.foreignKey.columns.map(x => x.columnName).join(', ')})`;
730
+ }
731
+ return super.title;
732
+ }
733
+ get codeName() {
734
+ if (this.isMultiple) {
735
+ return `${super.codeName}-${this.foreignKey.columns.map(x => x.columnName).join('_')}`;
736
+ }
737
+ return super.codeName;
738
+ }
739
+ get isChecked() {
740
+ return this.isCheckedNode;
741
+ }
742
+ toggleChecked(value) {
743
+ this.toggleCheckedNode(value);
744
+ }
745
+ getParentJoinCondition(alias, parentAlias) {
746
+ if (!this.foreignKey)
747
+ return [];
748
+ return this.foreignKey.columns.map(column => {
749
+ const res = {
750
+ conditionType: 'binary',
751
+ operator: '=',
752
+ left: {
753
+ exprType: 'column',
754
+ columnName: column.refColumnName,
755
+ source: { alias: parentAlias },
756
+ },
757
+ right: {
758
+ exprType: 'column',
759
+ columnName: column.columnName,
760
+ source: { alias },
761
+ },
762
+ };
763
+ return res;
764
+ });
765
+ }
766
+ }
767
+ exports.PerspectiveTableReferenceNode = PerspectiveTableReferenceNode;
768
+ class PerspectiveCustomJoinTreeNode extends PerspectiveTableNode {
769
+ constructor(customJoin, table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId) {
770
+ super(table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId);
771
+ this.customJoin = customJoin;
772
+ this.dataProvider = dataProvider;
773
+ }
774
+ matchChildRow(parentRow, childRow) {
775
+ for (const column of this.customJoin.columns) {
776
+ if (parentRow[column.baseColumnName] != childRow[column.refColumnName]) {
777
+ return false;
778
+ }
779
+ }
780
+ return true;
781
+ }
782
+ getChildMatchColumns() {
783
+ return this.customJoin.columns.map(x => x.baseColumnName);
784
+ }
785
+ getParentMatchColumns() {
786
+ return this.customJoin.columns.map(x => x.refColumnName);
787
+ }
788
+ getNodeLoadProps(parentRows) {
789
+ // console.log('CUSTOM JOIN', this.customJoin);
790
+ // console.log('this.getDataLoadColumns()', this.getDataLoadColumns());
791
+ return {
792
+ schemaName: this.table.schemaName,
793
+ pureName: this.table.pureName,
794
+ bindingColumns: this.getParentMatchColumns(),
795
+ bindingValues: (0, uniqBy_1.default)(parentRows.map(row => this.customJoin.columns.map(x => row[x.baseColumnName])), json_stable_stringify_1.default),
796
+ dataColumns: this.getDataLoadColumns(),
797
+ databaseConfig: this.databaseConfig,
798
+ orderBy: this.getOrderBy(this.table),
799
+ condition: this.getChildrenCondition(),
800
+ };
801
+ }
802
+ // get title() {
803
+ // return this.customJoin.joinName || this.customJoin.refTableName;
804
+ // }
805
+ get icon() {
806
+ return 'icon custom-join';
807
+ }
808
+ get codeName() {
809
+ return this.customJoin.refNodeDesignerId;
810
+ }
811
+ get customJoinConfig() {
812
+ return this.customJoin;
813
+ }
814
+ get isChecked() {
815
+ return this.isCheckedNode;
816
+ }
817
+ toggleChecked(value) {
818
+ this.toggleCheckedNode(value);
819
+ }
820
+ getParentJoinCondition(alias, parentAlias) {
821
+ return this.customJoin.columns.map(column => {
822
+ const res = {
823
+ conditionType: 'binary',
824
+ operator: '=',
825
+ left: {
826
+ exprType: 'column',
827
+ columnName: column.baseColumnName,
828
+ source: { alias: parentAlias },
829
+ },
830
+ right: {
831
+ exprType: 'column',
832
+ columnName: column.refColumnName,
833
+ source: { alias },
834
+ },
835
+ };
836
+ return res;
837
+ });
838
+ }
839
+ }
840
+ exports.PerspectiveCustomJoinTreeNode = PerspectiveCustomJoinTreeNode;
841
+ function findDesignerIdForNode(config, parentNode, nodeCreateFunc) {
842
+ const node = nodeCreateFunc(null);
843
+ const refColumns = node.createReferenceConfigColumns();
844
+ if (!(refColumns === null || refColumns === void 0 ? void 0 : refColumns.length)) {
845
+ return node;
846
+ }
847
+ const ref1 = config.references.find(x => x.sourceId == parentNode.designerId &&
848
+ (0, isEqual_1.default)(refColumns.map(x => x.source), x.columns.map(x => x.source)) &&
849
+ (0, isEqual_1.default)(refColumns.map(x => x.target), x.columns.map(x => x.target)) &&
850
+ (0, dbgate_tools_1.equalFullName)(config.nodes.find(n => n.designerId == x.targetId), node.namedObject));
851
+ if (ref1 && !parentNode.hasDesignerIdInIncestors(ref1.targetId)) {
852
+ // console.log('FOUND1', node.title, ref1.targetId, refColumns);
853
+ return nodeCreateFunc(ref1.targetId);
854
+ }
855
+ const ref2 = config.references.find(x => x.targetId == parentNode.designerId &&
856
+ (0, isEqual_1.default)(refColumns.map(x => x.target), x.columns.map(x => x.source)) &&
857
+ (0, isEqual_1.default)(refColumns.map(x => x.source), x.columns.map(x => x.target)) &&
858
+ (0, dbgate_tools_1.equalFullName)(config.nodes.find(n => n.designerId == x.sourceId), node.namedObject));
859
+ if (ref2 && !parentNode.hasDesignerIdInIncestors(ref2.sourceId)) {
860
+ // console.log('FOUND2', node.title, ref2.sourceId, refColumns);
861
+ return nodeCreateFunc(ref2.sourceId);
862
+ }
863
+ return node;
864
+ }
865
+ function getTableChildPerspectiveNodes(table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode) {
866
+ var _a, _b, _c, _d;
867
+ if (!table)
868
+ return [];
869
+ const db = parentNode.db;
870
+ const columnNodes = table.columns.map(col => findDesignerIdForNode(config, parentNode, designerId => new PerspectiveTableColumnNode(col, table, dbs, config, setConfig, dataProvider, databaseConfig, parentNode, designerId)));
871
+ // if (!columnNodes.find(x => x.isChecked)) {
872
+ // const circularColumns = columnNodes.filter(x => x.isCircular).map(x => x.columnName);
873
+ // const defaultColumns = getPerspectiveDefaultColumns(table, db, circularColumns);
874
+ // for (const node of columnNodes) {
875
+ // if (defaultColumns.includes(node.columnName)) {
876
+ // no
877
+ // }
878
+ // }
879
+ // }
880
+ const res = [];
881
+ res.push(...columnNodes);
882
+ const dependencies = [];
883
+ if (db && (table === null || table === void 0 ? void 0 : table.dependencies)) {
884
+ for (const fk of table === null || table === void 0 ? void 0 : table.dependencies) {
885
+ const tbl = db.tables.find(x => x.pureName == fk.pureName && x.schemaName == fk.schemaName);
886
+ if (tbl) {
887
+ const isMultiple = (table === null || table === void 0 ? void 0 : table.dependencies.filter(x => x.pureName == fk.pureName && x.schemaName == fk.schemaName).length) >= 2;
888
+ dependencies.push(findDesignerIdForNode(config, parentNode, designerId => new PerspectiveTableReferenceNode(fk, tbl, dbs, config, setConfig, dataProvider, databaseConfig, isMultiple, parentNode, designerId)));
889
+ }
890
+ }
891
+ }
892
+ res.push(...(0, sortBy_1.default)(dependencies, 'title'));
893
+ const customs = [];
894
+ for (const node of config.nodes) {
895
+ if (node.designerId == ((_a = parentNode.parentNode) === null || _a === void 0 ? void 0 : _a.designerId) || res.find(x => x.designerId == node.designerId)) {
896
+ // already used as FK
897
+ continue;
898
+ }
899
+ for (const ref of config.references) {
900
+ if ((ref.sourceId == parentNode.designerId && ref.targetId == node.designerId) ||
901
+ (ref.targetId == parentNode.designerId && ref.sourceId == node.designerId)) {
902
+ const newConfig = Object.assign({}, databaseConfig);
903
+ if (node.conid)
904
+ newConfig.conid = node.conid;
905
+ if (node.database)
906
+ newConfig.database = node.database;
907
+ const db = (_b = dbs === null || dbs === void 0 ? void 0 : dbs[newConfig.conid]) === null || _b === void 0 ? void 0 : _b[newConfig.database];
908
+ const table = (_c = db === null || db === void 0 ? void 0 : db.tables) === null || _c === void 0 ? void 0 : _c.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
909
+ const view = (_d = db === null || db === void 0 ? void 0 : db.views) === null || _d === void 0 ? void 0 : _d.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
910
+ const join = {
911
+ refNodeDesignerId: node.designerId,
912
+ referenceDesignerId: ref.designerId,
913
+ baseDesignerId: parentNode.designerId,
914
+ joinName: node.alias,
915
+ refTableName: node.pureName,
916
+ refSchemaName: node.schemaName,
917
+ conid: node.conid,
918
+ database: node.database,
919
+ columns: ref.sourceId == parentNode.designerId
920
+ ? ref.columns.map(col => ({ baseColumnName: col.source, refColumnName: col.target }))
921
+ : ref.columns.map(col => ({ baseColumnName: col.target, refColumnName: col.source })),
922
+ };
923
+ if (table || view) {
924
+ customs.push(new PerspectiveCustomJoinTreeNode(join, table || view, dbs, config, setConfig, dataProvider, newConfig, parentNode, node.designerId));
925
+ }
926
+ }
927
+ }
928
+ }
929
+ res.push(...(0, sortBy_1.default)(customs, 'title'));
930
+ // const customs = [];
931
+ // for (const join of config.customJoins || []) {
932
+ // if (join.baseUniqueName == parentColumn.uniqueName) {
933
+ // const newConfig = { ...databaseConfig };
934
+ // if (join.conid) newConfig.conid = join.conid;
935
+ // if (join.database) newConfig.database = join.database;
936
+ // const db = dbs?.[newConfig.conid]?.[newConfig.database];
937
+ // const table = db?.tables?.find(x => x.pureName == join.refTableName && x.schemaName == join.refSchemaName);
938
+ // const view = db?.views?.find(x => x.pureName == join.refTableName && x.schemaName == join.refSchemaName);
939
+ // if (table || view) {
940
+ // customs.push(
941
+ // new PerspectiveCustomJoinTreeNode(
942
+ // join,
943
+ // table || view,
944
+ // dbs,
945
+ // config,
946
+ // setConfig,
947
+ // dataProvider,
948
+ // newConfig,
949
+ // parentColumn,
950
+ // null
951
+ // )
952
+ // );
953
+ // }
954
+ // }
955
+ // }
956
+ // res.push(..._sortBy(customs, 'title'));
957
+ return res;
958
+ }
959
+ exports.getTableChildPerspectiveNodes = getTableChildPerspectiveNodes;