2015-05-13 19:00:01 +08:00
|
|
|
var step = require('step');
|
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
var assert = require('assert');
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
function PostgresFormat(id) {
|
2014-07-31 01:36:25 +08:00
|
|
|
this.id = id;
|
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype = {
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
getQuery: function(sql/*, options*/) {
|
2013-05-27 17:21:56 +08:00
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
getContentType: function(){
|
|
|
|
return this._contentType;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFileExtension: function() {
|
|
|
|
return this.id;
|
2014-06-03 00:57:45 +08:00
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype.handleQueryRow = function(row, result) {
|
2014-07-31 01:48:57 +08:00
|
|
|
result.addRow(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
PostgresFormat.prototype.handleQueryRowWithSkipFields = function(row, result) {
|
2014-07-31 01:36:25 +08:00
|
|
|
var sf = this.opts.skipfields;
|
2013-05-29 20:36:31 +08:00
|
|
|
for ( var j=0; j<sf.length; ++j ) {
|
2014-07-31 01:48:57 +08:00
|
|
|
delete row[sf[j]];
|
2013-05-29 20:36:31 +08:00
|
|
|
}
|
2014-07-31 08:17:07 +08:00
|
|
|
this.handleQueryRow(row, result);
|
2013-05-29 20:36:31 +08:00
|
|
|
};
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype.handleNotice = function(msg, result) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( ! result.notices ) {
|
|
|
|
result.notices = [];
|
|
|
|
}
|
2014-07-31 08:17:07 +08:00
|
|
|
for (var i=0; i<msg.length; i++) {
|
|
|
|
result.notices.push(msg[i]);
|
|
|
|
}
|
2013-11-19 00:01:06 +08:00
|
|
|
};
|
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype.handleQueryEnd = function(result) {
|
2014-06-02 20:48:38 +08:00
|
|
|
this.queryCanceller = undefined;
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
if ( this.error ) {
|
|
|
|
this.callback(this.error);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( this.opts.profiler ) {
|
|
|
|
this.opts.profiler.done('gotRows');
|
|
|
|
}
|
2014-03-20 01:34:21 +08:00
|
|
|
|
2014-07-31 01:36:25 +08:00
|
|
|
this.opts.total_time = (Date.now() - this.start_time)/1000;
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-09-26 19:26:45 +08:00
|
|
|
// Drop field description for skipped fields
|
2014-07-31 01:36:25 +08:00
|
|
|
if (this.hasSkipFields) {
|
|
|
|
var sf = this.opts.skipfields;
|
2013-09-26 19:26:45 +08:00
|
|
|
var newfields = [];
|
|
|
|
for ( var j=0; j<result.fields.length; ++j ) {
|
|
|
|
var f = result.fields[j];
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( sf.indexOf(f.name) === -1 ) {
|
|
|
|
newfields.push(f);
|
|
|
|
}
|
2013-09-26 19:26:45 +08:00
|
|
|
}
|
|
|
|
result.fields = newfields;
|
|
|
|
}
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
var that = this;
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
step (
|
2013-05-29 20:36:31 +08:00
|
|
|
function packageResult() {
|
2014-03-19 20:30:29 +08:00
|
|
|
if ( that.opts.abortChecker ) {
|
|
|
|
that.opts.abortChecker('packageResult');
|
|
|
|
}
|
2013-05-29 20:36:31 +08:00
|
|
|
that.transform(result, that.opts, this);
|
2013-05-27 17:21:56 +08:00
|
|
|
},
|
|
|
|
function sendResults(err, out){
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
assert.ifError(err);
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
// return to browser
|
2013-05-29 20:36:31 +08:00
|
|
|
if ( out ) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( that.opts.beforeSink ) {
|
|
|
|
that.opts.beforeSink();
|
|
|
|
}
|
2013-05-29 20:36:31 +08:00
|
|
|
that.opts.sink.send(out);
|
|
|
|
} else {
|
2014-07-31 01:36:25 +08:00
|
|
|
console.error("No output from transform, doing nothing ?!");
|
2013-05-29 20:36:31 +08:00
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
},
|
|
|
|
function errorHandle(err){
|
2013-05-29 20:36:31 +08:00
|
|
|
that.callback(err);
|
2013-05-27 17:21:56 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype.sendResponse = function(opts, callback) {
|
2013-05-29 20:36:31 +08:00
|
|
|
if ( this.callback ) {
|
|
|
|
callback(new Error("Invalid double call to .sendResponse on a pg formatter"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.callback = callback;
|
|
|
|
this.opts = opts;
|
|
|
|
|
2014-07-31 01:36:25 +08:00
|
|
|
this.hasSkipFields = opts.skipfields.length;
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
var sql = this.getQuery(opts.sql, {
|
|
|
|
gn: opts.gn,
|
|
|
|
dp: opts.dp,
|
|
|
|
skipfields: opts.skipfields
|
|
|
|
});
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.start_time = Date.now();
|
|
|
|
|
2015-08-04 22:23:36 +08:00
|
|
|
this.client = new PSQL(opts.dbopts, {}, { destroyOnError: true });
|
2014-06-02 20:48:38 +08:00
|
|
|
this.client.eventedQuery(sql, function(err, query, queryCanceller) {
|
|
|
|
that.queryCanceller = queryCanceller;
|
2013-05-29 20:36:31 +08:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( that.opts.profiler ) {
|
|
|
|
that.opts.profiler.done('eventedQuery');
|
|
|
|
}
|
2013-05-29 20:36:31 +08:00
|
|
|
|
2014-07-31 01:48:57 +08:00
|
|
|
if (that.hasSkipFields) {
|
|
|
|
query.on('row', that.handleQueryRowWithSkipFields.bind(that));
|
|
|
|
} else {
|
|
|
|
query.on('row', that.handleQueryRow.bind(that));
|
|
|
|
}
|
2013-05-29 20:36:31 +08:00
|
|
|
query.on('end', that.handleQueryEnd.bind(that));
|
2015-03-02 21:34:01 +08:00
|
|
|
query.on('error', function(err) {
|
|
|
|
that.error = err;
|
|
|
|
if (err.message && err.message.match(/row too large, was \d* bytes/i)) {
|
2016-12-07 23:22:21 +08:00
|
|
|
return console.error(JSON.stringify({
|
2015-03-02 21:34:01 +08:00
|
|
|
username: opts.username,
|
|
|
|
type: 'row_size_limit_exceeded',
|
|
|
|
error: err.message
|
|
|
|
}));
|
|
|
|
}
|
2016-12-07 23:22:21 +08:00
|
|
|
that.handleQueryEnd();
|
2015-03-02 21:34:01 +08:00
|
|
|
});
|
2013-11-19 00:01:06 +08:00
|
|
|
query.on('notice', function(msg) {
|
2014-07-31 01:48:57 +08:00
|
|
|
that.handleNotice(msg, query._result);
|
2013-11-19 00:01:06 +08:00
|
|
|
});
|
2013-05-29 20:36:31 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
PostgresFormat.prototype.cancel = function() {
|
2014-06-02 20:48:38 +08:00
|
|
|
if (this.queryCanceller) {
|
|
|
|
this.queryCanceller.call();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-31 01:43:43 +08:00
|
|
|
module.exports = PostgresFormat;
|