insert rows

This commit is contained in:
Jan Prochazka
2020-03-28 11:41:29 +01:00
parent 54976ca21f
commit 2107daf0f0
6 changed files with 85 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
import _ from 'lodash';
import { Command, Insert, Update, Delete, UpdateField, Condition } from '@dbgate/sqltree';
import { NamedObjectInfo } from '@dbgate/types';
export interface ChangeSetItem {
pureName: string;
@@ -189,3 +190,31 @@ export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: Chan
};
return changeSet;
}
export function getChangeSetInsertedRows(changeSet: ChangeSet, name?: NamedObjectInfo) {
if (!name) return [];
if (!changeSet) return [];
const rows = changeSet.inserts.filter(x => x.pureName == name.pureName && x.schemaName == name.schemaName);
const maxIndex = _.maxBy(rows, x => x.insertedRowIndex)?.insertedRowIndex;
if (maxIndex == null) return [];
const res = Array(maxIndex + 1).fill({});
for (const row of rows) {
res[row.insertedRowIndex] = row.fields;
}
return res;
}
export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectInfo): ChangeSet {
const insertedRows = getChangeSetInsertedRows(changeSet, name);
return {
...changeSet,
inserts: [
...changeSet.inserts,
{
...name,
insertedRowIndex: insertedRows.length,
fields: {},
},
],
};
}