feat: edit rows from tables without a primary key

This commit is contained in:
Fabio Di Stasio 2021-01-30 14:58:12 +01:00
parent 574d493908
commit 5940b0b842
2 changed files with 41 additions and 21 deletions

View File

@ -78,12 +78,33 @@ export default (connections) => {
else else
escapedParam = `"${sqlEscaper(params.content)}"`; escapedParam = `"${sqlEscaper(params.content)}"`;
await connections[params.uid] if (params.primary) {
.update({ [params.field]: `= ${escapedParam}` }) await connections[params.uid]
.schema(params.schema) .update({ [params.field]: `= ${escapedParam}` })
.from(params.table) .schema(params.schema)
.where({ [params.primary]: `= ${id}` }) .from(params.table)
.run(); .where({ [params.primary]: `= ${id}` })
.run();
}
else {
const { row } = params;
reload = true;
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)
.update({ [params.field]: `= ${escapedParam}` })
.from(params.table)
.where(row)
.limit(1)
.run();
}
return { status: 'success', response: { reload } }; return { status: 'success', response: { reload } };
} }
@ -92,7 +113,7 @@ export default (connections) => {
} }
}); });
ipcMain.handle('delete-table-rows', async (event, params) => { // TODO: check primary or try other ipcMain.handle('delete-table-rows', async (event, params) => {
if (params.primary) { if (params.primary) {
const idString = params.rows.map(row => typeof row[params.primary] === 'string' const idString = params.rows.map(row => typeof row[params.primary] === 'string'
? `"${row[params.primary]}"` ? `"${row[params.primary]}"`

View File

@ -69,7 +69,7 @@
class="tr" class="tr"
:class="{'selected': selectedRows.includes(row._id)}" :class="{'selected': selectedRows.includes(row._id)}"
@select-row="selectRow($event, row._id)" @select-row="selectRow($event, row._id)"
@update-field="updateField($event, getPrimaryValue(row))" @update-field="updateField($event, row)"
@contextmenu="contextMenu" @contextmenu="contextMenu"
/> />
</template> </template>
@ -253,19 +253,18 @@ export default {
refreshScroller () { refreshScroller () {
this.resizeResults(); this.resizeResults();
}, },
updateField (payload, id) { updateField (payload, row) {
if (!this.primaryField) delete row._id;
this.addNotification({ status: 'warning', message: this.$t('message.unableEditFieldWithoutPrimary') });
else { 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), id: this.getPrimaryValue(row),
id, row,
...payload ...payload
}; };
this.$emit('update-field', params); this.$emit('update-field', params);
}
}, },
deleteSelected () { deleteSelected () {
const rows = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => { const rows = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => {