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'); // Keeps track of what's waiting baking for export var bakingExports = {}; function OgrFormat(id) { this.id = id; } OgrFormat.prototype = { id: "ogr", is_file: true, getQuery: function(/*sql, options*/) { return null; // dont execute the query }, transform: function(/*result, options, callback*/) { throw "should not be called for file formats"; }, getContentType: function(){ return this._contentType; }, getFileExtension: function(){ return this._fileExtension; }, getKey: function(options) { return [this.id, options.dbopts.dbname, options.dbopts.user, options.gn, this.generateMD5(options.filename), 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 OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback) { //var gcol = options.gn; var sql = options.sql; var skipfields = options.skipfields; var out_layername = options.filename; var dbopts = options.dbopts; var ogr2ogr = global.settings.ogr2ogrCommand || 'ogr2ogr'; var dbhost = dbopts.host; var dbport = dbopts.port; var dbuser = dbopts.user; var dbpass = dbopts.pass; var dbname = dbopts.dbname; var timeout = options.timeout; var that = this; var columns = []; var geocol; var pg; // Drop ending semicolon (ogr doens't like it) sql = sql.replace(/;\s*$/, ''); step ( function fetchColumns() { var colsql = 'SELECT * FROM (' + sql + ') as _cartodbsqlapi LIMIT 0'; pg = new PSQL(dbopts); pg.query(colsql, this); }, // jshint maxcomplexity:9 function findSRS(err, result) { assert.ifError(err); //if ( ! result.rows.length ) throw new Error("Query returns no rows"); var needSRS = that._needSRS; // Skip system columns, find geom column for (var i=0; i