Analysis layers can have a sql_wrap option to wrap node queries

This commit is contained in:
Raul Ochoa 2016-05-06 16:37:52 +02:00
parent e4ba68850c
commit 7d4caf6974
3 changed files with 46 additions and 1 deletions

View File

@ -139,7 +139,12 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration
var layerSourceId = getLayerSourceId(layer); var layerSourceId = getLayerSourceId(layer);
var layerNode = sourceId2Node[layerSourceId]; var layerNode = sourceId2Node[layerSourceId];
if (layerNode) { if (layerNode) {
layer.options.sql = layerQuery(layerNode.getQuery(), layerNode.getColumns()); var analysisSql = layerQuery(layerNode.getQuery(), layerNode.getColumns());
var sqlQueryWrap = layer.options.sql_wrap;
if (sqlQueryWrap) {
analysisSql = sqlQueryWrap.replace(/<%=\s*__analysis_query\s*%>/g, analysisSql);
}
layer.options.sql = analysisSql;
var layerDataviews = getLayerDataviews(layer, dataviews); var layerDataviews = getLayerDataviews(layer, dataviews);
layer.options.columns = layerDataviews.reduce(function(columns, dataview) { layer.options.columns = layerDataviews.reduce(function(columns, dataview) {
return columns.concat(getDataviewColumns(dataview)); return columns.concat(getDataviewColumns(dataview));

View File

@ -327,4 +327,44 @@ describe('analysis-layers', function() {
testClient.drain(done); testClient.drain(done);
}); });
}); });
it('should wrap queries from analyses', function(done) {
var testClient = new TestClient(mapConfig(
[
{
"type": "cartodb",
"options": {
"source": {
"id": "2570e105-7b37-40d2-bdf4-1af889598745"
},
"sql_wrap": "SELECT * FROM (<%= __analysis_query %>) __wrapped WHERE adm0cap = 1",
"cartocss": DEFAULT_MULTITYPE_STYLE,
"cartocss_version": "2.3.0"
}
}
],
{},
[
{
"id": "2570e105-7b37-40d2-bdf4-1af889598745",
"type": "source",
"params": {
"query": "select * from populated_places_simple_reduced"
}
}
]
), 1234);
var tile = TILE_ANALYSIS_TABLES;
testClient.getTile(tile.z, tile.x, tile.y, function(err, res, image) {
assert.ok(!err, err);
var fixturePath = './test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png';
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) {
assert.ok(!err, err);
testClient.drain(done);
});
});
});
}); });

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB