feat: delete rows from tables without a primary key

This commit is contained in:
Fabio Di Stasio 2021-01-28 18:33:29 +01:00
parent af96647603
commit 574d493908
2 changed files with 51 additions and 32 deletions

View File

@ -92,13 +92,11 @@ export default (connections) => {
} }
}); });
ipcMain.handle('delete-table-rows', async (event, params) => { ipcMain.handle('delete-table-rows', async (event, params) => { // TODO: check primary or try other
let idString; if (params.primary) {
const idString = params.rows.map(row => typeof row[params.primary] === 'string'
if (typeof params.rows[0] === 'string') ? `"${row[params.primary]}"`
idString = params.rows.map(row => `"${row}"`).join(','); : row[params.primary]).join(',');
else
idString = params.rows.join(',');
try { try {
const result = await connections[params.uid] const result = await connections[params.uid]
@ -112,6 +110,31 @@ export default (connections) => {
catch (err) { catch (err) {
return { status: 'error', response: err.toString() }; return { status: 'error', response: err.toString() };
} }
}
else {
try {
for (const row of params.rows) {
for (const key in row) {
if (typeof row[key] === 'string')
row[key] = `'${row[key]}'`;
row[key] = `= ${row[key]}`;
}
await connections[params.uid]
.schema(params.schema)
.delete(params.table)
.where(row)
.limit(1)
.run();
}
return { status: 'success', response: [] };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
}
}); });
ipcMain.handle('insert-table-rows', async (event, params) => { ipcMain.handle('insert-table-rows', async (event, params) => {

View File

@ -268,22 +268,18 @@ export default {
} }
}, },
deleteSelected () { deleteSelected () {
if (!this.primaryField) const rows = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => {
this.addNotification({ status: 'warning', message: this.$t('message.unableEditFieldWithoutPrimary') }); delete row._id;
else { return row;
const rowIDs = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => });
row[this.primaryField.name] ||
row[`${this.primaryField.table}.${this.primaryField.name}`] ||
row[`${this.primaryField.tableAlias}.${this.primaryField.name}`]
);
const params = { const params = {
primary: this.primaryField.name, primary: this.primaryField.name,
schema: this.getSchema(this.resultsetIndex), schema: this.getSchema(this.resultsetIndex),
table: this.getTable(this.resultsetIndex), table: this.getTable(this.resultsetIndex),
rows: rowIDs rows
}; };
this.$emit('delete-selected', params); this.$emit('delete-selected', params);
}
}, },
applyUpdate (params) { applyUpdate (params) {
const { primary, id, field, table, content } = params; const { primary, id, field, table, content } = params;