2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
var Pg = require('./../pg');
|
|
|
|
var ArrayBufferSer = require('../../bin-encoder');
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function BinaryFormat () {}
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
BinaryFormat.prototype = new Pg('arraybuffer');
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BinaryFormat.prototype._contentType = 'application/octet-stream';
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
BinaryFormat.prototype._extractTypeFromName = function (name) {
|
|
|
|
var g = name.match(/.*__(uintclamp|uint|int|float)(8|16|32)/i);
|
|
|
|
if (g && g.length === 3) {
|
|
|
|
var typeName = g[1] + g[2];
|
|
|
|
return ArrayBufferSer.typeNames[typeName];
|
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
};
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
// jshint maxcomplexity:12
|
2019-12-24 01:19:08 +08:00
|
|
|
BinaryFormat.prototype.transform = function (result, options, callback) {
|
|
|
|
var total_rows = result.rowCount;
|
|
|
|
var rows = result.rows;
|
|
|
|
|
|
|
|
// get headers
|
|
|
|
if (!total_rows) {
|
|
|
|
callback(null, new Buffer(0));
|
|
|
|
return;
|
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
var headersNames = Object.keys(rows[0]);
|
|
|
|
var headerTypes = [];
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if (_.contains(headersNames, 'the_geom')) {
|
|
|
|
callback(new Error('geometry types are not supported'), null);
|
|
|
|
return;
|
2013-05-17 17:44:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
try {
|
|
|
|
var i;
|
|
|
|
var r;
|
|
|
|
var n;
|
|
|
|
var t;
|
|
|
|
// get header types (and guess from name)
|
|
|
|
for (i = 0; i < headersNames.length; ++i) {
|
|
|
|
r = rows[0];
|
|
|
|
n = headersNames[i];
|
|
|
|
if (typeof (r[n]) === 'string') {
|
|
|
|
headerTypes.push(ArrayBufferSer.STRING);
|
|
|
|
} else if (typeof (r[n]) === 'object') {
|
|
|
|
t = this._extractTypeFromName(n);
|
|
|
|
t = t || ArrayBufferSer.FLOAT32;
|
|
|
|
headerTypes.push(ArrayBufferSer.BUFFER + t);
|
|
|
|
} else {
|
|
|
|
t = this._extractTypeFromName(n);
|
|
|
|
headerTypes.push(t || ArrayBufferSer.FLOAT32);
|
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// pack the data
|
|
|
|
var header = new ArrayBufferSer(ArrayBufferSer.STRING, headersNames);
|
|
|
|
var data = [header];
|
|
|
|
for (i = 0; i < headersNames.length; ++i) {
|
|
|
|
var d = [];
|
|
|
|
n = headersNames[i];
|
|
|
|
for (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);
|
|
|
|
}
|
|
|
|
var b = new ArrayBufferSer(headerTypes[i], d);
|
|
|
|
data.push(b);
|
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// create the final buffer
|
|
|
|
var all = new ArrayBufferSer(ArrayBufferSer.BUFFER, data);
|
2013-05-17 17:44:50 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
callback(null, all.buffer);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e, null);
|
|
|
|
}
|
2013-05-17 17:44:50 +08:00
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
module.exports = BinaryFormat;
|