-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OGUI-1455] SQL queries logging. (#2675)
* log the SQL query performed by the user in a non-prepared statement way
- Loading branch information
1 parent
faa1d72
commit 205a59e
Showing
4 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license | ||
* Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
* All rights not expressly granted are reserved. | ||
* | ||
* This software is distributed under the terms of the GNU General Public | ||
* License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
* | ||
* In applying this license CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
|
||
/** | ||
* Translate the SQL prepared statement to a regular SQL query. | ||
* This function is to be used for logging purposes ONLY. | ||
* @param {string} requestRows - The prepared SQL statement. | ||
* @param {object} values - Values for the prepared SQL statement. | ||
* @param {number} limit - Configured limit of the sql query results. | ||
* @returns {string} The resulting SQL query as a string. | ||
*/ | ||
function processPreparedSQLStatement(requestRows, values, limit) { | ||
let sqlQuery = requestRows; | ||
|
||
const iterator = values.values(); | ||
for (const value of iterator) { | ||
if (Array.isArray(value)) { | ||
sqlQuery = sqlQuery.replace('?', convertArrayToString(value)); | ||
} else { | ||
sqlQuery = sqlQuery.replace('?', `'${value}'`); | ||
} | ||
} | ||
sqlQuery = sqlQuery.replace('?', limit); | ||
|
||
return sqlQuery; | ||
} | ||
|
||
/** | ||
* Helper function that converts arrays to strings with a single quote around the values. | ||
* This function can later be expanded to handle values other than strings in the array. | ||
* @param {Array} array - Array to convert to string. | ||
* @returns {string} a string representation of the input array. | ||
*/ | ||
function convertArrayToString(array) { | ||
let processedArray = ''; | ||
array.forEach((v) => { | ||
if (typeof v == 'string') { | ||
processedArray += `'${v}',`; | ||
} | ||
}); | ||
processedArray = processedArray.substring(0, processedArray.length - 1); | ||
return processedArray; | ||
} | ||
|
||
module.exports.processPreparedSQLStatement = processPreparedSQLStatement; |
38 changes: 38 additions & 0 deletions
38
InfoLogger/test/lib/services/mocha-preparedStatementParser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @license | ||
* Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
* All rights not expressly granted are reserved. | ||
* | ||
* This software is distributed under the terms of the GNU General Public | ||
* License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
* | ||
* In applying this license CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
|
||
const assert = require('assert'); | ||
const { processPreparedSQLStatement } = require('../../../lib/utils/preparedStatementParser.js'); | ||
|
||
describe('preparedStatementParser() - test suite', () => { | ||
it('should be able to fill in a prepared statement', async () => { | ||
const requestedRows = 'SELECT * FROM `messages` WHERE `timestamp`>=? AND `timestamp`<=? AND `hostname` = ? ' | ||
+ 'AND NOT(`hostname` = ? AND `hostname` IS NOT NULL) AND `severity` IN (?) ORDER BY `TIMESTAMP` LIMIT 10'; | ||
const values = [ | ||
'1563794601.351', | ||
'1563794661.354', | ||
'test', | ||
'testEx', | ||
[ | ||
'D', | ||
'W', | ||
], | ||
]; | ||
const sqlProcessedResult = processPreparedSQLStatement(requestedRows, values, 10); | ||
const expectedSqlResult = "SELECT * FROM `messages` WHERE `timestamp`>='1563794601.351' AND `timestamp`" + | ||
"<='1563794661.354' AND `hostname` = 'test' AND NOT(`hostname` = 'testEx' AND `hostname` IS NOT" + | ||
" NULL) AND `severity` IN ('D','W') ORDER BY `TIMESTAMP` LIMIT 10"; | ||
assert.strictEqual(sqlProcessedResult, expectedSqlResult); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters