CartoDB-SQL-API/lib/api/middlewares/affected-tables.js
2019-10-15 13:23:43 +02:00

30 lines
908 B
JavaScript

'use strict';
const PSQL = require('cartodb-psql');
const queryTables = require('cartodb-query-tables').queryTables;
module.exports = function affectedTables () {
return function affectedTablesMiddleware (req, res, next) {
const { sql } = res.locals.params;
const { authDbParams } = res.locals;
const pg = new PSQL(authDbParams);
queryTables.getQueryMetadataModel(pg, sql)
.then(affectedTables => {
res.locals.affectedTables = affectedTables;
if (req.profiler) {
req.profiler.done('queryExplain');
}
return next();
})
.catch(err => {
const message = (err && err.message) || 'unknown error';
console.error('Error on query explain \'%s\': %s', sql, message);
return next();
});
};
};