mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 10:46:00 +00:00
inline editing
This commit is contained in:
99
packages/datalib/src/ChangeSet.ts
Normal file
99
packages/datalib/src/ChangeSet.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
export interface ChangeSetItem {
|
||||
pureName: string;
|
||||
schemaName: string;
|
||||
insertedRowIndex?: number;
|
||||
condition?: { [column: string]: string };
|
||||
fields?: { [column: string]: string };
|
||||
}
|
||||
|
||||
export interface ChangeSet {
|
||||
inserts: ChangeSetItem[];
|
||||
updates: ChangeSetItem[];
|
||||
deletes: ChangeSetItem[];
|
||||
}
|
||||
|
||||
export function createChangeSet(): ChangeSet {
|
||||
return {
|
||||
inserts: [],
|
||||
updates: [],
|
||||
deletes: [],
|
||||
};
|
||||
}
|
||||
|
||||
export interface ChangeSetFieldDefinition {
|
||||
pureName: string;
|
||||
schemaName: string;
|
||||
uniqueName: string;
|
||||
columnName: string;
|
||||
insertedRowIndex?: number;
|
||||
condition?: { [column: string]: string };
|
||||
}
|
||||
|
||||
function findExistingChangeSetItem(
|
||||
changeSet: ChangeSet,
|
||||
definition: ChangeSetFieldDefinition
|
||||
): [keyof ChangeSet, ChangeSetItem] {
|
||||
if (definition.insertedRowIndex != null) {
|
||||
return [
|
||||
'inserts',
|
||||
changeSet.inserts.find(
|
||||
x =>
|
||||
x.pureName == definition.pureName &&
|
||||
x.schemaName == definition.schemaName &&
|
||||
x.insertedRowIndex == definition.insertedRowIndex
|
||||
),
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'updates',
|
||||
changeSet.updates.find(
|
||||
x =>
|
||||
x.pureName == definition.pureName &&
|
||||
x.schemaName == definition.schemaName &&
|
||||
_.isEqual(x.condition, definition.condition)
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export function setChangeSetValue(
|
||||
changeSet: ChangeSet,
|
||||
definition: ChangeSetFieldDefinition,
|
||||
value: string
|
||||
): ChangeSet {
|
||||
const [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition);
|
||||
if (existingItem) {
|
||||
return {
|
||||
...changeSet,
|
||||
[fieldName]: changeSet[fieldName].map(item =>
|
||||
item == existingItem
|
||||
? {
|
||||
...item,
|
||||
fields: {
|
||||
...item.fields,
|
||||
[definition.uniqueName]: value,
|
||||
},
|
||||
}
|
||||
: item
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...changeSet,
|
||||
[fieldName]: [
|
||||
...changeSet[fieldName],
|
||||
{
|
||||
pureName: definition.pureName,
|
||||
schemaName: definition.schemaName,
|
||||
condition: definition.condition,
|
||||
insertedRowIndex: definition.insertedRowIndex,
|
||||
fields: {
|
||||
[definition.uniqueName]: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { ForeignKeyInfo, TableInfo, ColumnInfo, DbType } from '@dbgate/types';
|
||||
import { parseFilter, getFilterType } from '@dbgate/filterparser';
|
||||
import { filterName } from './filterName';
|
||||
import { Select, Expression } from '@dbgate/sqltree';
|
||||
import { ChangeSetFieldDefinition } from './ChangeSet';
|
||||
|
||||
export interface DisplayColumn {
|
||||
schemaName: string;
|
||||
@@ -47,6 +48,8 @@ export abstract class GridDisplay {
|
||||
) {}
|
||||
abstract getPageQuery(offset: number, count: number): string;
|
||||
columns: DisplayColumn[];
|
||||
baseTable?: TableInfo;
|
||||
changeSetKeyFields: string[] = null;
|
||||
setColumnVisibility(uniquePath: string[], isVisible: boolean) {
|
||||
const uniqueName = uniquePath.join('.');
|
||||
if (uniquePath.length == 1) {
|
||||
@@ -350,4 +353,23 @@ export abstract class GridDisplay {
|
||||
});
|
||||
this.reload();
|
||||
}
|
||||
|
||||
getChangeSetCondition(row) {
|
||||
if (!this.changeSetKeyFields) return null;
|
||||
return _.pick(row, this.changeSetKeyFields);
|
||||
}
|
||||
|
||||
getChangeSetField(row, uniqueName): ChangeSetFieldDefinition {
|
||||
const col = this.columns.find(x => x.uniqueName == uniqueName);
|
||||
if (!col) return null;
|
||||
if (!this.baseTable) return null;
|
||||
if (this.baseTable.pureName != col.pureName || this.baseTable.schemaName != col.schemaName) return null;
|
||||
return {
|
||||
pureName: col.pureName,
|
||||
schemaName: col.schemaName,
|
||||
uniqueName: uniqueName,
|
||||
columnName: col.columnName,
|
||||
condition: this.getChangeSetCondition(row),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import _ from 'lodash'
|
||||
import _ from 'lodash';
|
||||
import { GridDisplay, combineReferenceActions } from './GridDisplay';
|
||||
import { Select, treeToSql, dumpSqlSelect } from '@dbgate/sqltree';
|
||||
import { TableInfo, EngineDriver } from '@dbgate/types';
|
||||
@@ -16,6 +16,10 @@ export class TableGridDisplay extends GridDisplay {
|
||||
) {
|
||||
super(config, setConfig, cache, setCache, getTableInfo);
|
||||
this.columns = this.getDisplayColumns(table, []);
|
||||
this.baseTable = table;
|
||||
this.changeSetKeyFields = table.primaryKey
|
||||
? table.primaryKey.columns.map(x => x.columnName)
|
||||
: table.columns.map(x => x.columnName);
|
||||
}
|
||||
|
||||
createSelect() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./GridDisplay";
|
||||
export * from "./GridConfig";
|
||||
export * from "./TableGridDisplay";
|
||||
export * from "./ChangeSet";
|
||||
export * from "./filterName";
|
||||
|
||||
Reference in New Issue
Block a user