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) => {
let idString;
ipcMain.handle('delete-table-rows', async (event, params) => { // TODO: check primary or try other
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')
idString = params.rows.map(row => `"${row}"`).join(',');
else
idString = params.rows.join(',');
try {
const result = await connections[params.uid]
.schema(params.schema)
.delete(params.table)
.where({ [params.primary]: `IN (${idString})` })
.run();
try {
const result = await connections[params.uid]
.schema(params.schema)
.delete(params.table)
.where({ [params.primary]: `IN (${idString})` })
.run();
return { status: 'success', response: result };
return { status: 'success', response: result };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
}
catch (err) {
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() };
}
}
});

View File

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