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,25 +92,48 @@ 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'
? `"${row[params.primary]}"`
: row[params.primary]).join(',');
if (typeof params.rows[0] === 'string') try {
idString = params.rows.map(row => `"${row}"`).join(','); const result = await connections[params.uid]
else .schema(params.schema)
idString = params.rows.join(','); .delete(params.table)
.where({ [params.primary]: `IN (${idString})` })
.run();
try { return { status: 'success', response: result };
const result = await connections[params.uid] }
.schema(params.schema) catch (err) {
.delete(params.table) return { status: 'error', response: err.toString() };
.where({ [params.primary]: `IN (${idString})` }) }
.run();
return { status: 'success', response: result };
} }
catch (err) { else {
return { status: 'error', response: err.toString() }; 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() };
}
} }
}); });

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}`] || const params = {
row[`${this.primaryField.tableAlias}.${this.primaryField.name}`] primary: this.primaryField.name,
); schema: this.getSchema(this.resultsetIndex),
const params = { table: this.getTable(this.resultsetIndex),
primary: this.primaryField.name, rows
schema: this.getSchema(this.resultsetIndex), };
table: this.getTable(this.resultsetIndex), this.$emit('delete-selected', params);
rows: rowIDs
};
this.$emit('delete-selected', params);
}
}, },
applyUpdate (params) { applyUpdate (params) {
const { primary, id, field, table, content } = params; const { primary, id, field, table, content } = params;