fix: update or delete rows with more than one primary key

This commit is contained in:
Fabio Di Stasio 2021-03-21 13:00:27 +01:00
parent db47b4040a
commit 22a8c25717
4 changed files with 27 additions and 16 deletions

View File

@ -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();
}

View File

@ -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}`;
}

View File

@ -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
};

View File

@ -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;