2015-05-13 19:00:01 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
|
|
|
|
var pg = require('./pg');
|
|
|
|
var ArrayBufferSer = require("../bin_encoder");
|
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) {
|
2015-05-13 19:00:01 +08:00
|
|
|
var g = name.match(/.*__(uintclamp|uint|int|float)(8|16|32)/i);
|
|
|
|
if(g && g.length === 3) {
|
2013-05-17 17:44:50 +08:00
|
|
|
var typeName = g[1] + g[2];
|
|
|
|
return ArrayBufferSer.typeNames[typeName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
// jshint maxcomplexity:12
|
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 {
|
2015-05-13 19:00:01 +08:00
|
|
|
var i;
|
|
|
|
var t;
|
2013-05-17 17:44:50 +08:00
|
|
|
// get header types (and guess from name)
|
2015-05-13 19:00:01 +08:00
|
|
|
for(i = 0; i < headersNames.length; ++i) {
|
|
|
|
r = rows[0];
|
|
|
|
n = headersNames[i];
|
|
|
|
if(typeof(r[n]) === 'string') {
|
2013-05-17 17:44:50 +08:00
|
|
|
headerTypes.push(ArrayBufferSer.STRING);
|
2015-05-13 19:00:01 +08:00
|
|
|
} else if(typeof(r[n]) === 'object') {
|
|
|
|
t = this._extractTypeFromName(n);
|
|
|
|
t = t || ArrayBufferSer.FLOAT32;
|
2013-05-17 17:44:50 +08:00
|
|
|
headerTypes.push(ArrayBufferSer.BUFFER + t);
|
|
|
|
} else {
|
2015-05-13 19:00:01 +08:00
|
|
|
t = this._extractTypeFromName(n);
|
|
|
|
headerTypes.push(t || ArrayBufferSer.FLOAT32);
|
2013-05-17 17:44:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pack the data
|
|
|
|
var header = new ArrayBufferSer(ArrayBufferSer.STRING, headersNames);
|
|
|
|
var data = [header];
|
2015-05-13 19:00:01 +08:00
|
|
|
for(i = 0; i < headersNames.length; ++i) {
|
2013-05-17 17:44:50 +08:00
|
|
|
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;
|