2016-03-31 18:39:03 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
|
2019-04-04 20:31:41 +08:00
|
|
|
function QueryRunner(userDatabaseMetadataService, logger) {
|
2016-05-23 23:51:56 +08:00
|
|
|
this.userDatabaseMetadataService = userDatabaseMetadataService;
|
2019-04-04 20:31:41 +08:00
|
|
|
this.logger = logger;
|
2016-03-31 18:39:03 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
module.exports = QueryRunner;
|
2016-03-31 18:39:03 +08:00
|
|
|
|
2018-02-15 19:25:55 +08:00
|
|
|
function hasDBParams (dbparams) {
|
|
|
|
return (dbparams.user && dbparams.host && dbparams.port && dbparams.dbname && dbparams.pass);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryRunner.prototype.run = function (job_id, sql, user, timeout, dbparams, callback) {
|
|
|
|
if (hasDBParams(dbparams)) {
|
|
|
|
return this._run(dbparams, job_id, sql, timeout, callback);
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:25:38 +08:00
|
|
|
const dbConfigurationError = new Error('Batch Job DB misconfiguration');
|
2016-03-31 18:39:03 +08:00
|
|
|
|
2019-04-04 20:31:41 +08:00
|
|
|
return callback(dbConfigurationError);
|
2018-02-15 19:25:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
QueryRunner.prototype._run = function (dbparams, job_id, sql, timeout, callback) {
|
|
|
|
var pg = new PSQL(dbparams);
|
2019-09-13 17:28:37 +08:00
|
|
|
this.logger.debug('Running query [timeout=%d] %s', timeout, sql);
|
|
|
|
pg.query(`/* ${job_id} */ ${sql}`, callback, false, timeout);
|
2016-03-31 18:39:03 +08:00
|
|
|
};
|