dbgate-datalib 7.2.0 → 7.2.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.
@@ -1,6 +1,7 @@
1
1
  import { Command, Condition } from 'dbgate-sqltree';
2
2
  import type { NamedObjectInfo, DatabaseInfo, TableInfo, SqlDialect } from 'dbgate-types';
3
3
  import { JsonDataObjectUpdateCommand } from 'dbgate-tools';
4
+ export type ChangeSetValue = any;
4
5
  export interface ChangeSetItem {
5
6
  pureName: string;
6
7
  schemaName?: string;
@@ -8,13 +9,13 @@ export interface ChangeSetItem {
8
9
  existingRowIndex?: number;
9
10
  document?: any;
10
11
  condition?: {
11
- [column: string]: string;
12
+ [column: string]: ChangeSetValue;
12
13
  };
13
14
  fields?: {
14
- [column: string]: string;
15
+ [column: string]: ChangeSetValue;
15
16
  };
16
17
  insertIfNotExistsFields?: {
17
- [column: string]: string;
18
+ [column: string]: ChangeSetValue;
18
19
  };
19
20
  }
20
21
  export interface ChangeSetItemFields {
@@ -34,7 +35,7 @@ export interface ChangeSetRowDefinition {
34
35
  insertedRowIndex?: number;
35
36
  existingRowIndex?: number;
36
37
  condition?: {
37
- [column: string]: string;
38
+ [column: string]: ChangeSetValue;
38
39
  };
39
40
  }
40
41
  export interface ChangeSetFieldDefinition extends ChangeSetRowDefinition {
@@ -63,13 +64,13 @@ export declare function removeSchemaFromChangeSet(changeSet: ChangeSet): {
63
64
  existingRowIndex?: number;
64
65
  document?: any;
65
66
  condition?: {
66
- [column: string]: string;
67
+ [column: string]: any;
67
68
  };
68
69
  fields?: {
69
- [column: string]: string;
70
+ [column: string]: any;
70
71
  };
71
72
  insertIfNotExistsFields?: {
72
- [column: string]: string;
73
+ [column: string]: any;
73
74
  };
74
75
  }[];
75
76
  updates: {
@@ -79,13 +80,13 @@ export declare function removeSchemaFromChangeSet(changeSet: ChangeSet): {
79
80
  existingRowIndex?: number;
80
81
  document?: any;
81
82
  condition?: {
82
- [column: string]: string;
83
+ [column: string]: any;
83
84
  };
84
85
  fields?: {
85
- [column: string]: string;
86
+ [column: string]: any;
86
87
  };
87
88
  insertIfNotExistsFields?: {
88
- [column: string]: string;
89
+ [column: string]: any;
89
90
  };
90
91
  }[];
91
92
  deletes: {
@@ -95,13 +96,13 @@ export declare function removeSchemaFromChangeSet(changeSet: ChangeSet): {
95
96
  existingRowIndex?: number;
96
97
  document?: any;
97
98
  condition?: {
98
- [column: string]: string;
99
+ [column: string]: any;
99
100
  };
100
101
  fields?: {
101
- [column: string]: string;
102
+ [column: string]: any;
102
103
  };
103
104
  insertIfNotExistsFields?: {
104
- [column: string]: string;
105
+ [column: string]: any;
105
106
  };
106
107
  }[];
107
108
  structure?: TableInfo;
package/lib/ChangeSet.js CHANGED
@@ -13,6 +13,23 @@ function createChangeSet() {
13
13
  };
14
14
  }
15
15
  exports.createChangeSet = createChangeSet;
16
+ function normalizeChangeSetValueForCompare(value) {
17
+ if ((value === null || value === void 0 ? void 0 : value.$bigint) != null)
18
+ return value.$bigint;
19
+ if ((value === null || value === void 0 ? void 0 : value.$decimal) != null)
20
+ return value.$decimal;
21
+ if (lodash_1.default.isNumber(value) || lodash_1.default.isBoolean(value))
22
+ return value.toString();
23
+ return value;
24
+ }
25
+ function normalizeChangeSetConditionForCompare(condition) {
26
+ if (condition == null)
27
+ return condition;
28
+ return lodash_1.default.mapValues(condition, normalizeChangeSetValueForCompare);
29
+ }
30
+ function isSameChangeSetCondition(left, right) {
31
+ return lodash_1.default.isEqual(normalizeChangeSetConditionForCompare(left), normalizeChangeSetConditionForCompare(right));
32
+ }
16
33
  function findExistingChangeSetItem(changeSet, definition) {
17
34
  if (!changeSet || !definition)
18
35
  return ['updates', null];
@@ -28,13 +45,13 @@ function findExistingChangeSetItem(changeSet, definition) {
28
45
  const inUpdates = changeSet.updates.find(x => x.pureName == definition.pureName &&
29
46
  x.schemaName == definition.schemaName &&
30
47
  ((definition.existingRowIndex != null && x.existingRowIndex == definition.existingRowIndex) ||
31
- (definition.existingRowIndex == null && lodash_1.default.isEqual(x.condition, definition.condition))));
48
+ (definition.existingRowIndex == null && isSameChangeSetCondition(x.condition, definition.condition))));
32
49
  if (inUpdates)
33
50
  return ['updates', inUpdates];
34
51
  const inDeletes = changeSet.deletes.find(x => x.pureName == definition.pureName &&
35
52
  x.schemaName == definition.schemaName &&
36
53
  ((definition.existingRowIndex != null && x.existingRowIndex == definition.existingRowIndex) ||
37
- (definition.existingRowIndex == null && lodash_1.default.isEqual(x.condition, definition.condition))));
54
+ (definition.existingRowIndex == null && isSameChangeSetCondition(x.condition, definition.condition))));
38
55
  if (inDeletes)
39
56
  return ['deletes', inDeletes];
40
57
  return ['updates', null];
@@ -0,0 +1,37 @@
1
+ import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet';
2
+ import { DisplayColumn } from './GridDisplay';
3
+ import { DatabaseInfo, QueryResultColumn } from 'dbgate-types';
4
+ export interface QueryResultTableMapping {
5
+ pureName: string;
6
+ schemaName?: string;
7
+ keyColumns: string[];
8
+ keyDisplayColumns: {
9
+ displayName: string;
10
+ sourceColumnName: string;
11
+ }[];
12
+ }
13
+ export interface QueryResultColumnBaseMapping {
14
+ pureName: string;
15
+ schemaName?: string;
16
+ sourceColumnName: string;
17
+ }
18
+ export declare function getTableKey(schemaName: string, pureName: string): string;
19
+ export declare function createEditableQueryResultMappings(columns: QueryResultColumn[], dbinfo: DatabaseInfo): {
20
+ editableColumns: Set<string>;
21
+ tableMappings: {
22
+ [key: string]: QueryResultTableMapping;
23
+ };
24
+ columnBaseMappings: {
25
+ [columnName: string]: QueryResultColumnBaseMapping;
26
+ };
27
+ };
28
+ export declare function hasCompleteQueryResultKey(mapping: QueryResultTableMapping, row: any): boolean;
29
+ export declare function isEditableQueryResultColumn(columns: DisplayColumn[], tableMappings: {
30
+ [key: string]: QueryResultTableMapping;
31
+ }, uniqueName: string, row?: any): boolean;
32
+ export declare function getEditableQueryResultChangeSetField(columns: DisplayColumn[], tableMappings: {
33
+ [key: string]: QueryResultTableMapping;
34
+ }, row: any, uniqueName: string, insertedRowIndex: any, existingRowIndex?: any): ChangeSetFieldDefinition;
35
+ export declare function getEditableQueryResultChangeSetRowDefinitions(tableMappings: {
36
+ [key: string]: QueryResultTableMapping;
37
+ }, row: any, insertedRowIndex: any, existingRowIndex: any): ChangeSetRowDefinition[];
@@ -0,0 +1,144 @@
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.getEditableQueryResultChangeSetRowDefinitions = exports.getEditableQueryResultChangeSetField = exports.isEditableQueryResultColumn = exports.hasCompleteQueryResultKey = exports.createEditableQueryResultMappings = exports.getTableKey = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ function getTableKey(schemaName, pureName) {
9
+ return `${schemaName || ''}\n${pureName}`;
10
+ }
11
+ exports.getTableKey = getTableKey;
12
+ function namesEqual(left, right) {
13
+ return left != null && right != null && left.toLowerCase() == right.toLowerCase();
14
+ }
15
+ function findTable(dbinfo, schemaName, pureName) {
16
+ if (!(dbinfo === null || dbinfo === void 0 ? void 0 : dbinfo.tables))
17
+ return null;
18
+ if (schemaName) {
19
+ const schemaTable = dbinfo.tables.find(table => table.schemaName == schemaName && table.pureName == pureName) ||
20
+ dbinfo.tables.find(table => namesEqual(table.schemaName, schemaName) && namesEqual(table.pureName, pureName));
21
+ if (schemaTable)
22
+ return schemaTable;
23
+ const uniqueNameTables = dbinfo.tables.filter(table => namesEqual(table.pureName, pureName));
24
+ return uniqueNameTables.length == 1 ? uniqueNameTables[0] : null;
25
+ }
26
+ const tables = dbinfo.tables.filter(table => table.pureName == pureName);
27
+ if (tables.length == 1)
28
+ return tables[0];
29
+ const matchingTables = dbinfo.tables.filter(table => namesEqual(table.pureName, pureName));
30
+ return matchingTables.length == 1 ? matchingTables[0] : null;
31
+ }
32
+ function findColumnName(table, columnName) {
33
+ var _a, _b;
34
+ if (!columnName)
35
+ return columnName;
36
+ const exactColumn = (_a = table === null || table === void 0 ? void 0 : table.columns) === null || _a === void 0 ? void 0 : _a.find(column => column.columnName == columnName);
37
+ if (exactColumn)
38
+ return exactColumn.columnName;
39
+ const matchingColumns = ((_b = table === null || table === void 0 ? void 0 : table.columns) === null || _b === void 0 ? void 0 : _b.filter(column => namesEqual(column.columnName, columnName))) || [];
40
+ return matchingColumns.length == 1 ? matchingColumns[0].columnName : columnName;
41
+ }
42
+ function createEditableQueryResultMappings(columns, dbinfo) {
43
+ var _a, _b;
44
+ const editableColumns = new Set();
45
+ const tableMappings = {};
46
+ const columnBaseMappings = {};
47
+ const groups = lodash_1.default.groupBy((columns || []).filter(column => column.tableName && column.sourceColumnName), column => getTableKey(column.tableSchema, column.tableName));
48
+ for (const groupColumns of Object.values(groups)) {
49
+ const firstColumn = groupColumns[0];
50
+ const table = findTable(dbinfo, firstColumn.tableSchema, firstColumn.tableName);
51
+ const tableSchema = table ? table.schemaName : firstColumn.tableSchema;
52
+ const tableName = (table === null || table === void 0 ? void 0 : table.pureName) || firstColumn.tableName;
53
+ const normalizedColumns = groupColumns.map(column => (Object.assign(Object.assign({}, column), { sourceColumnName: findColumnName(table, column.sourceColumnName) })));
54
+ const sourceToDisplay = new Map(normalizedColumns.map(column => [column.sourceColumnName, column.columnName]));
55
+ const primaryKeyColumns = ((_b = (_a = table === null || table === void 0 ? void 0 : table.primaryKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(column => column.columnName)) ||
56
+ normalizedColumns.filter(column => column.isPrimaryKey).map(column => column.sourceColumnName);
57
+ const keyColumns = primaryKeyColumns.length > 0 && primaryKeyColumns.every(columnName => sourceToDisplay.has(columnName))
58
+ ? primaryKeyColumns
59
+ : [];
60
+ if (keyColumns.length == 0)
61
+ continue;
62
+ const mapping = {
63
+ pureName: tableName,
64
+ schemaName: tableSchema,
65
+ keyColumns,
66
+ keyDisplayColumns: keyColumns.map(sourceColumnName => ({
67
+ sourceColumnName,
68
+ displayName: sourceToDisplay.get(sourceColumnName),
69
+ })),
70
+ };
71
+ tableMappings[getTableKey(tableSchema, tableName)] = mapping;
72
+ for (const column of normalizedColumns) {
73
+ editableColumns.add(column.columnName);
74
+ columnBaseMappings[column.columnName] = {
75
+ pureName: tableName,
76
+ schemaName: tableSchema,
77
+ sourceColumnName: column.sourceColumnName,
78
+ };
79
+ }
80
+ }
81
+ return { editableColumns, tableMappings, columnBaseMappings };
82
+ }
83
+ exports.createEditableQueryResultMappings = createEditableQueryResultMappings;
84
+ function hasCompleteQueryResultKey(mapping, row) {
85
+ return mapping.keyDisplayColumns.every(keyColumn => {
86
+ const value = row === null || row === void 0 ? void 0 : row[keyColumn.displayName];
87
+ return value !== null && value !== undefined;
88
+ });
89
+ }
90
+ exports.hasCompleteQueryResultKey = hasCompleteQueryResultKey;
91
+ function getQueryResultCondition(mapping, row) {
92
+ const condition = {};
93
+ for (const keyColumn of mapping.keyDisplayColumns) {
94
+ condition[keyColumn.sourceColumnName] = row === null || row === void 0 ? void 0 : row[keyColumn.displayName];
95
+ }
96
+ return condition;
97
+ }
98
+ function isEditableQueryResultColumn(columns, tableMappings, uniqueName, row) {
99
+ const col = columns.find(column => column.uniqueName == uniqueName);
100
+ if (!(col === null || col === void 0 ? void 0 : col.queryResultEditable))
101
+ return false;
102
+ const mapping = tableMappings[getTableKey(col.schemaName, col.pureName)];
103
+ return !!mapping && hasCompleteQueryResultKey(mapping, row);
104
+ }
105
+ exports.isEditableQueryResultColumn = isEditableQueryResultColumn;
106
+ function getEditableQueryResultChangeSetField(columns, tableMappings, row, uniqueName, insertedRowIndex, existingRowIndex = null) {
107
+ if (insertedRowIndex != null || !isEditableQueryResultColumn(columns, tableMappings, uniqueName, row))
108
+ return null;
109
+ const col = columns.find(column => column.uniqueName == uniqueName);
110
+ if (!col)
111
+ return null;
112
+ const mapping = tableMappings[getTableKey(col.schemaName, col.pureName)];
113
+ if (!mapping)
114
+ return null;
115
+ if (!hasCompleteQueryResultKey(mapping, row))
116
+ return null;
117
+ const condition = getQueryResultCondition(mapping, row);
118
+ if (lodash_1.default.isEmpty(condition))
119
+ return null;
120
+ return {
121
+ pureName: mapping.pureName,
122
+ schemaName: mapping.schemaName,
123
+ existingRowIndex,
124
+ condition,
125
+ uniqueName,
126
+ columnName: col.sourceColumnName,
127
+ };
128
+ }
129
+ exports.getEditableQueryResultChangeSetField = getEditableQueryResultChangeSetField;
130
+ function getEditableQueryResultChangeSetRowDefinitions(tableMappings, row, insertedRowIndex, existingRowIndex) {
131
+ if (insertedRowIndex != null)
132
+ return [];
133
+ return Object.values(tableMappings)
134
+ .filter(mapping => hasCompleteQueryResultKey(mapping, row))
135
+ .map(mapping => {
136
+ return {
137
+ pureName: mapping.pureName,
138
+ schemaName: mapping.schemaName,
139
+ existingRowIndex,
140
+ condition: getQueryResultCondition(mapping, row),
141
+ };
142
+ });
143
+ }
144
+ exports.getEditableQueryResultChangeSetRowDefinitions = getEditableQueryResultChangeSetRowDefinitions;
@@ -27,6 +27,7 @@ export declare class FreeTableGridDisplay extends GridDisplay {
27
27
  isPersisted?: boolean;
28
28
  isSparse?: boolean;
29
29
  defaultValue?: string;
30
+ onUpdateExpression?: string;
30
31
  defaultConstraint?: string;
31
32
  columnComment?: string;
32
33
  isUnsigned?: boolean;
@@ -57,6 +58,7 @@ export declare class FreeTableGridDisplay extends GridDisplay {
57
58
  isPersisted?: boolean;
58
59
  isSparse?: boolean;
59
60
  defaultValue?: string;
61
+ onUpdateExpression?: string;
60
62
  defaultConstraint?: string;
61
63
  columnComment?: string;
62
64
  isUnsigned?: boolean;
@@ -4,24 +4,10 @@ import { DatabaseInfo, EngineDriver } from 'dbgate-types';
4
4
  export declare class JslGridDisplay extends GridDisplay {
5
5
  queryResultEditing: boolean;
6
6
  private queryResultTableMappings;
7
+ private queryResultColumnBaseMappings;
7
8
  constructor(jslid: any, structure: any, config: GridConfig, setConfig: ChangeConfigFunc, cache: GridCache, setCache: ChangeCacheFunc, rows: any, isDynamicStructure: boolean, supportsReload: boolean, editable?: boolean, driver?: EngineDriver, currentSettings?: any, queryResultEditing?: boolean, dbinfo?: DatabaseInfo);
8
- private getTableKey;
9
- private findTable;
10
9
  private getEditableQueryResultColumns;
11
- private hasCompleteQueryResultKey;
12
10
  isColumnEditable(uniqueName: string, row?: any): boolean;
13
- getChangeSetField(row: any, uniqueName: any, insertedRowIndex: any, existingRowIndex?: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetFieldDefinition | {
14
- pureName: string;
15
- schemaName: string;
16
- existingRowIndex: any;
17
- condition: {};
18
- uniqueName: any;
19
- columnName: string;
20
- };
21
- getChangeSetRowDefinitions(row: any, insertedRowIndex: any, existingRowIndex: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetRowDefinition[] | {
22
- pureName: string;
23
- schemaName: string;
24
- existingRowIndex: any;
25
- condition: {};
26
- }[];
11
+ getChangeSetField(row: any, uniqueName: any, insertedRowIndex: any, existingRowIndex?: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetFieldDefinition;
12
+ getChangeSetRowDefinitions(row: any, insertedRowIndex: any, existingRowIndex: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetRowDefinition[];
27
13
  }
@@ -8,12 +8,14 @@ const lodash_1 = __importDefault(require("lodash"));
8
8
  const GridDisplay_1 = require("./GridDisplay");
9
9
  const CollectionGridDisplay_1 = require("./CollectionGridDisplay");
10
10
  const dbgate_tools_1 = require("dbgate-tools");
11
+ const EditableQueryResultDisplay_1 = require("./EditableQueryResultDisplay");
11
12
  class JslGridDisplay extends GridDisplay_1.GridDisplay {
12
13
  constructor(jslid, structure, config, setConfig, cache, setCache, rows, isDynamicStructure, supportsReload, editable = false, driver = null, currentSettings = null, queryResultEditing = false, dbinfo = null) {
13
14
  var _a;
14
15
  super(config, setConfig, cache, setCache, driver, dbinfo, undefined, currentSettings);
15
16
  this.queryResultEditing = queryResultEditing;
16
17
  this.queryResultTableMappings = {};
18
+ this.queryResultColumnBaseMappings = {};
17
19
  this.filterable = true;
18
20
  this.sortable = true;
19
21
  this.supportsReload = supportsReload;
@@ -32,18 +34,21 @@ class JslGridDisplay extends GridDisplay_1.GridDisplay {
32
34
  ? this.getEditableQueryResultColumns(structure.columns, dbinfo)
33
35
  : new Set();
34
36
  this.columns = lodash_1.default.uniqBy((_a = structure.columns
35
- .map(col => ({
36
- columnName: col.columnName,
37
- headerText: col.columnName,
38
- uniqueName: col.columnName,
39
- uniquePath: [col.columnName],
40
- notNull: col.notNull,
41
- autoIncrement: col.autoIncrement,
42
- pureName: queryResultEditing ? col.tableName : null,
43
- schemaName: queryResultEditing ? col.tableSchema : null,
44
- sourceColumnName: col.sourceColumnName,
45
- queryResultEditable: queryResultEditableColumns.has(col.columnName),
46
- }))) === null || _a === void 0 ? void 0 : _a.map(col => (Object.assign(Object.assign({}, col), { isChecked: this.isColumnChecked(col) }))), col => col.uniqueName);
37
+ .map(col => {
38
+ const columnBaseMapping = this.queryResultColumnBaseMappings[col.columnName];
39
+ return {
40
+ columnName: col.columnName,
41
+ headerText: col.columnName,
42
+ uniqueName: col.columnName,
43
+ uniquePath: [col.columnName],
44
+ notNull: col.notNull,
45
+ autoIncrement: col.autoIncrement,
46
+ pureName: queryResultEditing ? (columnBaseMapping ? columnBaseMapping.pureName : col.tableName) : null,
47
+ schemaName: queryResultEditing ? (columnBaseMapping ? columnBaseMapping.schemaName : col.tableSchema) : null,
48
+ sourceColumnName: columnBaseMapping ? columnBaseMapping.sourceColumnName : col.sourceColumnName,
49
+ queryResultEditable: queryResultEditableColumns.has(col.columnName),
50
+ };
51
+ })) === null || _a === void 0 ? void 0 : _a.map(col => (Object.assign(Object.assign({}, col), { isChecked: this.isColumnChecked(col) }))), col => col.uniqueName);
47
52
  }
48
53
  if (structure === null || structure === void 0 ? void 0 : structure.__isDynamicStructure) {
49
54
  this.columns = (0, CollectionGridDisplay_1.analyseCollectionDisplayColumns)(rows, this);
@@ -55,113 +60,28 @@ class JslGridDisplay extends GridDisplay_1.GridDisplay {
55
60
  }
56
61
  this.formColumns = this.columns;
57
62
  }
58
- getTableKey(schemaName, pureName) {
59
- return `${schemaName || ''}\n${pureName}`;
60
- }
61
- findTable(dbinfo, schemaName, pureName) {
62
- if (!(dbinfo === null || dbinfo === void 0 ? void 0 : dbinfo.tables))
63
- return null;
64
- if (schemaName) {
65
- return dbinfo.tables.find(table => table.schemaName == schemaName && table.pureName == pureName);
66
- }
67
- const tables = dbinfo.tables.filter(table => table.pureName == pureName);
68
- return tables.length == 1 ? tables[0] : null;
69
- }
70
63
  getEditableQueryResultColumns(columns, dbinfo) {
71
- var _a, _b;
72
- const res = new Set();
73
- const groups = lodash_1.default.groupBy(columns.filter(column => column.tableName && column.sourceColumnName), column => this.getTableKey(column.tableSchema, column.tableName));
74
- for (const groupColumns of Object.values(groups)) {
75
- const firstColumn = groupColumns[0];
76
- const table = this.findTable(dbinfo, firstColumn.tableSchema, firstColumn.tableName);
77
- const sourceToDisplay = new Map(groupColumns.map(column => [column.sourceColumnName, column.columnName]));
78
- const primaryKeyColumns = ((_b = (_a = table === null || table === void 0 ? void 0 : table.primaryKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(column => column.columnName)) ||
79
- groupColumns.filter(column => column.isPrimaryKey).map(column => column.sourceColumnName);
80
- const keyColumns = primaryKeyColumns.length > 0 && primaryKeyColumns.every(columnName => sourceToDisplay.has(columnName))
81
- ? primaryKeyColumns
82
- : [];
83
- if (keyColumns.length == 0)
84
- continue;
85
- const mapping = {
86
- pureName: firstColumn.tableName,
87
- schemaName: firstColumn.tableSchema,
88
- keyColumns,
89
- keyDisplayColumns: keyColumns.map(sourceColumnName => ({
90
- sourceColumnName,
91
- displayName: sourceToDisplay.get(sourceColumnName),
92
- })),
93
- };
94
- this.queryResultTableMappings[this.getTableKey(firstColumn.tableSchema, firstColumn.tableName)] = mapping;
95
- for (const column of groupColumns) {
96
- res.add(column.columnName);
97
- }
98
- }
99
- return res;
100
- }
101
- hasCompleteQueryResultKey(mapping, row) {
102
- return mapping.keyDisplayColumns.every(keyColumn => {
103
- const value = row === null || row === void 0 ? void 0 : row[keyColumn.displayName];
104
- return value !== null && value !== undefined;
105
- });
64
+ const { editableColumns, tableMappings, columnBaseMappings } = (0, EditableQueryResultDisplay_1.createEditableQueryResultMappings)(columns, dbinfo);
65
+ this.queryResultTableMappings = tableMappings;
66
+ this.queryResultColumnBaseMappings = columnBaseMappings;
67
+ return editableColumns;
106
68
  }
107
69
  isColumnEditable(uniqueName, row) {
108
70
  if (!this.queryResultEditing)
109
71
  return super.isColumnEditable(uniqueName, row);
110
- const col = this.columns.find(column => column.uniqueName == uniqueName);
111
- if (!(col === null || col === void 0 ? void 0 : col.queryResultEditable))
112
- return false;
113
- const mapping = this.queryResultTableMappings[this.getTableKey(col.schemaName, col.pureName)];
114
- return !!mapping && this.hasCompleteQueryResultKey(mapping, row);
72
+ return (0, EditableQueryResultDisplay_1.isEditableQueryResultColumn)(this.columns, this.queryResultTableMappings, uniqueName, row);
115
73
  }
116
74
  getChangeSetField(row, uniqueName, insertedRowIndex, existingRowIndex = null, baseNameOmitable = false) {
117
75
  if (!this.queryResultEditing) {
118
76
  return super.getChangeSetField(row, uniqueName, insertedRowIndex, existingRowIndex, baseNameOmitable);
119
77
  }
120
- if (insertedRowIndex != null || !this.isColumnEditable(uniqueName, row))
121
- return null;
122
- const col = this.columns.find(column => column.uniqueName == uniqueName);
123
- if (!col)
124
- return null;
125
- const mapping = this.queryResultTableMappings[this.getTableKey(col.schemaName, col.pureName)];
126
- if (!mapping)
127
- return null;
128
- if (!this.hasCompleteQueryResultKey(mapping, row))
129
- return null;
130
- const condition = {};
131
- for (const keyColumn of mapping.keyDisplayColumns) {
132
- condition[keyColumn.sourceColumnName] = row === null || row === void 0 ? void 0 : row[keyColumn.displayName];
133
- }
134
- if (lodash_1.default.isEmpty(condition))
135
- return null;
136
- return {
137
- pureName: mapping.pureName,
138
- schemaName: mapping.schemaName,
139
- existingRowIndex,
140
- condition,
141
- uniqueName,
142
- columnName: col.sourceColumnName,
143
- };
78
+ return (0, EditableQueryResultDisplay_1.getEditableQueryResultChangeSetField)(this.columns, this.queryResultTableMappings, row, uniqueName, insertedRowIndex, existingRowIndex);
144
79
  }
145
80
  getChangeSetRowDefinitions(row, insertedRowIndex, existingRowIndex, baseNameOmitable = false) {
146
81
  if (!this.queryResultEditing) {
147
82
  return super.getChangeSetRowDefinitions(row, insertedRowIndex, existingRowIndex, baseNameOmitable);
148
83
  }
149
- if (insertedRowIndex != null)
150
- return [];
151
- return Object.values(this.queryResultTableMappings)
152
- .filter(mapping => this.hasCompleteQueryResultKey(mapping, row))
153
- .map(mapping => {
154
- const condition = {};
155
- for (const keyColumn of mapping.keyDisplayColumns) {
156
- condition[keyColumn.sourceColumnName] = row === null || row === void 0 ? void 0 : row[keyColumn.displayName];
157
- }
158
- return {
159
- pureName: mapping.pureName,
160
- schemaName: mapping.schemaName,
161
- existingRowIndex,
162
- condition,
163
- };
164
- });
84
+ return (0, EditableQueryResultDisplay_1.getEditableQueryResultChangeSetRowDefinitions)(this.queryResultTableMappings, row, insertedRowIndex, existingRowIndex);
165
85
  }
166
86
  }
167
87
  exports.JslGridDisplay = JslGridDisplay;
@@ -58,6 +58,7 @@ export declare class TableGridDisplay extends GridDisplay {
58
58
  isPersisted?: boolean;
59
59
  isSparse?: boolean;
60
60
  defaultValue?: string;
61
+ onUpdateExpression?: string;
61
62
  defaultConstraint?: string;
62
63
  columnComment?: string;
63
64
  isUnsigned?: boolean;
@@ -1,16 +1,28 @@
1
1
  import { GridDisplay, ChangeCacheFunc, ChangeConfigFunc } from './GridDisplay';
2
- import type { EngineDriver, ViewInfo, ColumnInfo, DatabaseInfo } from 'dbgate-types';
2
+ import type { EngineDriver, ViewInfo, ColumnInfo, DatabaseInfo, QueryResultColumn } from 'dbgate-types';
3
3
  import { GridConfig, GridCache } from './GridConfig';
4
4
  export declare class ViewGridDisplay extends GridDisplay {
5
5
  view: ViewInfo;
6
- constructor(view: ViewInfo, driver: EngineDriver, config: GridConfig, setConfig: ChangeConfigFunc, cache: GridCache, setCache: ChangeCacheFunc, dbinfo: DatabaseInfo, serverVersion: any, currentSettings: any);
7
- getDisplayColumns(view: ViewInfo): {
6
+ viewResultInfo: {
7
+ columns?: QueryResultColumn[];
8
+ };
9
+ private queryResultTableMappings;
10
+ private queryResultColumnBaseMappings;
11
+ constructor(view: ViewInfo, driver: EngineDriver, config: GridConfig, setConfig: ChangeConfigFunc, cache: GridCache, setCache: ChangeCacheFunc, dbinfo: DatabaseInfo, serverVersion: any, currentSettings: any, viewResultInfo?: {
12
+ columns?: QueryResultColumn[];
13
+ }, editableView?: boolean);
14
+ private getEditableQueryResultColumns;
15
+ getDisplayColumns(view: ViewInfo, resultColumnMap?: {
16
+ [columnName: string]: QueryResultColumn;
17
+ }, queryResultEditableColumns?: Set<string>): {
8
18
  isChecked: boolean;
9
19
  pureName: string;
10
20
  schemaName: string;
11
21
  headerText: string;
12
22
  uniqueName: string;
13
23
  uniquePath: string[];
24
+ sourceColumnName: string;
25
+ queryResultEditable: boolean;
14
26
  pairingId?: string;
15
27
  columnName: string;
16
28
  notNull?: boolean;
@@ -24,6 +36,7 @@ export declare class ViewGridDisplay extends GridDisplay {
24
36
  isPersisted?: boolean;
25
37
  isSparse?: boolean;
26
38
  defaultValue?: string;
39
+ onUpdateExpression?: string;
27
40
  defaultConstraint?: string;
28
41
  columnComment?: string;
29
42
  isUnsigned?: boolean;
@@ -35,12 +48,14 @@ export declare class ViewGridDisplay extends GridDisplay {
35
48
  contentHash?: string;
36
49
  engine?: string;
37
50
  }[];
38
- getDisplayColumn(view: ViewInfo, col: ColumnInfo): {
51
+ getDisplayColumn(view: ViewInfo, col: ColumnInfo, resultColumn?: QueryResultColumn, queryResultEditableColumns?: Set<string>): {
39
52
  pureName: string;
40
53
  schemaName: string;
41
54
  headerText: string;
42
55
  uniqueName: string;
43
56
  uniquePath: string[];
57
+ sourceColumnName: string;
58
+ queryResultEditable: boolean;
44
59
  pairingId?: string;
45
60
  columnName: string;
46
61
  notNull?: boolean;
@@ -54,6 +69,7 @@ export declare class ViewGridDisplay extends GridDisplay {
54
69
  isPersisted?: boolean;
55
70
  isSparse?: boolean;
56
71
  defaultValue?: string;
72
+ onUpdateExpression?: string;
57
73
  defaultConstraint?: string;
58
74
  columnComment?: string;
59
75
  isUnsigned?: boolean;
@@ -65,5 +81,8 @@ export declare class ViewGridDisplay extends GridDisplay {
65
81
  contentHash?: string;
66
82
  engine?: string;
67
83
  };
84
+ isColumnEditable(uniqueName: string, row?: any): boolean;
85
+ getChangeSetField(row: any, uniqueName: any, insertedRowIndex: any, existingRowIndex?: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetFieldDefinition;
86
+ getChangeSetRowDefinitions(row: any, insertedRowIndex: any, existingRowIndex: any, baseNameOmitable?: boolean): import("./ChangeSet").ChangeSetRowDefinition[];
68
87
  createSelect(options?: {}): import("dbgate-sqltree").Select;
69
88
  }
@@ -1,29 +1,67 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.ViewGridDisplay = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
4
8
  const GridDisplay_1 = require("./GridDisplay");
9
+ const EditableQueryResultDisplay_1 = require("./EditableQueryResultDisplay");
5
10
  class ViewGridDisplay extends GridDisplay_1.GridDisplay {
6
- constructor(view, driver, config, setConfig, cache, setCache, dbinfo, serverVersion, currentSettings) {
11
+ constructor(view, driver, config, setConfig, cache, setCache, dbinfo, serverVersion, currentSettings, viewResultInfo = null, editableView = false) {
12
+ var _a;
7
13
  super(config, setConfig, cache, setCache, driver, dbinfo, serverVersion, currentSettings);
8
14
  this.view = view;
9
- this.columns = this.getDisplayColumns(view);
15
+ this.viewResultInfo = viewResultInfo;
16
+ this.queryResultTableMappings = {};
17
+ this.queryResultColumnBaseMappings = {};
18
+ const effectiveEditable = editableView && ((_a = driver === null || driver === void 0 ? void 0 : driver.databaseEngineTypes) === null || _a === void 0 ? void 0 : _a.includes('sql')) && (driver === null || driver === void 0 ? void 0 : driver.supportsEditableQueryResults);
19
+ const queryResultEditableColumns = effectiveEditable
20
+ ? this.getEditableQueryResultColumns((viewResultInfo === null || viewResultInfo === void 0 ? void 0 : viewResultInfo.columns) || [], dbinfo)
21
+ : new Set();
22
+ const resultColumnMap = lodash_1.default.keyBy((viewResultInfo === null || viewResultInfo === void 0 ? void 0 : viewResultInfo.columns) || [], 'columnName');
23
+ this.columns = this.getDisplayColumns(view, resultColumnMap, queryResultEditableColumns);
10
24
  this.formColumns = this.columns;
11
25
  this.filterable = true;
12
26
  this.sortable = true;
13
27
  this.groupable = false;
14
- this.editable = false;
28
+ this.editable = effectiveEditable && this.columns.some(col => col.queryResultEditable);
29
+ if (effectiveEditable) {
30
+ this.allowInsert = false;
31
+ this.allowDelete = false;
32
+ this.allowStructureChange = false;
33
+ this.allowRowDocumentEdit = false;
34
+ }
15
35
  this.supportsReload = true;
16
36
  this.baseView = view;
17
37
  }
18
- getDisplayColumns(view) {
38
+ getEditableQueryResultColumns(columns, dbinfo) {
39
+ const { editableColumns, tableMappings, columnBaseMappings } = (0, EditableQueryResultDisplay_1.createEditableQueryResultMappings)(columns, dbinfo);
40
+ this.queryResultTableMappings = tableMappings;
41
+ this.queryResultColumnBaseMappings = columnBaseMappings;
42
+ return editableColumns;
43
+ }
44
+ getDisplayColumns(view, resultColumnMap = {}, queryResultEditableColumns = new Set()) {
19
45
  var _a, _b;
20
- return (((_b = (_a = view === null || view === void 0 ? void 0 : view.columns) === null || _a === void 0 ? void 0 : _a.map(col => this.getDisplayColumn(view, col))) === null || _b === void 0 ? void 0 : _b.map(col => (Object.assign(Object.assign({}, col), { isChecked: this.isColumnChecked(col) })))) || []);
46
+ return (((_b = (_a = view === null || view === void 0 ? void 0 : view.columns) === null || _a === void 0 ? void 0 : _a.map(col => this.getDisplayColumn(view, col, resultColumnMap[col.columnName], queryResultEditableColumns))) === null || _b === void 0 ? void 0 : _b.map(col => (Object.assign(Object.assign({}, col), { isChecked: this.isColumnChecked(col) })))) || []);
21
47
  }
22
- getDisplayColumn(view, col) {
48
+ getDisplayColumn(view, col, resultColumn = null, queryResultEditableColumns = new Set()) {
23
49
  const uniquePath = [col.columnName];
24
50
  const uniqueName = uniquePath.join('.');
25
- return Object.assign(Object.assign({}, col), { pureName: view.pureName, schemaName: view.schemaName, headerText: col.columnName, uniqueName,
26
- uniquePath });
51
+ const columnBaseMapping = this.queryResultColumnBaseMappings[col.columnName];
52
+ return Object.assign(Object.assign({}, col), { pureName: columnBaseMapping ? columnBaseMapping.pureName : (resultColumn === null || resultColumn === void 0 ? void 0 : resultColumn.tableName) || view.pureName, schemaName: columnBaseMapping ? columnBaseMapping.schemaName : (resultColumn === null || resultColumn === void 0 ? void 0 : resultColumn.tableSchema) || view.schemaName, headerText: col.columnName, uniqueName,
53
+ uniquePath, sourceColumnName: columnBaseMapping ? columnBaseMapping.sourceColumnName : resultColumn === null || resultColumn === void 0 ? void 0 : resultColumn.sourceColumnName, queryResultEditable: queryResultEditableColumns.has(col.columnName) });
54
+ }
55
+ isColumnEditable(uniqueName, row) {
56
+ if (!this.editable)
57
+ return false;
58
+ return (0, EditableQueryResultDisplay_1.isEditableQueryResultColumn)(this.columns, this.queryResultTableMappings, uniqueName, row);
59
+ }
60
+ getChangeSetField(row, uniqueName, insertedRowIndex, existingRowIndex = null, baseNameOmitable = false) {
61
+ return (0, EditableQueryResultDisplay_1.getEditableQueryResultChangeSetField)(this.columns, this.queryResultTableMappings, row, uniqueName, insertedRowIndex, existingRowIndex);
62
+ }
63
+ getChangeSetRowDefinitions(row, insertedRowIndex, existingRowIndex, baseNameOmitable = false) {
64
+ return (0, EditableQueryResultDisplay_1.getEditableQueryResultChangeSetRowDefinitions)(this.queryResultTableMappings, row, insertedRowIndex, existingRowIndex);
27
65
  }
28
66
  createSelect(options = {}) {
29
67
  const select = this.createSelectBase(this.view, this.view.columns, options);
package/lib/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from './TableGridDisplay';
6
6
  export * from './ViewGridDisplay';
7
7
  export * from './JslGridDisplay';
8
8
  export * from './QueryResultChangeSet';
9
+ export * from './EditableQueryResultDisplay';
9
10
  export * from './ChangeSet';
10
11
  export * from './MacroDefinition';
11
12
  export * from './runMacro';
package/lib/index.js CHANGED
@@ -22,6 +22,7 @@ __exportStar(require("./TableGridDisplay"), exports);
22
22
  __exportStar(require("./ViewGridDisplay"), exports);
23
23
  __exportStar(require("./JslGridDisplay"), exports);
24
24
  __exportStar(require("./QueryResultChangeSet"), exports);
25
+ __exportStar(require("./EditableQueryResultDisplay"), exports);
25
26
  __exportStar(require("./ChangeSet"), exports);
26
27
  __exportStar(require("./MacroDefinition"), exports);
27
28
  __exportStar(require("./runMacro"), exports);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "7.2.0",
2
+ "version": "7.2.1",
3
3
  "name": "dbgate-datalib",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -19,14 +19,14 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "date-fns": "^4.1.0",
22
- "dbgate-filterparser": "7.2.0",
23
- "dbgate-sqltree": "7.2.0",
24
- "dbgate-tools": "7.2.0",
22
+ "dbgate-filterparser": "7.2.1",
23
+ "dbgate-sqltree": "7.2.1",
24
+ "dbgate-tools": "7.2.1",
25
25
  "uuid": "^3.4.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^13.7.0",
29
- "dbgate-types": "7.2.0",
29
+ "dbgate-types": "7.2.1",
30
30
  "jest": "^28.1.3",
31
31
  "ts-jest": "^28.0.7",
32
32
  "typescript": "^4.4.3"