perf: improved the way how field default value are handled

This commit is contained in:
Fabio Di Stasio 2021-05-22 16:24:19 +02:00
parent f6b77d1243
commit 7a766f04e6
5 changed files with 57 additions and 29 deletions

View File

@ -84,7 +84,7 @@ This is a roadmap with major features will come in near future.
- [ ] Windows
- [x] Linux
- [x] MacOS
- [ ] MacOS
## Translations

View File

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

View File

@ -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'),

View File

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

View File

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