-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpreparedStatementParser.js
More file actions
56 lines (51 loc) · 1.97 KB
/
Copy pathpreparedStatementParser.js
File metadata and controls
56 lines (51 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;