From 2e79781711d14627a1dba97fc87bfb7c3f5976ca Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 19 May 2016 13:32:32 +0200 Subject: [PATCH] Adds support for sql wrap in all layers Previously it was only working for analyses ones. --- NEWS.md | 7 +++ lib/cartodb/controllers/map.js | 6 ++ .../adapter/sql-wrap-mapconfig-adapter.js | 25 +++++++++ .../models/mapconfig/named_map_provider.js | 6 ++ npm-shrinkwrap.json | 2 +- package.json | 2 +- test/acceptance/sql-wrap.js | 52 ++++++++++++++++++ test/fixtures/sql-wrap-usa-filter.png | Bin 0 -> 4197 bytes 8 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 lib/cartodb/models/mapconfig/adapter/sql-wrap-mapconfig-adapter.js create mode 100644 test/acceptance/sql-wrap.js create mode 100644 test/fixtures/sql-wrap-usa-filter.png diff --git a/NEWS.md b/NEWS.md index 7e495c7d..9eeb06e3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,12 @@ # Changelog +## 2.44.0 + +Released 2016-mm-dd + +Adds support for sql wrap in all layers + + ## 2.43.1 Released 2016-05-19 diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index feaf6fa5..1f8f25bb 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -19,6 +19,7 @@ var MapConfigNamedLayersAdapter = require('../models/mapconfig_named_layers_adap var AnalysisMapConfigAdapter = require('../models/analysis-mapconfig-adapter'); var NamedMapMapConfigProvider = require('../models/mapconfig/named_map_provider'); var CreateLayergroupMapConfigProvider = require('../models/mapconfig/create_layergroup_provider'); +var SqlWrapMapConfigAdapter = require('../models/mapconfig/adapter/sql-wrap-mapconfig-adapter'); /** * @param {AuthApi} authApi @@ -52,6 +53,7 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata this.analysisMapConfigAdapter = new AnalysisMapConfigAdapter(analysisBackend); this.namedLayersAdapter = new MapConfigNamedLayersAdapter(templateMaps); this.overviewsAdapter = overviewsAdapter; + this.sqlWrapMapConfigAdapter = new SqlWrapMapConfigAdapter(); } util.inherits(MapController, BaseController); @@ -139,6 +141,10 @@ MapController.prototype.create = function(req, res, prepareConfigFn) { self.req2params(req, this); }, prepareConfigFn, + function prepareSqlWrap(err, requestMapConfig) { + assert.ifError(err); + self.sqlWrapMapConfigAdapter.getMapConfig(requestMapConfig, this); + }, function prepareAnalysisLayers(err, requestMapConfig) { assert.ifError(err); var analysisConfiguration = { diff --git a/lib/cartodb/models/mapconfig/adapter/sql-wrap-mapconfig-adapter.js b/lib/cartodb/models/mapconfig/adapter/sql-wrap-mapconfig-adapter.js new file mode 100644 index 00000000..6b9a5130 --- /dev/null +++ b/lib/cartodb/models/mapconfig/adapter/sql-wrap-mapconfig-adapter.js @@ -0,0 +1,25 @@ +function SqlWrapMapConfigAdapter() { +} + +module.exports = SqlWrapMapConfigAdapter; + + +SqlWrapMapConfigAdapter.prototype.getMapConfig = function(requestMapConfig, callback) { + if (requestMapConfig && Array.isArray(requestMapConfig.layers)) { + requestMapConfig.layers = requestMapConfig.layers.map(function(layer) { + if (layer.options) { + var sqlQueryWrap = layer.options.sql_wrap; + if (sqlQueryWrap) { + var layerSql = layer.options.sql; + if (layerSql) { + layer.options.sql_raw = layerSql; + layer.options.sql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, layerSql); + } + } + } + return layer; + }); + } + + return callback(null, requestMapConfig); +}; diff --git a/lib/cartodb/models/mapconfig/named_map_provider.js b/lib/cartodb/models/mapconfig/named_map_provider.js index e285b2b7..e1ad6fea 100644 --- a/lib/cartodb/models/mapconfig/named_map_provider.js +++ b/lib/cartodb/models/mapconfig/named_map_provider.js @@ -6,6 +6,7 @@ var step = require('step'); var MapConfig = require('windshaft').model.MapConfig; var templateName = require('../../backends/template_maps').templateName; var QueryTables = require('cartodb-query-tables'); +var SqlWrapMapConfigAdapter = require('./adapter/sql-wrap-mapconfig-adapter'); /** * @constructor @@ -22,6 +23,7 @@ function NamedMapMapConfigProvider(templateMaps, pgConnection, metadataBackend, this.turboCartoAdapter = turboCartoAdapter; this.analysisMapConfigAdapter = analysisMapConfigAdapter; this.overviewsAdapter = overviewsAdapter; + this.sqlWrapMapConfigAdapter = new SqlWrapMapConfigAdapter(); this.owner = owner; this.templateName = templateName(templateId); @@ -95,6 +97,10 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) { assert.ifError(err); return self.templateMaps.instance(self.template, templateParams); }, + function prepareSqlWrap(err, requestMapConfig) { + assert.ifError(err); + self.sqlWrapMapConfigAdapter.getMapConfig(requestMapConfig, this); + }, function prepareAnalysisLayers(err, requestMapConfig) { assert.ifError(err); var analysisConfiguration = { diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index c3b9529d..04f931c5 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "windshaft-cartodb", - "version": "2.42.3", + "version": "2.44.0", "dependencies": { "body-parser": { "version": "1.14.2", diff --git a/package.json b/package.json index ecf28729..c426011b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "windshaft-cartodb", - "version": "2.43.1", + "version": "2.44.0", "description": "A map tile server for CartoDB", "keywords": [ "cartodb" diff --git a/test/acceptance/sql-wrap.js b/test/acceptance/sql-wrap.js new file mode 100644 index 00000000..7b3c25d8 --- /dev/null +++ b/test/acceptance/sql-wrap.js @@ -0,0 +1,52 @@ +require('../support/test_helper'); + +var assert = require('../support/assert'); +var TestClient = require('../support/test-client'); + +describe('sql-wrap', function() { + + afterEach(function(done) { + if (this.testClient) { + this.testClient.drain(done); + } else { + return done(); + } + }); + + it('should use sql_wrap from layer options', function(done) { + var mapConfig = { + version: '1.5.0', + layers: [ + { + "type": "cartodb", + "options": { + "sql": "SELECT * FROM populated_places_simple_reduced", + "sql_wrap": "SELECT * FROM (<%= sql %>) _w WHERE adm0_a3 = 'USA'", + "cartocss": [ + "#points {", + " marker-fill-opacity: 1;", + " marker-line-color: #FFF;", + " marker-line-width: 0.5;", + " marker-line-opacity: 1;", + " marker-placement: point;", + " marker-type: ellipse;", + " marker-width: 8;", + " marker-fill: red;", + " marker-allow-overlap: true;", + "}" + ].join('\n'), + "cartocss_version": "2.3.0" + } + } + ] + }; + + this.testClient = new TestClient(mapConfig, 1234); + this.testClient.getTile(0, 0, 0, function(err, tile, img) { + assert.ok(!err, err); + var fixtureImg = './test/fixtures/sql-wrap-usa-filter.png'; + assert.imageIsSimilarToFile(img, fixtureImg, 20, done); + }); + }); + +}); diff --git a/test/fixtures/sql-wrap-usa-filter.png b/test/fixtures/sql-wrap-usa-filter.png new file mode 100644 index 0000000000000000000000000000000000000000..30bd4eb341b814a73a88b99177b2f0461d811189 GIT binary patch literal 4197 zcmb_g`9Bkm8{fv9b0l}mEkuZk-1nKI)Q2KRE6gp_oD-i)j$FCIC?=7soaM}!Ym6K@ zGg>2aHDfdT`X9cZ*Xwz`p693c>v?{8AI}R1I}0ujQ4RnAz-47=dIJCeo{m5O8_Q{P zeOTcI0PreVnHt?h7H%!tr`-B}zH2wob+$M41;TaeDW~HVW!LfEr<~QCwlLqy7qX%n z{Ws6qLMD3oTzLZ^*M&}!g&XoYUR>|3V3JpTp=z1lea7e{QT?s7GrPm3_#3KMW0v=i z^+>Lym{R#r)+I{T!pKMgh190`ZM$&!s3;Um9}SYR%KVQ?yBd>`VNgz4qB-4Z^1m%A z8hPcs9`$+oa#mTE66Wtbbmx=Tmc4gj1|LsR%p>bG-H(a^OS+{*iX2hKYQEV$e<{)r zrnh5g9r~*w?YVd$s(mH?k#t=Yr4fNd+KqAd_ty;7mQMJbx9vn($;q=aM)GNsZ4nBi zMFqIq-i=YHm+Dx5gM&l7!YDdMz9=eiXs$ckNzBS7Cps4${m>~8`p*Ys!5fpwVly1} zRgQ}INOuP^WTWh)FJXX6eTgc3wn}`L)?Qv4Bojl_?0tFgTMM(2@xxe0JY-myMxFZPLntc!)me|6E<+3%+(b>gMr0Rchia= zFCET|7I!uh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