CartoDB-SQL-API/lib/api/middlewares/affected-tables.js

30 lines
908 B
JavaScript
Raw Normal View History

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