Skip to content

Commit

Permalink
mssql adapter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ctjong committed Mar 7, 2018
1 parent ae77f14 commit 6692979
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 47 deletions.
108 changes: 62 additions & 46 deletions adapters/db/mssqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ module.exports =
*/
function initialize(config)
{
if(!!pool || !config.database.connectionString)
return;
var sql = require("mssql");
pool = new sql.ConnectionPool(config.database.connectionString, function(err)
{
if (err)
{
console.log(err);
throw new _this.error.Error("f8cb", 500, "error while connecting to database");
}
});
pool.sql = sql;
// initialization should happen on the first request
}

/**
Expand Down Expand Up @@ -271,51 +260,78 @@ module.exports =
*/
function execute(ctx, query, successCb, completeCb)
{
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

var request = new pool.sql.Request(pool);
for (var key in queryParams)
ensurePoolInitialized(function()
{
if (!queryParams.hasOwnProperty(key))
continue;
var paramValue = queryParams[key];
if (typeof (paramValue) === "number" && Math.abs(paramValue) > 2147483647)
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

var request = new pool.sql.Request(pool);
for (var key in queryParams)
{
request.input(key, pool.sql.BigInt, paramValue);
if (!queryParams.hasOwnProperty(key))
continue;
var paramValue = queryParams[key];
if (typeof (paramValue) === "number" && Math.abs(paramValue) > 2147483647)
{
request.input(key, pool.sql.BigInt, paramValue);
}
else
{
request.input(key, paramValue);
}
}
else
request.query(queryString, function (err, dbResponse)
{
request.input(key, paramValue);
}
if (err)
{
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(err);
throw new _this.error.Error("a07f", 500, "error while sending query to database");
}
else
{
_this.exec.safeExecute(ctx, function ()
{
successCb(dbResponse.recordset);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, completeCb);
}
}
});
console.log("-------------------------------------------------");
});
}

/**
* Ensure the connection pool is initialized
* @param {any} callback Callback function
*/
function ensurePoolInitialized(callback)
{
if(!!pool)
{
callback();
return;
}
request.query(queryString, function (err, dbResponse)
var sql = require("mssql");
pool = new sql.ConnectionPool(config.database.connectionString, function(err)
{
if (err)
{
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(err);
throw new _this.error.Error("a07f", 500, "error while sending query to database");
}
else
{
_this.exec.safeExecute(ctx, function ()
{
successCb(dbResponse.recordset);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, completeCb);
}
throw new _this.error.Error("f8cb", 500, "error while connecting to database");
}
pool.sql = sql;
callback();
});
console.log("-------------------------------------------------");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orion-api",
"version": "1.2.6",
"version": "1.2.7",
"description": "REST API server application",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 6692979

Please sign in to comment.