From 3baf6fa1736a405c95fb02d17c11514861ca9e04 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Thu, 6 May 2021 22:21:42 +0200 Subject: [PATCH] fix: better detection and handling of field default type --- src/main/libs/clients/MySQLClient.js | 6 +++--- src/main/libs/clients/PostgreSQLClient.js | 4 ++-- src/renderer/components/WorkspacePropsTab.vue | 14 +++++++++++++ .../components/WorkspacePropsTableRow.vue | 20 +++---------------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 887caddc..c6b811ab 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -695,7 +695,7 @@ export class MySQLClient extends AntaresCore { }, []).join(',') : ''; - const sql = `CREATE ${routine.definer ? `DEFINER=${routine.definer} ` : ''}PROCEDURE \`${routine.name}\`(${parameters}) + const sql = `CREATE ${routine.definer ? `DEFINER=${routine.definer} ` : ''}PROCEDURE \`${this._schema}\`.\`${routine.name}\`(${parameters}) LANGUAGE SQL ${routine.deterministic ? 'DETERMINISTIC' : 'NOT DETERMINISTIC'} ${routine.dataAccess} @@ -1090,7 +1090,7 @@ export class MySQLClient extends AntaresCore { ${addition.zerofill ? 'ZEROFILL' : ''} ${addition.nullable ? 'NULL' : 'NOT NULL'} ${addition.autoIncrement ? 'AUTO_INCREMENT' : ''} - ${addition.default ? `DEFAULT ${typeof addition.default === 'string' ? `'${addition.default}'` : addition.default}` : ''} + ${addition.default ? `DEFAULT ${addition.default}` : ''} ${addition.comment ? `COMMENT '${addition.comment}'` : ''} ${addition.collation ? `COLLATE ${addition.collation}` : ''} ${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''} @@ -1128,7 +1128,7 @@ export class MySQLClient extends AntaresCore { ${change.zerofill ? 'ZEROFILL' : ''} ${change.nullable ? 'NULL' : 'NOT NULL'} ${change.autoIncrement ? 'AUTO_INCREMENT' : ''} - ${change.default ? `DEFAULT ${typeof change.default === 'string' ? `'${change.default}'` : change.default}` : ''} + ${change.default ? `DEFAULT ${change.default}` : ''} ${change.comment ? `COMMENT '${change.comment}'` : ''} ${change.collation ? `COLLATE ${change.collation}` : ''} ${change.onUpdate ? `ON UPDATE ${change.onUpdate}` : ''} diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 398f1782..1db08efb 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -1047,7 +1047,7 @@ export class PostgreSQLClient extends AntaresCore { ${addition.zerofill ? 'ZEROFILL' : ''} ${addition.nullable ? 'NULL' : 'NOT NULL'} ${addition.autoIncrement ? 'AUTO_INCREMENT' : ''} - ${addition.default ? `DEFAULT ${typeof addition.default === 'string' ? `'${addition.default}'` : addition.default}` : ''} + ${addition.default ? `DEFAULT ${addition.default}` : ''} ${addition.comment ? `COMMENT '${addition.comment}'` : ''} ${addition.collation ? `COLLATE ${addition.collation}` : ''} ${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`); @@ -1093,7 +1093,7 @@ export class PostgreSQLClient extends AntaresCore { alterColumns.push(`ALTER COLUMN "${change.name}" TYPE ${localType}${length ? `(${length})` : ''}${change.isArray ? '[]' : ''} USING "${change.name}"::${localType}`); alterColumns.push(`ALTER COLUMN "${change.name}" ${change.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`); - alterColumns.push(`ALTER COLUMN "${change.name}" ${change.default ? `SET DEFAULT ${typeof change.default === 'string' ? `'${change.default}'` : change.default}` : 'DROP DEFAULT'}`); + alterColumns.push(`ALTER COLUMN "${change.name}" ${change.default ? `SET DEFAULT ${change.default}` : 'DROP DEFAULT'}`); if (['SERIAL', 'SMALLSERIAL', 'BIGSERIAL'].includes(change.type)) { const sequenceName = `${table}_${change.name}_seq`.replace(' ', '_'); createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.name}"`); diff --git a/src/renderer/components/WorkspacePropsTab.vue b/src/renderer/components/WorkspacePropsTab.vue index 84a27068..3efe6233 100644 --- a/src/renderer/components/WorkspacePropsTab.vue +++ b/src/renderer/components/WorkspacePropsTab.vue @@ -233,6 +233,20 @@ export default { const { status, response } = await Tables.getTableColumns(params); if (status === 'success') { this.originalFields = response.map(field => { + if (field.autoIncrement) + field.defaultType = 'autoincrement'; + else if (field.default === null) + field.defaultType = 'noval'; + else if (field.default === 'NULL') + field.defaultType = 'null'; + else if (field.default.match(/^\s*(\w+)\s*\((.*)\)$/)) + field.defaultType = 'expression'; + else { + field.defaultType = 'custom'; + if (isNaN(field.default) && !field.default.includes('\'')) + field.default = `'${field.default}'`; + } + return { ...field, _id: uidGen() }; }); this.localFields = JSON.parse(JSON.stringify(this.originalFields)); diff --git a/src/renderer/components/WorkspacePropsTableRow.vue b/src/renderer/components/WorkspacePropsTableRow.vue index 6f87d0b7..b7e33519 100644 --- a/src/renderer/components/WorkspacePropsTableRow.vue +++ b/src/renderer/components/WorkspacePropsTableRow.vue @@ -444,25 +444,11 @@ export default { }); this.defaultValue.onUpdate = this.localRow.onUpdate; - - if (this.localRow.autoIncrement) - this.defaultValue.type = 'autoincrement'; - else if (this.localRow.default === null) - this.defaultValue.type = 'noval'; - else if (this.localRow.default === 'NULL') - this.defaultValue.type = 'null'; - else if (this.localRow.default.match(/^'.*'$/g)) { - this.defaultValue.type = 'custom'; - this.defaultValue.custom = this.localRow.default.replaceAll(/(^')|('$)/g, ''); - } - else if (!isNaN(this.localRow.default.replace(/[:.-\s]/g, ''))) { - this.defaultValue.type = 'custom'; + this.defaultValue.type = this.localRow.defaultType; + if (this.defaultValue.type === 'custom') this.defaultValue.custom = this.localRow.default; - } - else { - this.defaultValue.type = 'expression'; + if (this.defaultValue.type === 'expression') this.defaultValue.expression = this.localRow.default; - } }, editON (event, content, field) { if (field === 'length') {