CartoDB-SQL-API/lib/batch/query-runner.js

31 lines
948 B
JavaScript
Raw Normal View History

'use strict';
var PSQL = require('cartodb-psql');
2019-12-24 01:19:08 +08:00
function QueryRunner (userDatabaseMetadataService, logger) {
this.userDatabaseMetadataService = userDatabaseMetadataService;
this.logger = logger;
}
2016-05-14 00:50:55 +08:00
module.exports = QueryRunner;
function hasDBParams (dbparams) {
return (dbparams.user && dbparams.host && dbparams.port && dbparams.dbname && dbparams.pass);
}
2019-12-27 01:12:47 +08:00
QueryRunner.prototype.run = function (jobId, sql, user, timeout, dbparams, callback) {
if (hasDBParams(dbparams)) {
2019-12-27 01:12:47 +08:00
return this._run(dbparams, jobId, sql, timeout, callback);
}
const dbConfigurationError = new Error('Batch Job DB misconfiguration');
return callback(dbConfigurationError);
};
2019-12-27 01:12:47 +08:00
QueryRunner.prototype._run = function (dbparams, jobId, sql, timeout, callback) {
var pg = new PSQL(dbparams);
this.logger.debug('Running query [timeout=%d] %s', timeout, sql);
2019-12-27 01:12:47 +08:00
pg.query(`/* ${jobId} */ ${sql}`, callback, false, timeout);
};