From 7a766f04e668868e8844aac1d7d445bc2da21558 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Sat, 22 May 2021 16:24:19 +0200 Subject: [PATCH] perf: improved the way how field default value are handled --- README.md | 2 +- package.json | 5 +- src/main/libs/clients/MySQLClient.js | 57 ++++++++++++------- src/renderer/components/ModalFakerRows.vue | 11 +++- .../components/WorkspacePropsTableRow.vue | 11 +++- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 99a7267e..21393825 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ This is a roadmap with major features will come in near future. - [ ] Windows - [x] Linux -- [x] MacOS +- [ ] MacOS ## Translations diff --git a/package.json b/package.json index 47fda475..67430453 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,7 @@ "target": { "target": "default", "arch": [ - "x64", - "arm64" + "x64" ] } }, @@ -132,4 +131,4 @@ "vue-template-compiler": "^2.6.12", "webpack": "^4.46.0" } -} +} \ No newline at end of file diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index de1574d5..73fca3a3 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -309,24 +309,43 @@ export class MySQLClient extends AntaresCore { .orderBy({ ORDINAL_POSITION: 'ASC' }) .run(); - // const { rows } = await this.raw(`SHOW CREATE TABLE ${schema}.${table}`); + const { rows: fields } = await this.raw(`SHOW CREATE TABLE ${schema}.${table}`); - // const fields = rows.map(row => { - // let n = 0; - // return row['Create Table'] - // .split('') - // .reduce((acc, curr) => { - // if (curr === ')') n--; - // if (n !== 0) acc += curr; - // if (curr === '(') n++; - // return acc; - // }, '') - // .replaceAll('\n', '') - // .split(',') - // .map(f => { - // return f.trim();// TODO: here map the field - // }); - // }); + const remappedFields = fields.map(row => { + let n = 0; + return row['Create Table'] + .split('') + .reduce((acc, curr) => { + if (curr === ')') n--; + if (n !== 0) acc += curr; + if (curr === '(') n++; + return acc; + }, '') + .replaceAll('\n', '') + .split(',') + .map(f => { + const fieldArr = f.trim().split(' '); + const nameAndType = fieldArr.slice(0, 2); + + if (!nameAndType[0].includes('`')) return false; + + const details = fieldArr.slice(2).join(' '); + const defaultValue = details.includes('DEFAULT') ? details.match(/(?<=DEFAULT ).*?$/gs)[0] : null; + const typeAndLength = nameAndType[1].replace(')', '').split('('); + + return { + name: nameAndType[0].replaceAll('`', ''), + type: typeAndLength[0].toUpperCase(), + length: typeAndLength[1] ? typeAndLength[1] : null, + default: defaultValue + }; + }) + .filter(Boolean) + .reduce((acc, curr) => { + acc[curr.name] = curr; + return acc; + }, {}); + })[0]; return rows.map(field => { let numLength = field.COLUMN_TYPE.match(/int\(([^)]+)\)/); @@ -338,7 +357,7 @@ export class MySQLClient extends AntaresCore { return { name: field.COLUMN_NAME, key: field.COLUMN_KEY.toLowerCase(), - type: field.DATA_TYPE.toUpperCase(), + type: remappedFields[field.COLUMN_NAME].type, schema: field.TABLE_SCHEMA, table: field.TABLE_NAME, numPrecision: field.NUMERIC_PRECISION, @@ -350,7 +369,7 @@ export class MySQLClient extends AntaresCore { unsigned: field.COLUMN_TYPE.includes('unsigned'), zerofill: field.COLUMN_TYPE.includes('zerofill'), order: field.ORDINAL_POSITION, - default: field.COLUMN_DEFAULT, // TODO: get from show create table + default: remappedFields[field.COLUMN_NAME].default, charset: field.CHARACTER_SET_NAME, collation: field.COLLATION_NAME, autoIncrement: field.EXTRA.includes('auto_increment'), diff --git a/src/renderer/components/ModalFakerRows.vue b/src/renderer/components/ModalFakerRows.vue index 497cd255..388c93f0 100644 --- a/src/renderer/components/ModalFakerRows.vue +++ b/src/renderer/components/ModalFakerRows.vue @@ -250,10 +250,15 @@ export default { if (field.default === 'NULL') fieldDefault = null; else { if ([...NUMBER, ...FLOAT].includes(field.type)) - fieldDefault = +field.default; + fieldDefault = Number.isNaN(+field.default) ? null : +field.default; - if ([...TEXT, ...LONG_TEXT].includes(field.type)) - fieldDefault = field.default; + if ([...TEXT, ...LONG_TEXT].includes(field.type)) { + fieldDefault = field.default + ? field.default.includes('\'') + ? field.default.split('\'')[1] + : field.default + : ''; + } if ([...TIME, ...DATE].includes(field.type)) fieldDefault = field.default; diff --git a/src/renderer/components/WorkspacePropsTableRow.vue b/src/renderer/components/WorkspacePropsTableRow.vue index 091eeeb2..8a8e19c2 100644 --- a/src/renderer/components/WorkspacePropsTableRow.vue +++ b/src/renderer/components/WorkspacePropsTableRow.vue @@ -450,8 +450,13 @@ export default { this.defaultValue.onUpdate = this.localRow.onUpdate; this.defaultValue.type = this.localRow.defaultType; - if (this.defaultValue.type === 'custom') - this.defaultValue.custom = this.localRow.default; + if (this.defaultValue.type === 'custom') { + this.defaultValue.custom = this.localRow.default + ? this.localRow.default.includes('\'') + ? this.localRow.default.split('\'')[1] + : this.localRow.default + : ''; + } if (this.defaultValue.type === 'expression') this.defaultValue.expression = this.localRow.default; }, @@ -530,7 +535,7 @@ export default { break; case 'custom': this.localRow.autoIncrement = false; - this.localRow.default = this.defaultValue.custom; + this.localRow.default = Number.isNaN(+this.defaultValue.custom) ? `'${this.defaultValue.custom}'` : this.defaultValue.custom; break; case 'expression': this.localRow.autoIncrement = false;