Commit 181a2eee authored by Fernando Erd's avatar Fernando Erd 👌

Merge remote-tracking branch 'origin/v1.11.17'

parents a208643a fea8787e
......@@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## 1..11.16 - 2019-10-30
## 1.11.17 - 2019-10-08
## Changed
- Fix query\_exec
## 1.11.16 - 2019-10-30
## Changed
- University Teacher updated
......
/*
Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
This file is part of simcaq-node.
simcaq-node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
simcaq-node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with simcaq-node. If not, see <https://www.gnu.org/licenses/>.
*/
const libs = `${process.cwd()}/libs`;
......@@ -5,6 +25,8 @@ const log = require(`${libs}/log`)(module);
const conn = require(`${libs}/db/monet`);
const SqlString = require('sqlstring');
// Promise that executes an SQL query with optional parameters
// ```
// Examples:
......@@ -17,25 +39,16 @@ const conn = require(`${libs}/db/monet`);
// ```
function execSqlQuery(sqlQuery, sqlQueryParams = []) {
log.debug(`Executing SQL query '${sqlQuery}' with params '${sqlQueryParams}'`);
let queryStrWithParams = SqlString.format(sqlQuery, sqlQueryParams);
return new Promise((resolve, reject) => {
// Prepare statement
conn.prepare(sqlQuery, true).then((dbQuery) => {
// Execute query
dbQuery.exec(sqlQueryParams).then((dbResult) => {
// release resources allocated for the prepared statement
dbQuery.release();
resolve(dbResult.data);
}).catch((queryError) => {
log.error(`SQL query execution error: ${queryError.message}`);
log.error(`SQL query: ${sqlQuery} with params: ${sqlQueryParams}`);
// release resources allocated for the prepared statement
dbQuery.release();
reject(new Error(queryError.message));
});
}).catch((prepError) => {
log.error(`SQL prepared statement error: ${prepError.message}`);
// Execute query
conn.query(queryStrWithParams, [], true).then((dbResult) => {
// release resources allocated for the prepared statement
resolve(dbResult.data);
},(queryError) => {
log.error(`SQL query execution error: ${queryError.message}`);
log.error(`SQL query: ${sqlQuery} with params: ${sqlQueryParams}`);
reject(new Error(prepError.message));
reject(new Error(queryError.message));
});
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment