2013-05-27 17:21:56 +08:00
|
|
|
var pg = require('./pg');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
function json() {}
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
json.prototype = new pg('json');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var p = json.prototype;
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p._contentType = "application/json; charset=utf-8";
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-06-15 00:35:04 +08:00
|
|
|
var typeNames = {
|
2013-08-22 19:44:21 +08:00
|
|
|
16: 'boolean', // bool
|
2013-06-15 00:35:04 +08:00
|
|
|
17: 'string', // bytea
|
2013-07-10 03:42:41 +08:00
|
|
|
20: 'number', // int8
|
|
|
|
21: 'number', // int2
|
|
|
|
23: 'number', // int4
|
2013-07-04 22:03:05 +08:00
|
|
|
25: 'string', // text
|
2013-07-10 03:42:41 +08:00
|
|
|
26: 'number', // oid
|
2013-06-15 00:35:04 +08:00
|
|
|
114: 'object', // JSON
|
2013-07-10 03:42:41 +08:00
|
|
|
700: 'number', // float4
|
|
|
|
701: 'number', // float8
|
2013-08-22 19:44:21 +08:00
|
|
|
1000: 'boolean[]', // _bool
|
2013-06-15 00:35:04 +08:00
|
|
|
1015: 'string[]', // _varchar
|
2013-07-10 03:42:41 +08:00
|
|
|
1042: 'string', // bpchar
|
|
|
|
1043: 'string', // varchar
|
2013-06-15 00:35:04 +08:00
|
|
|
1005: 'number[]', // _int2
|
|
|
|
1007: 'number[]', // _int4
|
|
|
|
1014: 'string[]', // _bpchar
|
|
|
|
1016: 'number[]', // _int8
|
|
|
|
1021: 'number[]', // _float4
|
|
|
|
1022: 'number[]', // _float8
|
2013-07-10 03:42:41 +08:00
|
|
|
1008: 'string[]', // _regproc
|
|
|
|
1009: 'string[]', // _text
|
2013-06-15 00:35:04 +08:00
|
|
|
1114: 'date', // timestamp without timezone
|
|
|
|
1182: 'date', // date
|
|
|
|
1184: 'date', // timestamp
|
|
|
|
1186: 'string', // interval
|
|
|
|
1231: 'number[]', // _numeric
|
2013-07-10 03:42:41 +08:00
|
|
|
1700: 'number', // numeric
|
2013-06-15 00:35:04 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-10-02 16:22:13 +08:00
|
|
|
p.formatResultFields = function(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;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-11-09 23:55:40 +08:00
|
|
|
console.log('cname:'+cname+' tname:'+tname);
|
2013-06-15 00:35:04 +08:00
|
|
|
nfields[f.name] = { type: tname };
|
|
|
|
}
|
|
|
|
return nfields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p.transform = function(result, options, callback) {
|
2013-11-09 23:55:40 +08:00
|
|
|
//console.log(result.fields, this.formatResultFields(result.fields));
|
2013-05-27 17:21:56 +08:00
|
|
|
var j = {
|
|
|
|
time: options.total_time,
|
2013-10-02 16:22:13 +08:00
|
|
|
fields: this.formatResultFields(result.fields),
|
2013-05-27 17:21:56 +08:00
|
|
|
total_rows: result.rowCount,
|
|
|
|
rows: result.rows
|
2013-05-16 17:24:52 +08:00
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
callback(null, j);
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
module.exports = json;
|