CartoDB-SQL-API/app/models/formats/json.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

var pg = require('./pg');
2013-05-16 17:24:52 +08:00
function json() {}
2013-05-16 17:24:52 +08:00
json.prototype = new pg('json');
2013-05-16 17:24:52 +08:00
var p = json.prototype;
2013-05-16 17:24:52 +08:00
p._contentType = "application/json; charset=utf-8";
2013-05-16 17:24:52 +08:00
p.formatResultFields = function(flds) {
var nfields = {};
for (var i=0; i<flds.length; ++i) {
var f = flds[i];
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/) ) {
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-11-09 23:56:58 +08:00
//console.log('cname:'+cname+' tname:'+tname);
nfields[f.name] = { type: tname };
}
return nfields;
}
p.transform = function(result, options, callback) {
var j = {
time: options.total_time,
fields: this.formatResultFields(result.fields),
total_rows: result.rowCount,
rows: result.rows
2013-05-16 17:24:52 +08:00
}
if ( result.notices ) {
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);
}
}
callback(null, j);
2013-05-16 17:24:52 +08:00
};
module.exports = json;