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 = {
|
|
|
|
16: 'string', // bool
|
|
|
|
17: 'string', // bytea
|
|
|
|
20: 'number',
|
|
|
|
21: 'number',
|
|
|
|
23: 'number',
|
|
|
|
26: 'number',
|
|
|
|
114: 'object', // JSON
|
|
|
|
701: 'number',
|
|
|
|
1015: 'string[]', // _varchar
|
|
|
|
1042: 'string',
|
|
|
|
1043: 'string',
|
|
|
|
1005: 'number[]', // _int2
|
|
|
|
1007: 'number[]', // _int4
|
|
|
|
1014: 'string[]', // _bpchar
|
|
|
|
1016: 'number[]', // _int8
|
|
|
|
1021: 'number[]', // _float4
|
|
|
|
1022: 'number[]', // _float8
|
|
|
|
1008: 'string[]',
|
|
|
|
1009: 'string[]',
|
|
|
|
1114: 'date', // timestamp without timezone
|
|
|
|
1182: 'date', // date
|
|
|
|
1184: 'date', // timestamp
|
|
|
|
1186: 'string', // interval
|
|
|
|
1231: 'number[]', // _numeric
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function formatResultFields(flds) {
|
|
|
|
var nfields = {};
|
|
|
|
for (var i=0; i<flds.length; ++i) {
|
|
|
|
var f = flds[i];
|
|
|
|
/*
|
|
|
|
{ name: 'the_geom',
|
|
|
|
tableID: 5528687,
|
|
|
|
columnID: 6,
|
|
|
|
dataTypeID: 77869,
|
|
|
|
dataTypeSize: -1,
|
|
|
|
dataTypeModifier: -1,
|
|
|
|
format: 'text' },
|
|
|
|
*/
|
|
|
|
var tname = typeNames[f.dataTypeID];
|
|
|
|
if ( ! tname ) {
|
|
|
|
if ( f.name.match(/^the_geom/) ) {
|
|
|
|
tname = 'geometry';
|
|
|
|
} else {
|
|
|
|
tname = f.dataTypeID; // unknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nfields[f.name] = { type: tname };
|
|
|
|
}
|
|
|
|
return nfields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p.transform = function(result, options, callback) {
|
|
|
|
var j = {
|
|
|
|
time: options.total_time,
|
2013-06-15 00:35:04 +08:00
|
|
|
fields: 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;
|