Adds support for sql wrap in all layers

Previously it was only working for analyses ones.
This commit is contained in:
Raul Ochoa 2016-05-19 13:32:32 +02:00
parent 289ffbbedc
commit 2e79781711
8 changed files with 98 additions and 2 deletions

View File

@ -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

View File

@ -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 = {

View File

@ -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);
};

View File

@ -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 = {

2
npm-shrinkwrap.json generated
View File

@ -1,6 +1,6 @@
{
"name": "windshaft-cartodb",
"version": "2.42.3",
"version": "2.44.0",
"dependencies": {
"body-parser": {
"version": "1.14.2",

View File

@ -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"

View File

@ -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);
});
});
});

BIN
test/fixtures/sql-wrap-usa-filter.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB