move building of where clause to global scope function

This commit is contained in:
Paul Trowbridge 2019-03-13 13:18:41 -04:00
parent 2352a898be
commit e649fd84e0

View File

@ -129,34 +129,10 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) {
var path = './route_sql/addmonth_vd.sql';
var callback = function(arg){
sql = arg
sql = arg;
//buile where clause expression
({ c, w, d } = build_where(req, c, w, d, args));
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
if (Array.isArray(req.body.scenario[i])){
//if the scenario key has a value that is an array of items, push it into an `IN` statement
//iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
w = w + i + " IN (";
for (var j in req.body.scenario[i]){
if (d>1){
w = w + ",";
}
w = w + "'" + req.body.scenario[i][j] + "'";
d = d + 1;
}
w = w + ")"
} else {
w = w + i + " = '" + req.body.scenario[i] + "'";
}
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
@ -373,4 +349,36 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) {
});
})
function build_where(req, c, w, d, args) {
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `;
}
if (Array.isArray(req.body.scenario[i])) {
//if the scenario key has a value that is an array of items, push it into an `IN` statement
//iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
w = w + i + " IN (";
for (var j in req.body.scenario[i]) {
if (d > 1) {
w = w + ",";
}
w = w + "'" + req.body.scenario[i][j] + "'";
d = d + 1;
}
w = w + ")";
}
else {
w = w + i + " = '" + req.body.scenario[i] + "'";
}
args.push(req.body.scenario[i]);
c = c + 1;
}
;
return { c, w, d };
}
server.listen(3000, () => console.log('started'))