1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-06-05 21:59:22 +02:00

perf: improved the way how field default value are handled

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

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