Merge pull request #441 from CartoDB/sql-wrap

Analysis layers can have a sql_wrap option to wrap node queries
This commit is contained in:
Raul Ochoa 2016-05-09 14:09:16 +02:00
commit b99db7cb69
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 layerNode = sourceId2Node[layerSourceId];
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*sql\s*%>/g, analysisSql);
}
layer.options.sql = analysisSql;
var layerDataviews = getLayerDataviews(layer, dataviews);
layer.options.columns = layerDataviews.reduce(function(columns, dataview) {
return columns.concat(getDataviewColumns(dataview));

View File

@ -327,4 +327,44 @@ describe('analysis-layers', function() {
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 (<%= sql %>) __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