CartoDB-SQL-API/app/middlewares/affected-tables.js

31 lines
869 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);
queryTables.getQueryMetadataModel(pg, sql, (err, affectedTables) => {
2019-07-26 23:44:28 +08:00
if (err) {
const message = (err && err.message) || 'unknown error';
console.error('Error on query explain \'%s\': %s', sql, message);
return next();
}
res.locals.affectedTables = affectedTables;
2019-07-27 00:05:47 +08:00
if (req.profiler) {
req.profiler.done('queryExplain');
}
2019-07-26 23:44:28 +08:00
return next();
});
};
};