2013-05-27 17:21:56 +08:00
|
|
|
var Step = require('step')
|
|
|
|
var PSQL = require(global.settings.app_root + '/app/models/psql')
|
|
|
|
|
|
|
|
function pg(id) { this.id = id; }
|
|
|
|
|
|
|
|
pg.prototype = {
|
|
|
|
|
|
|
|
getQuery: function(sql, options) {
|
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
getContentType: function(){
|
|
|
|
return this._contentType;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFileExtension: function() {
|
|
|
|
return this.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
pg.prototype.handleQueryRow = function(row, result) {
|
|
|
|
//console.log("Got query row, row is "); console.dir(row);
|
|
|
|
//console.log("opts are: "); console.dir(this.opts);
|
|
|
|
var sf = this.opts.skipfields;
|
|
|
|
if ( sf.length ){
|
|
|
|
for ( var j=0; j<sf.length; ++j ) {
|
|
|
|
delete row[sf[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.addRow(row);
|
|
|
|
};
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
pg.prototype.handleQueryEnd = function(result) {
|
|
|
|
if ( this.error ) {
|
|
|
|
this.callback(this.error);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
//console.log("Got query end, result is "); console.dir(result);
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-05-29 20:36:31 +08:00
|
|
|
var end = Date.now();
|
|
|
|
this.opts.total_time = (end - 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
|
|
|
|
var sf = this.opts.skipfields;
|
|
|
|
if ( sf.length ){
|
|
|
|
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() {
|
|
|
|
that.transform(result, that.opts, this);
|
2013-05-27 17:21:56 +08:00
|
|
|
},
|
|
|
|
function sendResults(err, out){
|
|
|
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
// return to browser
|
2013-05-29 20:36:31 +08:00
|
|
|
if ( out ) {
|
|
|
|
that.opts.sink.send(out);
|
|
|
|
} else {
|
|
|
|
console.error("No output from transform, doing nothing ?!");
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
var sql = this.getQuery(opts.sql, {
|
|
|
|
gn: opts.gn,
|
|
|
|
dp: opts.dp,
|
|
|
|
skipfields: opts.skipfields
|
|
|
|
});
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.start_time = Date.now();
|
|
|
|
|
2013-10-02 16:22:13 +08:00
|
|
|
this.client = new PSQL(opts.user_id, opts.database);
|
|
|
|
this.client.eventedQuery(sql, function(err, query) {
|
2013-05-29 20:36:31 +08:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.on('row', that.handleQueryRow.bind(that));
|
|
|
|
query.on('end', that.handleQueryEnd.bind(that));
|
|
|
|
query.on('error', function(err) { that.error = err; });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
module.exports = pg;
|