2019-07-26 23:44:28 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const PSQL = require('cartodb-psql');
|
2019-09-13 19:45:43 +08:00
|
|
|
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-09-13 19:45:43 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|