Avoid multiple connections if possible

This commit is contained in:
Thomas FORGIONE 2015-05-20 10:47:27 +02:00
parent 66a2159972
commit 7a6d4b0247
1 changed files with 82 additions and 88 deletions

View File

@ -36,8 +36,7 @@ var checkId = function(req, res, next, callback, id) {
});
}
var addCamerasFromId = function(req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) {
var addCamerasFromId = function(client, req, res, callback, id) {
client.query(
"SELECT ((camera).position).x AS px, " +
"((camera).position).y AS py, " +
@ -69,14 +68,11 @@ var addCamerasFromId = function(req, res, callback, id) {
);
}
callback();
release();
}
);
});
}
var addCoinsFromId = function(req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) {
var addCoinsFromId = function(client, req, res, callback, id) {
client.query(
"SELECT coin_id, time FROM coinclicked WHERE user_id = $1",
[id],
@ -92,14 +88,11 @@ var addCoinsFromId = function(req, res, callback, id) {
);
}
callback();
release();
}
);
});
}
var addArrowsFromId = function(req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) {
var addArrowsFromId = function(client, req, res, callback, id) {
client.query(
"SELECT arrow_id, time FROM arrowclicked WHERE user_id = $1",
[id],
@ -115,10 +108,8 @@ var addArrowsFromId = function(req, res, callback, id) {
);
}
callback();
release();
}
);
});
}
module.exports.index = function(req, res) {
@ -171,9 +162,10 @@ module.exports.replay_info = function(req, res) {
// Parse id
var id = tools.filterInt(req.params.id);
addCamerasFromId(req, res, function() {
addCoinsFromId(req, res, function() {
addArrowsFromId(req, res, function() {
pg.connect(pgc.url, function(err, client, release) {
addCamerasFromId(client, req, res, function() {
addCoinsFromId(client, req, res, function() {
addArrowsFromId(client, req, res, function() {
res.locals.path.sort(function(elt1, elt2) {
// Dates as string can be compared
if (elt1.time < elt2.time)
@ -186,6 +178,8 @@ module.exports.replay_info = function(req, res) {
}, id);
}, id);
}, id);
release();
});
}
module.exports.replay = function(req, res, next) {