Revert "Stream JSON responses"

This reverts commit 49ef1bc0c7.

Conflicts:
	NEWS.md
This commit is contained in:
Raul Ochoa 2014-06-04 20:57:11 +02:00
parent 55dcb50809
commit 7906707130
2 changed files with 12 additions and 83 deletions

View File

@ -1,5 +1,8 @@
1.10.1 - 2014-mm-dd
-------------------
Bug fixes:
* Backing out Stream JSON responses
1.10.0 - 2014-06-04
-------------------

View File

@ -1,5 +1,4 @@
var pg = require('./pg');
var util = require('util');
function json() {}
@ -43,96 +42,23 @@ p.formatResultFields = function(flds) {
return nfields;
}
p.startStreaming = function() {
if ( this.opts.profiler ) this.opts.profiler.done('startStreaming');
this.total_rows = 0;
if ( this.opts.beforeSink ) {
this.opts.beforeSink();
delete this.opts.beforeSink;
p.transform = function(result, options, callback) {
var j = {
time: options.total_time,
fields: this.formatResultFields(result.fields),
total_rows: result.rowCount,
rows: result.rows
}
var out = '{"rows":[';
this.opts.sink.write(out);
this._streamingStarted = true;
};
p.handleQueryRow = function(row) {
if ( ! this._streamingStarted ) {
this.startStreaming();
}
var sf = this.opts.skipfields;
if ( sf.length ){
for ( var j=0; j<sf.length; ++j ) {
delete row[sf[j]];
}
}
var out = ( this.total_rows ? ',' : '' ) + JSON.stringify(row);
this.opts.sink.write(out);
this.total_rows++;
}
p.handleQueryEnd = function(result) {
if ( this.error ) {
this.callback(this.error);
return;
}
if ( this.opts.profiler ) this.opts.profiler.done('gotRows');
//console.log("Got query end, result is "); console.dir(result);
if ( ! this._streamingStarted ) {
this.startStreaming();
}
var end = Date.now();
this.opts.total_time = (end - this.start_time)/1000;
// Drop field description for skipped fields
var newfields = [];
var sf = this.opts.skipfields;
if ( sf.length ){
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;
}
var end = Date.now();
var total_time = (end - this.start_time)/1000;
var out = [
'],', // end of "rows" array
'"time":', JSON.stringify(total_time),
',"fields":', JSON.stringify(this.formatResultFields(result.fields)),
',"total_rows":', JSON.stringify(result.rowCount)
];
if ( result.notices ) {
var j = {};
for (var i=0; i<result.notices.length; ++i) {
var m = result.notices[i];
var l = m.severity.toLowerCase() + 's';
if ( ! j[l] ) j[l] = [];
j[l].push(m.message);
}
for (var s in j) {
out.push(',"' + s + '":');
out.push( JSON.stringify(j[s]) );
}
delete j;
}
out.push('}');
while (out.length) {
var cmp = out.shift();
this.opts.sink.write(cmp);
}
this.opts.sink.end();
if ( this.opts.profiler ) this.opts.profiler.done('endStreaming');
this.callback();
}
callback(null, j);
};
module.exports = json;