From eef7c1dcecc6593ab0e69ed678187a57fe0a4fb6 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Sat, 22 Jan 2022 12:29:49 +0100 Subject: [PATCH] perf: support of scale in field's length setting --- src/common/data-types/mysql.js | 1 + src/common/data-types/postgresql.js | 11 +++--- src/main/libs/clients/MySQLClient.js | 13 ++++--- src/main/libs/clients/PostgreSQLClient.js | 7 ++-- .../components/WorkspaceTabPropsTableRow.vue | 35 ++++++++++++++++++- .../components/WorkspaceTabQueryTable.vue | 1 + 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/common/data-types/mysql.js b/src/common/data-types/mysql.js index 78e3d2c9..f1eeaf33 100644 --- a/src/common/data-types/mysql.js +++ b/src/common/data-types/mysql.js @@ -66,6 +66,7 @@ module.exports = [ { name: 'DECIMAL', length: true, + scale: true, collation: false, unsigned: false, zerofill: false diff --git a/src/common/data-types/postgresql.js b/src/common/data-types/postgresql.js index bfe9e50d..e666229b 100644 --- a/src/common/data-types/postgresql.js +++ b/src/common/data-types/postgresql.js @@ -22,11 +22,6 @@ module.exports = [ length: false, unsigned: true }, - { - name: 'NUMERIC', - length: true, - unsigned: true - }, { name: 'SMALLSERIAL', length: false, @@ -52,6 +47,12 @@ module.exports = [ length: false, unsigned: true }, + { + name: 'NUMERIC', + length: true, + unsigned: true, + scale: true + }, { name: 'DOUBLE PRECISION', length: false, diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index b8ab691c..8560dea7 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -435,7 +435,7 @@ export class MySQLClient extends AntaresCore { return rows.map(field => { let numLength = field.COLUMN_TYPE.match(/int\(([^)]+)\)/); - numLength = numLength ? +numLength.pop() : null; + numLength = numLength ? +numLength.pop() : field.NUMERIC_PRECISION || null; const enumValues = /(enum|set)/.test(field.COLUMN_TYPE) ? field.COLUMN_TYPE.match(/\(([^)]+)\)/)[0].slice(1, -1) : null; @@ -443,10 +443,13 @@ export class MySQLClient extends AntaresCore { return { name: field.COLUMN_NAME, key: field.COLUMN_KEY.toLowerCase(), - type: (remappedFields && remappedFields[field.COLUMN_NAME]) ? remappedFields[field.COLUMN_NAME].type : field.DATA_TYPE, + type: (remappedFields && remappedFields[field.COLUMN_NAME]) + ? remappedFields[field.COLUMN_NAME].type + : field.DATA_TYPE.toUpperCase(), schema: field.TABLE_SCHEMA, table: field.TABLE_NAME, numPrecision: field.NUMERIC_PRECISION, + numScale: field.NUMERIC_SCALE, numLength, enumValues, datePrecision: field.DATETIME_PRECISION, @@ -1295,7 +1298,7 @@ export class MySQLClient extends AntaresCore { const length = typeInfo.length ? field.enumValues || field.numLength || field.charLength || field.datePrecision : false; newColumns.push(`\`${field.name}\` - ${field.type.toUpperCase()}${length ? `(${length})` : ''} + ${field.type.toUpperCase()}${length ? `(${length}${field.numScale !== null ? `,${field.numScale}` : ''})` : ''} ${field.unsigned ? 'UNSIGNED' : ''} ${field.zerofill ? 'ZEROFILL' : ''} ${field.nullable ? 'NULL' : 'NOT NULL'} @@ -1364,7 +1367,7 @@ export class MySQLClient extends AntaresCore { const length = typeInfo.length ? addition.enumValues || addition.numLength || addition.charLength || addition.datePrecision : false; alterColumns.push(`ADD COLUMN \`${addition.name}\` - ${addition.type.toUpperCase()}${length ? `(${length})` : ''} + ${addition.type.toUpperCase()}${length ? `(${length}${addition.numScale !== null ? `,${addition.numScale}` : ''})` : ''} ${addition.unsigned ? 'UNSIGNED' : ''} ${addition.zerofill ? 'ZEROFILL' : ''} ${addition.nullable ? 'NULL' : 'NOT NULL'} @@ -1402,7 +1405,7 @@ export class MySQLClient extends AntaresCore { const length = typeInfo.length ? change.enumValues || change.numLength || change.charLength || change.datePrecision : false; alterColumns.push(`CHANGE COLUMN \`${change.orgName}\` \`${change.name}\` - ${change.type.toUpperCase()}${length ? `(${length})` : ''} + ${change.type.toUpperCase()}${length ? `(${length}${change.numScale !== null ? `,${change.numScale}` : ''})` : ''} ${change.unsigned ? 'UNSIGNED' : ''} ${change.zerofill ? 'ZEROFILL' : ''} ${change.nullable ? 'NULL' : 'NOT NULL'} diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index a5a7865c..16d4913c 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -326,6 +326,7 @@ export class PostgreSQLClient extends AntaresCore { isArray, schema: field.table_schema, table: field.table_name, + numScale: field.numeric_scale, numPrecision: field.numeric_precision, datePrecision: field.datetime_precision, charLength: field.character_maximum_length, @@ -1144,7 +1145,7 @@ export class PostgreSQLClient extends AntaresCore { const length = typeInfo.length ? field.enumValues || field.numLength || field.charLength || field.datePrecision : false; newColumns.push(`"${field.name}" - ${field.type.toUpperCase()}${length ? `(${length})` : ''} + ${field.type.toUpperCase()}${length ? `(${length}${field.numScale !== null ? `,${field.numScale}` : ''})` : ''} ${field.unsigned ? 'UNSIGNED' : ''} ${field.zerofill ? 'ZEROFILL' : ''} ${field.nullable ? 'NULL' : 'NOT NULL'} @@ -1208,7 +1209,7 @@ export class PostgreSQLClient extends AntaresCore { const length = typeInfo.length ? addition.numLength || addition.charLength || addition.datePrecision : false; alterColumns.push(`ADD COLUMN "${addition.name}" - ${addition.type.toUpperCase()}${length ? `(${length})` : ''}${addition.isArray ? '[]' : ''} + ${addition.type.toUpperCase()}${length ? `(${length}${addition.numScale !== null ? `,${addition.numScale}` : ''})` : ''}${addition.isArray ? '[]' : ''} ${addition.unsigned ? 'UNSIGNED' : ''} ${addition.zerofill ? 'ZEROFILL' : ''} ${addition.nullable ? 'NULL' : 'NOT NULL'} @@ -1254,7 +1255,7 @@ export class PostgreSQLClient extends AntaresCore { localType = change.type.toLowerCase(); } - alterColumns.push(`ALTER COLUMN "${change.name}" TYPE ${localType}${length ? `(${length})` : ''}${change.isArray ? '[]' : ''} USING "${change.name}"::${localType}`); + alterColumns.push(`ALTER COLUMN "${change.name}" TYPE ${localType}${length ? `(${length}${change.numScale !== null ? `,${change.numScale}` : ''})` : ''}${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 ${change.default}` : 'DROP DEFAULT'}`); diff --git a/src/renderer/components/WorkspaceTabPropsTableRow.vue b/src/renderer/components/WorkspaceTabPropsTableRow.vue index 999c71ce..f6a9bf00 100644 --- a/src/renderer/components/WorkspaceTabPropsTableRow.vue +++ b/src/renderer/components/WorkspaceTabPropsTableRow.vue @@ -99,6 +99,9 @@ {{ localRow.enumValues }} + + {{ localLength }}, {{ localRow.numScale }} + {{ localLength }} @@ -112,6 +115,16 @@ class="editable-field form-input input-sm px-1" @blur="editOFF" > + 31 && (charCode < 48 || charCode > 57)) && charCode !== 44) || (charCode === 44 && e.target.value.includes(','))) + e.preventDefault(); + else + return true; + }, hideDefaultModal () { this.isDefaultModal = false; } diff --git a/src/renderer/components/WorkspaceTabQueryTable.vue b/src/renderer/components/WorkspaceTabQueryTable.vue index 5b9e3ed2..d41ebb44 100644 --- a/src/renderer/components/WorkspaceTabQueryTable.vue +++ b/src/renderer/components/WorkspaceTabQueryTable.vue @@ -280,6 +280,7 @@ export default { fieldLength (field) { if ([...BLOB, ...LONG_TEXT].includes(field.type)) return null; else if (TEXT.includes(field.type)) return field.charLength; + else if (field.numScale) return `${field.numPrecision}, ${field.numScale}`; return field.length; }, keyName (key) {