From d9a3eab015302e9f23112f659658073ab3242191 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Wed, 5 Jan 2022 18:23:31 +0100 Subject: [PATCH] perf(MySQL): support to ANSI_QUOTES sql_mode, closes #158 --- src/main/libs/clients/MySQLClient.js | 46 +++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index d53d3589..b8ab691c 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -137,8 +137,16 @@ export class MySQLClient extends AntaresCore { if (!this._poolSize) { this._connection = await mysql.createConnection(dbConfig); + // ANSI_QUOTES check + const res = await this.getVariable('sql_mode', 'global'); + const sqlMode = res?.value.split(','); + const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES'); + if (this._params.readonly) await this.raw('SET SESSION TRANSACTION READ ONLY'); + + if (hasAnsiQuotes) + await this.raw(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`); } else { this._connection = mysql.createPool({ @@ -152,11 +160,21 @@ export class MySQLClient extends AntaresCore { } }); - if (this._params.readonly) { - this._connection.on('connection', connection => { + // ANSI_QUOTES check + const res = await this.getVariable('sql_mode', 'global'); + const sqlMode = res?.value.split(','); + const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES'); + + if (hasAnsiQuotes) + await this._connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`); + + this._connection.on('connection', connection => { + if (this._params.readonly) connection.query('SET SESSION TRANSACTION READ ONLY'); - }); - } + + if (hasAnsiQuotes) + connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`); + }); } } @@ -1140,6 +1158,26 @@ export class MySQLClient extends AntaresCore { }); } + /** + * SHOW VARIABLES LIKE %variable% + * + * @param {String} variable + * @param {'global'|'session'|null} level + * @returns {Object} variable + * @memberof MySQLClient + */ + async getVariable (variable, level) { + const sql = `SHOW${level ? ' ' + level.toUpperCase() : ''} VARIABLES LIKE '%${variable}%'`; + const results = await this.raw(sql); + + if (results.rows.length) { + return { + name: results.rows[0].Variable_name, + value: results.rows[0].Value + }; + } + } + /** * SHOW ENGINES *