2015-05-13 19:00:01 +08:00
|
|
|
var crypto = require('crypto');
|
|
|
|
var step = require('step');
|
|
|
|
var fs = require('fs');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var assert = require('assert');
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
// Keeps track of what's waiting baking for export
|
|
|
|
var bakingExports = {};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
function OgrFormat(id) {
|
2013-05-27 17:21:56 +08:00
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
OgrFormat.prototype = {
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
id: "ogr",
|
|
|
|
|
|
|
|
is_file: true,
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
getQuery: function(/*sql, options*/) {
|
2013-05-27 17:21:56 +08:00
|
|
|
return null; // dont execute the query
|
|
|
|
},
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
transform: function(/*result, options, callback*/) {
|
|
|
|
throw "should not be called for file formats";
|
2013-05-27 17:21:56 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getContentType: function(){ return this._contentType; },
|
|
|
|
|
|
|
|
getFileExtension: function(){ return this._fileExtension; },
|
|
|
|
|
|
|
|
getKey: function(options) {
|
|
|
|
return [this.id,
|
2013-11-18 18:42:43 +08:00
|
|
|
options.dbopts.dbname,
|
|
|
|
options.dbopts.user,
|
2013-05-27 17:21:56 +08:00
|
|
|
options.gn,
|
2013-11-06 00:29:02 +08:00
|
|
|
this.generateMD5(options.filename),
|
2013-05-27 17:21:56 +08:00
|
|
|
this.generateMD5(options.sql)].concat(options.skipfields).join(':');
|
|
|
|
},
|
|
|
|
|
|
|
|
generateMD5: function (data){
|
|
|
|
var hash = crypto.createHash('md5');
|
|
|
|
hash.update(data);
|
|
|
|
return hash.digest('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Internal function usable by all OGR-driven outputs
|
2014-08-03 02:27:05 +08:00
|
|
|
OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback) {
|
2013-11-15 20:24:48 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
//var gcol = options.gn;
|
2013-11-15 20:24:48 +08:00
|
|
|
var sql = options.sql;
|
|
|
|
var skipfields = options.skipfields;
|
|
|
|
var out_layername = options.filename;
|
|
|
|
|
2013-11-18 18:42:43 +08:00
|
|
|
var dbopts = options.dbopts;
|
|
|
|
|
2015-06-16 21:53:33 +08:00
|
|
|
var ogr2ogr = global.settings.ogr2ogrCommand || 'ogr2ogr';
|
2013-11-18 18:42:43 +08:00
|
|
|
var dbhost = dbopts.host;
|
2017-01-12 02:03:11 +08:00
|
|
|
var dbport = dbopts.port;
|
|
|
|
var dbuser = dbopts.user;
|
2013-11-18 18:42:43 +08:00
|
|
|
var dbpass = dbopts.pass;
|
2017-01-12 02:03:11 +08:00
|
|
|
var dbname = dbopts.dbname;
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2017-08-09 18:50:16 +08:00
|
|
|
var timeout = options.timeout;
|
|
|
|
|
2013-10-01 23:19:44 +08:00
|
|
|
var that = this;
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var columns = [];
|
2013-10-01 23:19:44 +08:00
|
|
|
var geocol;
|
|
|
|
var pg;
|
2013-05-27 17:21:56 +08:00
|
|
|
// Drop ending semicolon (ogr doens't like it)
|
2017-01-12 02:03:11 +08:00
|
|
|
sql = sql.replace(/;\s*$/, '');
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2017-11-17 21:45:58 +08:00
|
|
|
const theGeomFirst = (fieldA, fieldB) => {
|
|
|
|
if (fieldA.name === 'the_geom') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (fieldB.name === 'the_geom') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
step (
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
function fetchColumns() {
|
2013-10-01 23:19:44 +08:00
|
|
|
var colsql = 'SELECT * FROM (' + sql + ') as _cartodbsqlapi LIMIT 0';
|
2017-08-09 00:21:10 +08:00
|
|
|
pg = new PSQL(dbopts);
|
2013-05-27 17:21:56 +08:00
|
|
|
pg.query(colsql, this);
|
|
|
|
},
|
2013-10-01 23:19:44 +08:00
|
|
|
function findSRS(err, result) {
|
2015-05-13 19:00:01 +08:00
|
|
|
assert.ifError(err);
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-10-01 23:19:44 +08:00
|
|
|
var needSRS = that._needSRS;
|
|
|
|
|
2017-11-17 21:45:58 +08:00
|
|
|
columns = result.fields
|
|
|
|
// skip columns
|
|
|
|
.filter(field => skipfields.indexOf(field.name) === -1)
|
|
|
|
// put "the_geom" first (if exists)
|
|
|
|
.sort(theGeomFirst)
|
|
|
|
// get first geometry to calculate SRID ("the_geom" if exists)
|
|
|
|
.map(field => {
|
|
|
|
if (needSRS && !geocol && pg.typeName(field.dataTypeID) === 'geometry') {
|
|
|
|
geocol = field.name;
|
2013-10-01 23:19:44 +08:00
|
|
|
}
|
2017-11-17 21:45:58 +08:00
|
|
|
|
|
|
|
return field;
|
|
|
|
})
|
|
|
|
// apply quotes to columns
|
|
|
|
.map(field => out_format === 'CSV' ? pg.quoteIdentifier(field.name)+'::text' : pg.quoteIdentifier(field.name));
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( ! needSRS || ! geocol ) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-01 23:19:44 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var next = this;
|
|
|
|
|
2013-10-02 18:12:09 +08:00
|
|
|
var qgeocol = pg.quoteIdentifier(geocol);
|
|
|
|
var sridsql = 'SELECT ST_Srid(' + qgeocol + ') as srid, GeometryType(' +
|
|
|
|
qgeocol + ') as type FROM (' + sql + ') as _cartodbsqlapi WHERE ' +
|
|
|
|
qgeocol + ' is not null limit 1';
|
2013-10-01 23:19:44 +08:00
|
|
|
|
|
|
|
pg.query(sridsql, function(err, result) {
|
|
|
|
if ( err ) { next(err); return; }
|
|
|
|
if ( result.rows.length ) {
|
|
|
|
var srid = result.rows[0].srid;
|
2013-10-02 18:12:09 +08:00
|
|
|
var type = result.rows[0].type;
|
|
|
|
next(null, srid, type);
|
2016-05-12 23:21:07 +08:00
|
|
|
} else {
|
|
|
|
// continue as srid and geom type are not critical when there are no results
|
|
|
|
next(null);
|
2013-10-01 23:19:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2013-10-02 18:12:09 +08:00
|
|
|
function spawnDumper(err, srid, type) {
|
2015-05-13 19:00:01 +08:00
|
|
|
assert.ifError(err);
|
2013-10-01 23:19:44 +08:00
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
var ogrsql = 'SELECT ' + columns.join(',') + ' FROM (' + sql + ') as _cartodbsqlapi';
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2013-10-01 23:19:44 +08:00
|
|
|
var ogrargs = [
|
2013-05-27 17:21:56 +08:00
|
|
|
'-f', out_format,
|
|
|
|
'-lco', 'ENCODING=UTF-8',
|
|
|
|
'-lco', 'LINEFORMAT=CRLF',
|
|
|
|
out_filename,
|
2015-05-13 19:00:01 +08:00
|
|
|
"PG:host=" + dbhost + " port=" + dbport + " user=" + dbuser + " dbname=" + dbname + " password=" + dbpass,
|
2013-10-01 23:19:44 +08:00
|
|
|
'-sql', ogrsql
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( srid ) {
|
|
|
|
ogrargs.push('-a_srs', 'EPSG:'+srid);
|
|
|
|
}
|
|
|
|
|
2013-10-02 18:12:09 +08:00
|
|
|
if ( type ) {
|
|
|
|
ogrargs.push('-nlt', type);
|
|
|
|
}
|
|
|
|
|
2015-06-03 18:09:48 +08:00
|
|
|
if (options.cmd_params){
|
2017-01-12 02:03:11 +08:00
|
|
|
ogrargs = ogrargs.concat(options.cmd_params);
|
2015-06-03 18:09:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:29:02 +08:00
|
|
|
ogrargs.push('-nln', out_layername);
|
|
|
|
|
2017-08-10 17:22:08 +08:00
|
|
|
// TODO: research if `exec` could fit better than `spawn`
|
2013-10-01 23:19:44 +08:00
|
|
|
var child = spawn(ogr2ogr, ogrargs);
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2017-08-10 17:22:08 +08:00
|
|
|
var timedOut = false;
|
2017-08-09 18:50:16 +08:00
|
|
|
var ogrTimeout;
|
|
|
|
if (timeout > 0) {
|
|
|
|
ogrTimeout = setTimeout(function () {
|
2017-08-10 17:22:08 +08:00
|
|
|
timedOut = true;
|
2017-08-09 18:50:16 +08:00
|
|
|
child.kill();
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:43:25 +08:00
|
|
|
child.on('error', function (err) {
|
2017-08-09 18:50:16 +08:00
|
|
|
clearTimeout(ogrTimeout);
|
2017-04-11 15:43:25 +08:00
|
|
|
next(err);
|
|
|
|
});
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2017-04-18 17:40:37 +08:00
|
|
|
var stderrData = [];
|
|
|
|
child.stderr.setEncoding('utf8');
|
|
|
|
child.stderr.on('data', function (data) {
|
|
|
|
stderrData.push(data);
|
2013-05-27 17:21:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
child.on('exit', function(code) {
|
2017-08-09 18:50:16 +08:00
|
|
|
clearTimeout(ogrTimeout);
|
|
|
|
|
2017-08-10 17:22:08 +08:00
|
|
|
if (timedOut) {
|
2017-08-09 18:50:16 +08:00
|
|
|
return next(new Error('You are over platform\'s limits. Please contact us to know more details'));
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:40:37 +08:00
|
|
|
if (code !== 0) {
|
|
|
|
var errMessage = 'ogr2ogr command return code ' + code;
|
|
|
|
if (stderrData.length > 0) {
|
|
|
|
errMessage += ', Error: ' + stderrData.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(new Error(errMessage));
|
2013-05-27 17:21:56 +08:00
|
|
|
}
|
2017-04-18 17:40:37 +08:00
|
|
|
|
|
|
|
return next();
|
2013-05-27 17:21:56 +08:00
|
|
|
});
|
2017-04-18 17:40:37 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
callback(err, out_filename);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
OgrFormat.prototype.toOGR_SingleFile = function(options, fmt, callback) {
|
2013-11-15 20:24:48 +08:00
|
|
|
|
2013-11-18 18:42:43 +08:00
|
|
|
var dbname = options.dbopts.dbname;
|
|
|
|
var user_id = options.dbopts.user;
|
2013-11-15 20:24:48 +08:00
|
|
|
var gcol = options.gcol;
|
|
|
|
var sql = options.sql;
|
|
|
|
var skipfields = options.skipfields;
|
|
|
|
var ext = this._fileExtension;
|
|
|
|
var layername = options.filename;
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var tmpdir = global.settings.tmpDir || '/tmp';
|
2015-05-13 19:00:01 +08:00
|
|
|
var reqKey = [
|
|
|
|
fmt,
|
|
|
|
dbname,
|
|
|
|
user_id,
|
|
|
|
gcol,
|
|
|
|
this.generateMD5(layername),
|
|
|
|
this.generateMD5(sql)
|
|
|
|
].concat(skipfields).join(':');
|
2013-05-29 22:48:20 +08:00
|
|
|
var outdirpath = tmpdir + '/sqlapi-' + process.pid + '-' + reqKey;
|
2013-05-27 17:21:56 +08:00
|
|
|
var dumpfile = outdirpath + ':cartodb-query.' + ext;
|
|
|
|
|
|
|
|
// TODO: following tests:
|
|
|
|
// - fetch query with no "the_geom" column
|
2013-11-15 20:24:48 +08:00
|
|
|
this.toOGR(options, fmt, dumpfile, callback);
|
2013-05-27 17:21:56 +08:00
|
|
|
};
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
OgrFormat.prototype.sendResponse = function(opts, callback) {
|
2015-05-13 19:00:01 +08:00
|
|
|
//var next = callback;
|
2013-05-27 17:21:56 +08:00
|
|
|
var reqKey = this.getKey(opts);
|
2014-03-20 01:34:21 +08:00
|
|
|
var qElem = new ExportRequest(opts.sink, callback, opts.beforeSink);
|
2013-05-27 17:21:56 +08:00
|
|
|
var baking = bakingExports[reqKey];
|
|
|
|
if ( baking ) {
|
|
|
|
baking.req.push( qElem );
|
|
|
|
} else {
|
|
|
|
baking = bakingExports[reqKey] = { req: [ qElem ] };
|
|
|
|
this.generate(opts, function(err, dumpfile) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( opts.profiler ) {
|
|
|
|
opts.profiler.done('generate');
|
|
|
|
}
|
|
|
|
step (
|
2013-05-29 23:33:10 +08:00
|
|
|
function sendResults() {
|
2013-05-27 17:21:56 +08:00
|
|
|
var nextPipe = function(finish) {
|
|
|
|
var r = baking.req.shift();
|
|
|
|
if ( ! r ) { finish(null); return; }
|
|
|
|
r.sendFile(err, dumpfile, function() {
|
|
|
|
nextPipe(finish);
|
|
|
|
});
|
2014-08-03 02:23:47 +08:00
|
|
|
};
|
2013-05-27 17:21:56 +08:00
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( ! err ) {
|
|
|
|
nextPipe(this);
|
|
|
|
} else {
|
2013-05-27 17:21:56 +08:00
|
|
|
_.each(baking.req, function(r) {
|
|
|
|
r.cb(err);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2015-05-13 19:00:01 +08:00
|
|
|
function cleanup(/*err*/) {
|
2013-05-27 17:21:56 +08:00
|
|
|
delete bakingExports[reqKey];
|
|
|
|
|
|
|
|
// unlink dump file (sync to avoid race condition)
|
|
|
|
console.log("removing", dumpfile);
|
|
|
|
try { fs.unlinkSync(dumpfile); }
|
|
|
|
catch (e) {
|
2015-05-13 19:00:01 +08:00
|
|
|
if ( e.code !== 'ENOENT' ) {
|
2013-05-27 17:21:56 +08:00
|
|
|
console.log("Could not unlink dumpfile " + dumpfile + ": " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-05-13 19:00:01 +08:00
|
|
|
});
|
2013-05-27 17:21:56 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: put in an ExportRequest.js ----- {
|
|
|
|
|
2014-03-20 01:34:21 +08:00
|
|
|
function ExportRequest(ostream, callback, beforeSink) {
|
2013-05-27 17:21:56 +08:00
|
|
|
this.cb = callback;
|
2014-03-20 01:34:21 +08:00
|
|
|
this.beforeSink = beforeSink;
|
2013-05-27 17:21:56 +08:00
|
|
|
this.ostream = ostream;
|
|
|
|
this.istream = null;
|
|
|
|
this.canceled = false;
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.ostream.on('close', function() {
|
|
|
|
//console.log("Request close event, qElem.stream is " + qElem.stream);
|
|
|
|
that.canceled = true;
|
|
|
|
if ( that.istream ) {
|
|
|
|
that.istream.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportRequest.prototype.sendFile = function (err, filename, callback) {
|
|
|
|
var that = this;
|
|
|
|
if ( ! this.canceled ) {
|
|
|
|
//console.log("Creating readable stream out of dumpfile");
|
|
|
|
this.istream = fs.createReadStream(filename)
|
2015-05-13 19:00:01 +08:00
|
|
|
.on('open', function(/*fd*/) {
|
|
|
|
if ( that.beforeSink ) {
|
|
|
|
that.beforeSink();
|
|
|
|
}
|
2013-05-27 17:21:56 +08:00
|
|
|
that.istream.pipe(that.ostream);
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.on('error', function(e) {
|
|
|
|
console.log("Can't send response: " + e);
|
2017-01-12 02:03:11 +08:00
|
|
|
that.ostream.end();
|
2013-05-27 17:21:56 +08:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//console.log("Response was canceled, not streaming the file");
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
this.cb();
|
2014-08-03 02:23:47 +08:00
|
|
|
};
|
2013-05-27 17:21:56 +08:00
|
|
|
|
|
|
|
//------ }
|
|
|
|
|
2014-08-03 02:27:05 +08:00
|
|
|
module.exports = OgrFormat;
|