Skip to content

Commit

Permalink
Table bug fix and wrap sql only when needed
Browse files Browse the repository at this point in the history
Conditionally wrap sql into subqueries if its not already. Drop default
sql table support
  • Loading branch information
BHare1985 committed Jun 13, 2016
1 parent 6dcd84e commit c75b445
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ module.exports = function(opts){
function preTileRender(err) {
if (err) throw err;

if(!req.params.sql) throw new Error("SQL is required after parameter parsing");
if(!req.params.style) delete req.params.style
if(!req.params.sql) delete req.params.sql

_.defaults(req.params, app.getDefaults(req.params));

Expand Down Expand Up @@ -169,14 +169,23 @@ module.exports = function(opts){

layers.forEach(function(layer, i){
params.ids.push(layer.id);
params.sql.push("("+layer.sql+") as " + layer.id + "_subquery");
params.sql.push(app.wrapSql(layer.sql, layer.id));
params.style.push(layer.style);
params.interactivity.push(layer.interactivity);
if(typeof params.layer === 'undefined' && layer.interactivity) { // The first valid interactivity is the layer
params.layer = i;
}
});
}

app.wrapSql = function(sql, layerId) {
var preWrapped = /\(.*?\)\s+(as )?\w+$/.test(sql)
if(!preWrapped) {
sql = "("+sql+") as " + layerId + "_subquery";
}

return sql;
}

app.getDefaults = function(params) {
var params = params || {};
Expand All @@ -191,15 +200,11 @@ module.exports = function(opts){
'#<%= id %>[mapnik-geometry-type=1] ' + def_style_point +
'#<%= id %>[mapnik-geometry-type=2] ' + def_style_line +
'#<%= id %>[mapnik-geometry-type=3] ' + def_style_poly);

// If table parameter exists, use it as default sql
var default_sql = _.template("SELECT * FROM <%= table %>");

return {
dbtype: options[params.mapKey].dbtype,
id: params.id,
style: default_style(params),
sql: default_sql(params),
format: 'png',
geom_type: 'polygon',
interactivity: null
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "windwalker",
"version": "0.1.1",
"version": "0.1.2",
"main": "./lib/index.js",
"description": "A Node.js based map tile server using Mapnik",
"url": "https://github.com/BHare1985/Windwalker",
Expand Down
3 changes: 3 additions & 0 deletions test/support/server_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ module.exports = function(opts) {
// no default interactivity. to enable specify the database column you'd like to interact with
req.params.interactivity = null;

req.params.sql = "SELECT * FROM "+req.params.table;

// this is in case you want to test sql parameters eg ...png?sql=select * from my_table limit 10
req.params = _.extend({}, req.params);
_.extend(req.params, req.query);


// send the finished req object on
callback(null,req);
Expand Down

0 comments on commit c75b445

Please sign in to comment.