2016-03-31 18:39:03 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
|
|
|
|
function QueryRunner() {
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
module.exports = QueryRunner;
|
2016-03-31 18:39:03 +08:00
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
QueryRunner.prototype.run = function (job_id, sql, userDatabaseMetadata, callback) {
|
2016-03-31 18:39:03 +08:00
|
|
|
var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true });
|
|
|
|
|
|
|
|
pg.query('SET statement_timeout=0', function (err) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark query to allow to users cancel their queries
|
|
|
|
sql = '/* ' + job_id + ' */ ' + sql;
|
|
|
|
|
|
|
|
pg.eventedQuery(sql, function (err, query) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
query.on('error', callback);
|
|
|
|
|
|
|
|
query.on('end', function (result) {
|
|
|
|
// only if result is present then query is done sucessfully otherwise an error has happened
|
|
|
|
// and it was handled by error listener
|
|
|
|
if (result) {
|
|
|
|
callback(null, result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|