From 22a8c25717a4d4b285855426098a3a2846ce7448 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Sun, 21 Mar 2021 13:00:27 +0100 Subject: [PATCH] fix: update or delete rows with more than one primary key --- src/main/ipc-handlers/tables.js | 15 ++++++++------- src/main/libs/clients/PostgreSQLClient.js | 4 +++- .../components/WorkspaceQueryTable.vue | 18 +++++++++++++----- src/renderer/scss/main.scss | 6 +++--- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index a7f27647..f80849cf 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -119,30 +119,31 @@ export default (connections) => { else escapedParam = `'${sqlEscaper(params.content)}'`; - if (params.primary) { + if (params.primary) { // TODO: handle multiple primary await connections[params.uid] .update({ [params.field]: `= ${escapedParam}` }) .schema(params.schema) .from(params.table) .where({ [params.primary]: `= ${id}` }) + .limit(1) .run(); } else { - const { row } = params; + const { orgRow } = params; reload = true; - for (const key in row) { - if (typeof row[key] === 'string') - row[key] = `'${row[key]}'`; + for (const key in orgRow) { + if (typeof orgRow[key] === 'string') + orgRow[key] = `'${orgRow[key]}'`; - row[key] = `= ${row[key]}`; + orgRow[key] = `= ${orgRow[key]}`; } await connections[params.uid] .schema(params.schema) .update({ [params.field]: `= ${escapedParam}` }) .from(params.table) - .where(row) + .where(orgRow) .limit(1) .run(); } diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 1ae498a2..0161426b 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -328,6 +328,8 @@ export class PostgreSQLClient extends AntaresCore { * @returns {Array} */ async getTableByIDs (ids) { + if (!ids) return; + const { rows } = await this.raw(` SELECT relid AS tableid, relname, schemaname FROM pg_statio_all_tables WHERE relid IN (${ids}) UNION @@ -1206,7 +1208,7 @@ export class PostgreSQLClient extends AntaresCore { const orderByRaw = orderByArray.length ? `ORDER BY ${orderByArray.join(', ')} ` : ''; // LIMIT - const limitRaw = this._query.limit.length ? `LIMIT ${this._query.limit.join(', ')} ` : ''; + const limitRaw = selectArray.length && this._query.limit.length ? `LIMIT ${this._query.limit.join(', ')} ` : ''; return `${selectRaw}${updateRaw ? 'UPDATE' : ''}${insertRaw ? 'INSERT ' : ''}${this._query.delete ? 'DELETE ' : ''}${fromRaw}${updateRaw}${whereRaw}${groupByRaw}${orderByRaw}${limitRaw}${insertRaw}`; } diff --git a/src/renderer/components/WorkspaceQueryTable.vue b/src/renderer/components/WorkspaceQueryTable.vue index 6107e72f..3bc53823 100644 --- a/src/renderer/components/WorkspaceQueryTable.vue +++ b/src/renderer/components/WorkspaceQueryTable.vue @@ -122,7 +122,12 @@ export default { return this.getWorkspace(this.connUid).breadcrumbs.schema; }, primaryField () { - return this.fields.filter(field => ['pri', 'uni'].includes(field.key))[0] || false; + const primaryFields = this.fields.filter(field => ['pri', 'uni'].includes(field.key)); + + if (primaryFields.length > 1 || !primaryFields.length) + return false; + + return primaryFields[0]; }, isSortable () { return this.fields.every(field => field.name); @@ -289,15 +294,17 @@ export default { this.resizeResults(); }, updateField (payload, row) { - const localRow = Object.assign({}, row); - delete localRow._id; + const orgRow = this.localResults.find(lr => lr._id === row._id); + delete row._id; + delete orgRow._id; const params = { primary: this.primaryField.name, schema: this.getSchema(this.resultsetIndex), table: this.getTable(this.resultsetIndex), - id: this.getPrimaryValue(localRow), - localRow, + id: this.getPrimaryValue(orgRow), + row, + orgRow, ...payload }; this.$emit('update-field', params); @@ -326,6 +333,7 @@ export default { table: this.getTable(this.resultsetIndex), id: this.getPrimaryValue(row), row, + orgRow: row, field: this.selectedCell.field, content: null }; diff --git a/src/renderer/scss/main.scss b/src/renderer/scss/main.scss index 6f74e286..cfee5a36 100644 --- a/src/renderer/scss/main.scss +++ b/src/renderer/scss/main.scss @@ -1,4 +1,4 @@ -@import "~spectre.css/src/_variables.scss"; +@import "~spectre.css/src/variables"; @import "variables"; @import "transitions"; @import "data-types"; @@ -6,8 +6,8 @@ @import "fake-tables"; @import "mdi-additions"; @import "db-icons"; -@import "~spectre.css/src/spectre.scss"; -@import "~spectre.css/src/spectre-exp.scss"; +@import "~spectre.css/src/spectre"; +@import "~spectre.css/src/spectre-exp"; body { user-select: none;