2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
var Pg = require('./../pg');
|
2019-01-02 23:15:28 +08:00
|
|
|
const errorHandlerFactory = require('../../../services/error_handler_factory');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
function JsonFormat() {
|
|
|
|
this.buffer = '';
|
2014-12-01 23:55:59 +08:00
|
|
|
this.lastKnownResult = {};
|
2014-07-31 08:17:07 +08:00
|
|
|
}
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
JsonFormat.prototype = new Pg('json');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
JsonFormat.prototype._contentType = "application/json; charset=utf-8";
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-06-03 23:45:38 +08:00
|
|
|
// jshint maxcomplexity:10
|
2014-07-31 08:17:07 +08:00
|
|
|
JsonFormat.prototype.formatResultFields = function(flds) {
|
2014-12-01 23:55:59 +08:00
|
|
|
flds = flds || [];
|
2013-06-15 00:35:04 +08:00
|
|
|
var nfields = {};
|
|
|
|
for (var i=0; i<flds.length; ++i) {
|
|
|
|
var f = flds[i];
|
2013-10-02 16:22:13 +08:00
|
|
|
var cname = this.client.typeName(f.dataTypeID);
|
|
|
|
var tname;
|
2019-06-03 23:37:40 +08:00
|
|
|
|
2013-10-02 16:22:13 +08:00
|
|
|
if ( ! cname ) {
|
|
|
|
tname = 'unknown(' + f.dataTypeID + ')';
|
|
|
|
} else {
|
|
|
|
if ( cname.match('bool') ) {
|
|
|
|
tname = 'boolean';
|
|
|
|
}
|
2013-11-09 23:55:40 +08:00
|
|
|
else if ( cname.match(/int|float|numeric/) ) {
|
2013-10-02 16:22:13 +08:00
|
|
|
tname = 'number';
|
|
|
|
}
|
|
|
|
else if ( cname.match(/text|char|unknown/) ) {
|
|
|
|
tname = 'string';
|
|
|
|
}
|
|
|
|
else if ( cname.match(/date|time/) ) {
|
|
|
|
tname = 'date';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tname = cname;
|
|
|
|
}
|
|
|
|
if ( tname && cname.match(/^_/) ) {
|
|
|
|
tname += '[]';
|
2013-06-15 00:35:04 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:37:40 +08:00
|
|
|
|
2019-06-04 02:42:49 +08:00
|
|
|
if (['geography', 'geometry'].includes(cname)) {
|
|
|
|
let { wkbtype, ndims, srid } = this.client.typeModInfo(f.dataTypeModifier);
|
2019-06-03 23:37:40 +08:00
|
|
|
nfields[f.name] = { type: tname, wkbtype, dims: ndims, srid };
|
|
|
|
} else {
|
|
|
|
nfields[f.name] = { type: tname, pgtype: cname };
|
|
|
|
}
|
|
|
|
|
2013-06-15 00:35:04 +08:00
|
|
|
}
|
|
|
|
return nfields;
|
2014-07-31 08:17:07 +08:00
|
|
|
};
|
2013-06-15 00:35:04 +08:00
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
JsonFormat.prototype.startStreaming = function() {
|
|
|
|
this.total_rows = 0;
|
|
|
|
if (this.opts.beforeSink) {
|
|
|
|
this.opts.beforeSink();
|
|
|
|
}
|
|
|
|
if (this.opts.callback) {
|
|
|
|
this.buffer += this.opts.callback + '(';
|
|
|
|
}
|
|
|
|
this.buffer += '{"rows":[';
|
|
|
|
this._streamingStarted = true;
|
|
|
|
};
|
2013-06-15 00:35:04 +08:00
|
|
|
|
2014-12-01 23:55:59 +08:00
|
|
|
JsonFormat.prototype.handleQueryRow = function(row, result) {
|
2014-07-31 08:17:07 +08:00
|
|
|
if ( ! this._streamingStarted ) {
|
|
|
|
this.startStreaming();
|
2013-11-19 00:01:06 +08:00
|
|
|
}
|
2014-07-31 08:17:07 +08:00
|
|
|
|
2014-12-01 23:55:59 +08:00
|
|
|
this.lastKnownResult = result;
|
|
|
|
|
2017-06-08 18:42:37 +08:00
|
|
|
this.buffer += (this.total_rows++ ? ',' : '') + JSON.stringify(row, function (key, value) {
|
|
|
|
if (value !== value) {
|
|
|
|
return 'NaN';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === Infinity) {
|
|
|
|
return 'Infinity';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === -Infinity) {
|
|
|
|
return '-Infinity';
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
});
|
2014-07-31 08:17:07 +08:00
|
|
|
|
2014-12-02 19:32:54 +08:00
|
|
|
if (this.total_rows % (this.opts.bufferedRows || 1000)) {
|
2014-07-31 08:17:07 +08:00
|
|
|
this.opts.sink.write(this.buffer);
|
|
|
|
this.buffer = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
// jshint maxcomplexity:13
|
2014-07-31 08:17:07 +08:00
|
|
|
JsonFormat.prototype.handleQueryEnd = function(result) {
|
2014-12-01 23:55:59 +08:00
|
|
|
if (this.error && !this._streamingStarted) {
|
2014-07-31 08:17:07 +08:00
|
|
|
this.callback(this.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( this.opts.profiler ) {
|
|
|
|
this.opts.profiler.done('gotRows');
|
|
|
|
}
|
2014-07-31 08:17:07 +08:00
|
|
|
|
|
|
|
if ( ! this._streamingStarted ) {
|
|
|
|
this.startStreaming();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.opts.total_time = (Date.now() - this.start_time)/1000;
|
|
|
|
|
2014-12-01 23:55:59 +08:00
|
|
|
result = result || this.lastKnownResult || {};
|
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
// Drop field description for skipped fields
|
|
|
|
if (this.hasSkipFields) {
|
|
|
|
var newfields = [];
|
|
|
|
var sf = this.opts.skipfields;
|
|
|
|
for (var i = 0; i < result.fields.length; i++) {
|
|
|
|
var f = result.fields[i];
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( sf.indexOf(f.name) === -1 ) {
|
|
|
|
newfields.push(f);
|
|
|
|
}
|
2014-07-31 08:17:07 +08:00
|
|
|
}
|
|
|
|
result.fields = newfields;
|
|
|
|
}
|
|
|
|
|
|
|
|
var total_time = (Date.now() - this.start_time)/1000;
|
|
|
|
|
|
|
|
var out = [
|
|
|
|
'],', // end of "rows" array
|
|
|
|
'"time":', JSON.stringify(total_time),
|
|
|
|
',"fields":', JSON.stringify(this.formatResultFields(result.fields)),
|
2014-12-01 23:55:59 +08:00
|
|
|
',"total_rows":', JSON.stringify(result.rowCount || this.total_rows)
|
2014-07-31 08:17:07 +08:00
|
|
|
];
|
|
|
|
|
2014-12-01 23:55:59 +08:00
|
|
|
if (this.error) {
|
2019-01-02 23:15:28 +08:00
|
|
|
out.push(',"error":', JSON.stringify(errorHandlerFactory(this.error).getResponse().error));
|
2014-12-01 23:55:59 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
|
|
|
|
if ( result.notices && result.notices.length > 0 ) {
|
|
|
|
var notices = {},
|
|
|
|
severities = [];
|
|
|
|
_.each(result.notices, function(notice) {
|
|
|
|
var severity = notice.severity.toLowerCase() + 's';
|
|
|
|
if (!notices[severity]) {
|
|
|
|
severities.push(severity);
|
|
|
|
notices[severity] = [];
|
|
|
|
}
|
2015-05-13 19:00:01 +08:00
|
|
|
notices[severity].push(notice.message);
|
2014-07-31 08:17:07 +08:00
|
|
|
});
|
|
|
|
_.each(severities, function(severity) {
|
|
|
|
out.push(',');
|
|
|
|
out.push(JSON.stringify(severity));
|
|
|
|
out.push(':');
|
|
|
|
out.push(JSON.stringify(notices[severity]));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push('}');
|
|
|
|
|
|
|
|
|
|
|
|
this.buffer += out.join('');
|
|
|
|
|
|
|
|
if (this.opts.callback) {
|
|
|
|
this.buffer += ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.opts.sink.write(this.buffer);
|
|
|
|
this.opts.sink.end();
|
|
|
|
this.buffer = '';
|
|
|
|
|
|
|
|
this.callback();
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
2014-07-31 08:17:07 +08:00
|
|
|
module.exports = JsonFormat;
|