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