From 690a4541f96eaf831dea07b777a421729173b654 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Fri, 11 Jun 2021 14:32:51 +0200 Subject: [PATCH] perf: remove comments from queries before execution --- src/main/libs/clients/MySQLClient.js | 7 ++++++- src/main/libs/clients/PostgreSQLClient.js | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 9f3e077d..d512eed6 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -1325,6 +1325,7 @@ export class MySQLClient extends AntaresCore { * @memberof MySQLClient */ async raw (sql, args) { + sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, ''); if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder args = { @@ -1337,7 +1338,11 @@ export class MySQLClient extends AntaresCore { const nestTables = args.nest ? '.' : false; const resultsArr = []; let paramsArr = []; - const queries = args.split ? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm) : [sql]; + const queries = args.split + ? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm) + .filter(Boolean) + .map(q => q.trim()) + : [sql]; const isPool = typeof this._connection.getConnection === 'function'; const connection = isPool ? await this._connection.getConnection() : this._connection; diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 02775749..ebfbd50d 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -1174,6 +1174,8 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async raw (sql, args) { + sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, ''); + args = { nest: false, details: false, @@ -1186,7 +1188,11 @@ export class PostgreSQLClient extends AntaresCore { const resultsArr = []; let paramsArr = []; - const queries = args.split ? sql.split(/(?!\B'[^']*);(?![^']*'\B)/gm) : [sql]; + const queries = args.split + ? sql.split(/(?!\B'[^']*);(?![^']*'\B)/gm) + .filter(Boolean) + .map(q => q.trim()) + : [sql]; if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder