2015-12-04 00:28:18 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var step = require('step');
|
|
|
|
var PSQL = require('cartodb-psql');
|
2019-07-26 17:27:46 +08:00
|
|
|
const formats = require('../models/formats');
|
2015-12-04 00:28:18 +08:00
|
|
|
var getContentDisposition = require('../utils/content_disposition');
|
2018-05-04 21:15:37 +08:00
|
|
|
const bodyParserMiddleware = require('../middlewares/body-parser');
|
2018-02-17 01:21:06 +08:00
|
|
|
const userMiddleware = require('../middlewares/user');
|
|
|
|
const errorMiddleware = require('../middlewares/error');
|
2018-02-19 22:49:17 +08:00
|
|
|
const authorizationMiddleware = require('../middlewares/authorization');
|
2018-02-22 19:22:39 +08:00
|
|
|
const connectionParamsMiddleware = require('../middlewares/connection-params');
|
2018-02-22 19:45:55 +08:00
|
|
|
const timeoutLimitsMiddleware = require('../middlewares/timeout-limits');
|
2018-02-19 22:13:36 +08:00
|
|
|
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
|
2018-03-01 19:19:57 +08:00
|
|
|
const rateLimitsMiddleware = require('../middlewares/rate-limit');
|
2018-03-01 20:15:32 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
|
2019-07-26 23:18:18 +08:00
|
|
|
const parameters = require('../middlewares/parameters');
|
2019-02-27 16:02:31 +08:00
|
|
|
const logMiddleware = require('../middlewares/log');
|
2019-07-05 21:41:26 +08:00
|
|
|
const cancelOnClientAbort = require('../middlewares/cancel-on-client-abort');
|
2019-07-26 23:44:28 +08:00
|
|
|
const affectedTables = require('../middlewares/affected-tables');
|
2019-07-27 00:05:47 +08:00
|
|
|
const accessValidator = require('../middlewares/access-validator');
|
2019-07-27 00:23:14 +08:00
|
|
|
const queryMayWrite = require('../middlewares/query-may-write');
|
2019-07-27 00:42:09 +08:00
|
|
|
const cacheControl = require('../middlewares/cache-control');
|
2019-07-27 00:56:54 +08:00
|
|
|
const cacheChannel = require('../middlewares/cache-channel');
|
|
|
|
const surrogateKey = require('../middlewares/surrogate-key');
|
2019-07-27 01:08:39 +08:00
|
|
|
const lastModified = require('../middlewares/last-modified');
|
2016-01-28 21:08:18 +08:00
|
|
|
|
2019-07-24 23:44:04 +08:00
|
|
|
function QueryController(metadataBackend, userDatabaseService, statsd_client, userLimitsService) {
|
2018-02-22 18:46:34 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2015-12-04 00:28:18 +08:00
|
|
|
this.statsd_client = statsd_client;
|
2016-01-22 00:24:48 +08:00
|
|
|
this.userDatabaseService = userDatabaseService;
|
2018-03-01 18:31:35 +08:00
|
|
|
this.userLimitsService = userLimitsService;
|
2015-12-04 00:28:18 +08:00
|
|
|
}
|
|
|
|
|
2015-12-04 01:43:13 +08:00
|
|
|
QueryController.prototype.route = function (app) {
|
2018-02-19 20:24:44 +08:00
|
|
|
const { base_url } = global.settings;
|
2018-06-05 19:14:50 +08:00
|
|
|
const forceToBeMaster = false;
|
|
|
|
|
2018-12-11 01:19:56 +08:00
|
|
|
const queryMiddlewares = () => {
|
2018-03-01 19:19:57 +08:00
|
|
|
return [
|
2018-05-04 21:15:37 +08:00
|
|
|
bodyParserMiddleware(),
|
2018-03-01 19:19:57 +08:00
|
|
|
initializeProfilerMiddleware('query'),
|
2018-05-29 19:23:50 +08:00
|
|
|
userMiddleware(this.metadataBackend),
|
2018-12-11 01:19:56 +08:00
|
|
|
rateLimitsMiddleware(this.userLimitsService, RATE_LIMIT_ENDPOINTS_GROUPS.QUERY),
|
2018-06-05 19:14:50 +08:00
|
|
|
authorizationMiddleware(this.metadataBackend, forceToBeMaster),
|
2018-03-01 19:19:57 +08:00
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2019-07-26 23:18:18 +08:00
|
|
|
parameters({ strategy: 'query' }),
|
2019-02-28 18:49:05 +08:00
|
|
|
logMiddleware(logMiddleware.TYPES.QUERY),
|
2019-07-05 21:41:26 +08:00
|
|
|
cancelOnClientAbort(),
|
2019-07-26 23:44:28 +08:00
|
|
|
affectedTables(),
|
2019-07-27 00:05:47 +08:00
|
|
|
accessValidator(),
|
2019-07-27 00:23:14 +08:00
|
|
|
queryMayWrite(),
|
2019-07-27 00:42:09 +08:00
|
|
|
cacheControl(),
|
2019-07-27 00:56:54 +08:00
|
|
|
cacheChannel(),
|
|
|
|
surrogateKey(),
|
2019-07-27 01:08:39 +08:00
|
|
|
lastModified(),
|
2018-03-01 19:19:57 +08:00
|
|
|
this.handleQuery.bind(this),
|
|
|
|
errorMiddleware()
|
|
|
|
];
|
2018-03-01 21:47:34 +08:00
|
|
|
};
|
2018-03-01 19:19:57 +08:00
|
|
|
|
2018-12-11 01:19:56 +08:00
|
|
|
app.all(`${base_url}/sql`, queryMiddlewares());
|
|
|
|
app.all(`${base_url}/sql.:f`, queryMiddlewares());
|
2015-12-04 00:28:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// jshint maxcomplexity:21
|
2018-02-17 01:21:06 +08:00
|
|
|
QueryController.prototype.handleQuery = function (req, res, next) {
|
2015-12-07 16:40:51 +08:00
|
|
|
var self = this;
|
2018-02-19 20:24:44 +08:00
|
|
|
|
2019-07-26 23:44:28 +08:00
|
|
|
const {
|
|
|
|
user: username,
|
|
|
|
userDbParams: dbopts,
|
|
|
|
userLimits,
|
2019-07-27 00:23:14 +08:00
|
|
|
affectedTables,
|
|
|
|
mayWrite
|
2019-07-26 23:44:28 +08:00
|
|
|
} = res.locals;
|
2019-07-26 17:27:46 +08:00
|
|
|
const { orderBy, sortOrder, limit, offset } = res.locals.params;
|
2019-07-26 23:44:28 +08:00
|
|
|
const { sql, format, skipfields, decimalPrecision, filename, callback } = res.locals.params;
|
2015-12-04 00:28:18 +08:00
|
|
|
|
|
|
|
try {
|
2019-07-26 17:27:46 +08:00
|
|
|
let formatter;
|
2015-12-04 00:28:18 +08:00
|
|
|
|
2019-07-26 17:27:46 +08:00
|
|
|
if (req.profiler) {
|
2015-12-04 00:28:18 +08:00
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:44:28 +08:00
|
|
|
// 1. Setup headers
|
|
|
|
// 2. Send formatted results back
|
|
|
|
// 3. Handle error
|
2015-12-04 00:28:18 +08:00
|
|
|
step(
|
2019-07-26 23:44:28 +08:00
|
|
|
function setHeaders() {
|
2015-12-04 00:28:18 +08:00
|
|
|
var FormatClass = formats[format];
|
|
|
|
formatter = new FormatClass();
|
|
|
|
req.formatter = formatter;
|
|
|
|
|
|
|
|
// configure headers for given format
|
2019-07-26 17:27:46 +08:00
|
|
|
var useInline = (!req.query.format && !req.body.format && !req.query.filename && !req.body.filename);
|
|
|
|
res.header("Content-Disposition", getContentDisposition(formatter, filename, useInline));
|
2015-12-04 00:28:18 +08:00
|
|
|
res.header("Content-Type", formatter.getContentType());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function generateFormat(err){
|
2018-11-08 01:05:39 +08:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-12-04 00:28:18 +08:00
|
|
|
|
|
|
|
var opts = {
|
2018-02-19 20:24:44 +08:00
|
|
|
username: username,
|
2015-12-04 00:28:18 +08:00
|
|
|
dbopts: dbopts,
|
|
|
|
sink: res,
|
2019-07-26 17:27:46 +08:00
|
|
|
gn: 'the_geom', // TODO: read from configuration FILE,
|
|
|
|
dp: decimalPrecision,
|
2015-12-04 00:28:18 +08:00
|
|
|
skipfields: skipfields,
|
2019-07-26 23:44:28 +08:00
|
|
|
sql: new PSQL.QueryWrapper(sql).orderBy(orderBy, sortOrder).window(limit, offset).query(),
|
2015-12-04 00:28:18 +08:00
|
|
|
filename: filename,
|
|
|
|
bufferedRows: global.settings.bufferedRows,
|
2019-07-26 17:27:46 +08:00
|
|
|
callback: callback,
|
2017-08-09 18:50:16 +08:00
|
|
|
timeout: userLimits.timeout
|
2015-12-04 00:28:18 +08:00
|
|
|
};
|
|
|
|
|
2019-07-26 17:27:46 +08:00
|
|
|
if (req.profiler) {
|
2015-12-04 00:28:18 +08:00
|
|
|
opts.profiler = req.profiler;
|
|
|
|
opts.beforeSink = function() {
|
|
|
|
req.profiler.done('beforeSink');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbopts.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', dbopts.host);
|
|
|
|
}
|
2019-02-27 16:02:31 +08:00
|
|
|
|
2015-12-04 00:28:18 +08:00
|
|
|
formatter.sendResponse(opts, this);
|
|
|
|
},
|
|
|
|
function errorHandle(err){
|
|
|
|
formatter = null;
|
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
2015-12-04 00:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
2018-02-19 22:13:36 +08:00
|
|
|
req.profiler.sendStats();
|
2015-12-04 00:28:18 +08:00
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
if (self.statsd_client) {
|
2015-12-04 00:28:18 +08:00
|
|
|
if ( err ) {
|
2015-12-07 16:40:51 +08:00
|
|
|
self.statsd_client.increment('sqlapi.query.error');
|
2015-12-04 00:28:18 +08:00
|
|
|
} else {
|
2015-12-07 16:40:51 +08:00
|
|
|
self.statsd_client.increment('sqlapi.query.success');
|
2015-12-04 00:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2018-02-17 01:21:06 +08:00
|
|
|
next(err);
|
2015-12-04 00:28:18 +08:00
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
if (self.statsd_client) {
|
|
|
|
self.statsd_client.increment('sqlapi.query.error');
|
2015-12-04 00:28:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = QueryController;
|