2014-07-31 01:36:25 +08:00
|
|
|
var Step = require('step'),
|
|
|
|
PSQL = require(global.settings.app_root + '/app/models/psql');
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2014-07-31 01:36:25 +08:00
|
|
|
function pg(id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
pg.prototype = {
|
|
|
|
|
|
|
|
getQuery: function(sql, options) {
|
|
|
|
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
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
pg.prototype.handleQueryRow = function(row, result) {
|
2014-07-31 01:36:25 +08:00
|
|
|
if (this.hasSkipFields) {
|
|
|
|
var sf = this.opts.skipfields;
|
2013-05-29 20:36:31 +08:00
|
|
|
for ( var j=0; j<sf.length; ++j ) {
|
|
|
|
delete row[sf[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.addRow(row);
|
|
|
|
};
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-11-19 00:01:06 +08:00
|
|
|
pg.prototype.handleNotice = function(msg, result) {
|
|
|
|
if ( ! result.notices ) result.notices = [];
|
|
|
|
for (var i=0; i<msg.length; ++i) {
|
|
|
|
var m = msg[i];
|
|
|
|
result.notices.push(m);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
pg.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
|
|
|
|
2014-03-20 01:34:21 +08:00
|
|
|
if ( this.opts.profiler ) this.opts.profiler.done('gotRows');
|
|
|
|
|
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];
|
|
|
|
if ( sf.indexOf(f.name) == -1 ) newfields.push(f);
|
|
|
|
}
|
|
|
|
result.fields = newfields;
|
|
|
|
}
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
var that = this;
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
Step (
|
|
|
|
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){
|
|
|
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
2014-03-20 01:34:21 +08:00
|
|
|
if ( that.opts.profiler ) that.opts.profiler.done('packageResult');
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
// return to browser
|
2013-05-29 20:36:31 +08:00
|
|
|
if ( out ) {
|
2014-03-20 01:34:21 +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
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
pg.prototype.sendResponse = function(opts, callback) {
|
|
|
|
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();
|
|
|
|
|
2013-11-18 18:42:43 +08:00
|
|
|
this.client = new PSQL(opts.dbopts);
|
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;
|
|
|
|
}
|
2014-03-20 01:34:21 +08:00
|
|
|
if ( that.opts.profiler ) that.opts.profiler.done('eventedQuery');
|
2013-05-29 20:36:31 +08:00
|
|
|
|
|
|
|
query.on('row', that.handleQueryRow.bind(that));
|
|
|
|
query.on('end', that.handleQueryEnd.bind(that));
|
|
|
|
query.on('error', function(err) { that.error = err; });
|
2013-11-19 00:01:06 +08:00
|
|
|
query.on('notice', function(msg) {
|
|
|
|
that.handleNotice(msg, query._result);
|
|
|
|
});
|
2013-05-29 20:36:31 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-02 20:48:38 +08:00
|
|
|
pg.prototype.cancel = function() {
|
|
|
|
if (this.queryCanceller) {
|
|
|
|
this.queryCanceller.call();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
module.exports = pg;
|