1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-03-10 00:10:16 +01:00

perf(MySQL): improved several field types support on exporter

This commit is contained in:
Fabio Di Stasio 2022-02-19 12:40:54 +01:00
parent 748d44977e
commit 1990d9a3d4
5 changed files with 56 additions and 12 deletions

View File

@ -41,6 +41,7 @@ export const NUMBER = [
export const FLOAT = [ export const FLOAT = [
'FLOAT', 'FLOAT',
'DECIMAL',
'DOUBLE', 'DOUBLE',
'REAL', 'REAL',
'DOUBLE PRECISION', 'DOUBLE PRECISION',

View File

@ -23,7 +23,7 @@ export function mimeFromHex (hex) {
case '425A68': case '425A68':
return { ext: 'bz2', mime: 'application/x-bzip2' }; return { ext: 'bz2', mime: 'application/x-bzip2' };
default: default:
switch (hex) { // 4 bites switch (hex) { // 4 bytes
case '89504E47': case '89504E47':
return { ext: 'png', mime: 'image/png' }; return { ext: 'png', mime: 'image/png' };
case '47494638': case '47494638':

View File

@ -149,7 +149,7 @@ export default (connections) => {
} }
} }
} }
else if ([...BIT].includes(params.type)) { else if (BIT.includes(params.type)) {
escapedParam = `b'${sqlEscaper(params.content)}'`; escapedParam = `b'${sqlEscaper(params.content)}'`;
reload = true; reload = true;
} }

View File

@ -101,6 +101,26 @@ export class MySQLClient extends AntaresCore {
.filter(_type => _type.name === type.toUpperCase())[0]; .filter(_type => _type.name === type.toUpperCase())[0];
} }
_reducer (acc, curr) {
const type = typeof curr;
switch (type) {
case 'number':
case 'string':
return [...acc, curr];
case 'object':
if (Array.isArray(curr))
return [...acc, ...curr];
else {
const clausoles = [];
for (const key in curr)
clausoles.push(`\`${key}\` ${curr[key]}`);
return clausoles;
}
}
}
/** /**
* *
* @returns dbConfig * @returns dbConfig
@ -496,6 +516,7 @@ export class MySQLClient extends AntaresCore {
charset: field.CHARACTER_SET_NAME, charset: field.CHARACTER_SET_NAME,
collation: field.COLLATION_NAME, collation: field.COLLATION_NAME,
autoIncrement: field.EXTRA.includes('auto_increment'), autoIncrement: field.EXTRA.includes('auto_increment'),
generated: field.EXTRA.toLowerCase().includes('generated'),
onUpdate: field.EXTRA.toLowerCase().includes('on update') onUpdate: field.EXTRA.toLowerCase().includes('on update')
? field.EXTRA.substr(field.EXTRA.indexOf('on update') + 9, field.EXTRA.length).trim() ? field.EXTRA.substr(field.EXTRA.indexOf('on update') + 9, field.EXTRA.length).trim()
: '', : '',
@ -1600,7 +1621,7 @@ export class MySQLClient extends AntaresCore {
let insertRaw = ''; let insertRaw = '';
if (this._query.insert.length) { if (this._query.insert.length) {
const fieldsList = Object.keys(this._query.insert[0]); const fieldsList = Object.keys(this._query.insert[0]).map(col => '`' + col + '`');
const rowsList = this._query.insert.map(el => `(${Object.values(el).join(', ')})`); const rowsList = this._query.insert.map(el => `(${Object.values(el).join(', ')})`);
insertRaw = `(${fieldsList.join(', ')}) VALUES ${rowsList.join(', ')} `; insertRaw = `(${fieldsList.join(', ')}) VALUES ${rowsList.join(', ')} `;

View File

@ -1,6 +1,7 @@
import { SqlExporter } from './SqlExporter'; import { SqlExporter } from './SqlExporter';
import { BLOB, BIT } from 'common/fieldTypes'; import { BLOB, BIT, DATE, DATETIME, FLOAT } from 'common/fieldTypes';
import hexToBinary from 'common/libs/hexToBinary'; import hexToBinary from 'common/libs/hexToBinary';
import moment from 'moment';
export default class MysqlExporter extends SqlExporter { export default class MysqlExporter extends SqlExporter {
async getSqlHeader () { async getSqlHeader () {
@ -65,7 +66,9 @@ ${footer}
table: tableName, table: tableName,
schema: this.schemaName schema: this.schemaName
}); });
const columnNames = columns.map(col => '`' + col.name + '`');
const notGeneratedColumns = columns.filter(col => !col.generated);
const columnNames = notGeneratedColumns.map(col => '`' + col.name + '`');
const insertStmt = `INSERT INTO \`${tableName}\` (${columnNames.join( const insertStmt = `INSERT INTO \`${tableName}\` (${columnNames.join(
', ' ', '
)}) VALUES`; )}) VALUES`;
@ -101,25 +104,44 @@ ${footer}
else if (parseInt(rowIndex) === 0) sqlInsertString += '\n\t('; else if (parseInt(rowIndex) === 0) sqlInsertString += '\n\t(';
else sqlInsertString += ',\n\t('; else sqlInsertString += ',\n\t(';
for (const i in columns) { for (const i in notGeneratedColumns) {
const column = columns[i]; const column = notGeneratedColumns[i];
const val = row[column.name]; const val = row[column.name];
if (val === null) sqlInsertString += 'NULL'; if (val === null) sqlInsertString += 'NULL';
else if (DATE.includes(column.type)) {
sqlInsertString += moment(val).isValid()
? this.escapeAndQuote(moment(val).format('YYYY-MM-DD'))
: val;
}
else if (DATETIME.includes(column.type)) {
if (typeof val === 'string')
sqlInsertString += this.escapeAndQuote(val);
let datePrecision = '';
for (let i = 0; i < column.precision; i++)
datePrecision += i === 0 ? '.S' : 'S';
sqlInsertString += moment(val).isValid()
? this.escapeAndQuote(moment(val).format(`YYYY-MM-DD HH:mm:ss${datePrecision}`))
: val;
}
else if (BIT.includes(column.type)) else if (BIT.includes(column.type))
sqlInsertString += `b'${hexToBinary(Buffer.from(val).toString('hex'))}'`; sqlInsertString += `b'${hexToBinary(Buffer.from(val).toString('hex'))}'`;
else if (BLOB.includes(column.type)) else if (BLOB.includes(column.type))
sqlInsertString += `X'${val.toString('hex').toUpperCase()}'`; sqlInsertString += `X'${val.toString('hex').toUpperCase()}'`;
else if (FLOAT.includes(column.type))
sqlInsertString += parseFloat(val);
else if (val === '') sqlInsertString += '\'\''; else if (val === '') sqlInsertString += '\'\'';
else { else {
sqlInsertString += typeof val === 'string' sqlInsertString += typeof val === 'string'
? this.escapeAndQuote(val) ? this.escapeAndQuote(val)
: val; : typeof val === 'object'
? this.escapeAndQuote(JSON.stringify(val))
: val;
} }
if (parseInt(i) !== columns.length - 1) if (parseInt(i) !== notGeneratedColumns.length - 1)
sqlInsertString += ', '; sqlInsertString += ', ';
} }
@ -302,7 +324,7 @@ ${footer}
} }
async _queryStream (sql) { async _queryStream (sql) {
if (process.env.NODE_ENV === 'development') console.log(sql); if (process.env.NODE_ENV === 'development') console.log('EXPORTER:', sql);
const isPool = typeof this._client._connection.getConnection === 'function'; const isPool = typeof this._client._connection.getConnection === 'function';
const connection = isPool ? await this._client._connection.getConnection() : this._client._connection; const connection = isPool ? await this._client._connection.getConnection() : this._client._connection;
const stream = connection.connection.query(sql).stream(); const stream = connection.connection.query(sql).stream();