2014-08-03 02:26:01 +08:00
|
|
|
var pg = require('./pg'),
|
|
|
|
ArrayBufferSer = require("../bin_encoder"),
|
|
|
|
_ = require('underscore');
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
function BinaryFormat() {}
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
BinaryFormat.prototype = new pg('arraybuffer');
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
BinaryFormat.prototype._contentType = "application/octet-stream";
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
BinaryFormat.prototype._extractTypeFromName = function(name) {
|
2013-05-17 17:44:50 +08:00
|
|
|
var g = name.match(new RegExp(/.*__(uintclamp|uint|int|float)(8|16|32)/i))
|
|
|
|
if(g && g.length == 3) {
|
|
|
|
var typeName = g[1] + g[2];
|
|
|
|
return ArrayBufferSer.typeNames[typeName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
BinaryFormat.prototype.transform = function(result, options, callback) {
|
2013-05-17 17:44:50 +08:00
|
|
|
var total_rows = result.rowCount;
|
2014-08-03 02:23:47 +08:00
|
|
|
var rows = result.rows;
|
2013-05-17 17:44:50 +08:00
|
|
|
|
|
|
|
// get headers
|
|
|
|
if(!total_rows) {
|
|
|
|
callback(null, new Buffer(0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var headersNames = Object.keys(rows[0]);
|
|
|
|
var headerTypes = [];
|
|
|
|
|
|
|
|
if(_.contains(headersNames, 'the_geom')) {
|
|
|
|
callback(new Error("geometry types are not supported"), null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// get header types (and guess from name)
|
|
|
|
for(var i = 0; i < headersNames.length; ++i) {
|
|
|
|
var r = rows[0];
|
|
|
|
var n = headersNames[i];
|
|
|
|
if(typeof(r[n]) == 'string') {
|
|
|
|
headerTypes.push(ArrayBufferSer.STRING);
|
|
|
|
} else if(typeof(r[n]) == 'object') {
|
|
|
|
var t = this._extractTypeFromName(n);
|
|
|
|
t = t == undefined ? ArrayBufferSer.FLOAT32: t;
|
|
|
|
headerTypes.push(ArrayBufferSer.BUFFER + t);
|
|
|
|
} else {
|
|
|
|
var t = this._extractTypeFromName(n);
|
|
|
|
headerTypes.push(t == undefined ? ArrayBufferSer.FLOAT32: t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pack the data
|
|
|
|
var header = new ArrayBufferSer(ArrayBufferSer.STRING, headersNames);
|
|
|
|
var data = [header];
|
|
|
|
for(var i = 0; i < headersNames.length; ++i) {
|
|
|
|
var d = [];
|
|
|
|
var n = headersNames[i];
|
|
|
|
for(var r = 0; r < total_rows; ++r) {
|
|
|
|
var row = rows[r][n];
|
|
|
|
if(headerTypes[i] > ArrayBufferSer.BUFFER) {
|
|
|
|
row = new ArrayBufferSer(headerTypes[i] - ArrayBufferSer.BUFFER, row);
|
|
|
|
}
|
|
|
|
d.push(row);
|
2014-08-03 02:23:47 +08:00
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
var b = new ArrayBufferSer(headerTypes[i], d);
|
|
|
|
data.push(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the final buffer
|
|
|
|
var all = new ArrayBufferSer(ArrayBufferSer.BUFFER, data);
|
|
|
|
|
|
|
|
callback(null, all.buffer);
|
|
|
|
|
|
|
|
} catch(e) {
|
|
|
|
callback(e, null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
module.exports = BinaryFormat;
|