perf(MySQL): support to ANSI_QUOTES sql_mode, closes #158

This commit is contained in:
Fabio Di Stasio 2022-01-05 18:23:31 +01:00
parent 2ab49c218d
commit d9a3eab015
1 changed files with 42 additions and 4 deletions

View File

@ -137,8 +137,16 @@ export class MySQLClient extends AntaresCore {
if (!this._poolSize) { if (!this._poolSize) {
this._connection = await mysql.createConnection(dbConfig); 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) if (this._params.readonly)
await this.raw('SET SESSION TRANSACTION READ ONLY'); 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 { else {
this._connection = mysql.createPool({ this._connection = mysql.createPool({
@ -152,11 +160,21 @@ export class MySQLClient extends AntaresCore {
} }
}); });
if (this._params.readonly) { // ANSI_QUOTES check
this._connection.on('connection', connection => { 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'); 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 * SHOW ENGINES
* *