Skip to content

Commit

Permalink
fix: update or delete rows with more than one primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Mar 21, 2021
1 parent db47b40 commit 22a8c25
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
15 changes: 8 additions & 7 deletions src/main/ipc-handlers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`;
}
Expand Down
18 changes: 13 additions & 5 deletions src/renderer/components/WorkspaceQueryTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -326,6 +333,7 @@ export default {
table: this.getTable(this.resultsetIndex),
id: this.getPrimaryValue(row),
row,
orgRow: row,
field: this.selectedCell.field,
content: null
};
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/scss/main.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@import "~spectre.css/src/_variables.scss";
@import "~spectre.css/src/variables";
@import "variables";
@import "transitions";
@import "data-types";
@import "table-keys";
@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;
Expand Down

0 comments on commit 22a8c25

Please sign in to comment.