forked from OpenTreeMap/otm-tiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
displayFiltersToWhere.js
64 lines (55 loc) · 2.26 KB
/
displayFiltersToWhere.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
var _ = require('underscore'),
format = require('util').format,
utils = require('./filterObjectUtils'),
config = require('./config');
module.exports = function(displayFilters, restrictFeatureFilters, displayPlotsOnly) {
var featureTypes, inClause,
plotFilters = ['Tree', 'Plot', 'EmptyPlot'],
defaultPlotFilter = ['Plot'];
if ( ! _.isBoolean(displayPlotsOnly)) {
throw new Error('`displayPlotsOnly must be a boolean value.');
}
if (displayPlotsOnly) {
if (_.isArray(displayFilters) && displayFilters.length > 0) {
displayFilters = _.intersection(displayFilters, plotFilters);
} else {
displayFilters = defaultPlotFilter;
}
}
if (_.isArray(displayFilters)) {
displayFilters = _.intersection(
displayFilters,
_.union(restrictFeatureFilters || [], plotFilters));
} else {
displayFilters = _.union(restrictFeatureFilters || [], defaultPlotFilter);
}
if (_.isEmpty(displayFilters)) {
// With empty display filters, nothing should ever be shown
// 'WHERE FALSE' should override any other filters
return 'FALSE';
}
if ( ! utils.isTreeInDisplayFilters(displayFilters)) {
featureTypes = _.map(displayFilters, utils.convertValueToEscapedSqlLiteral);
inClause = featureTypes.join(', ');
return format('"%s"."feature_type" IN ( %s )', config.modelMapping.mapFeature, inClause);
}
var clauses = _.map(displayFilters, function(filter) {
var modelSql, treeSql, modelNameString;
if (_.contains(config.treeDisplayFilters, filter)) {
if (filter === 'Tree') {
treeSql = format('"%s"."id" IS NOT NULL', config.modelMapping.tree);
} else {
treeSql = format('"%s"."id" IS NULL', config.modelMapping.tree);
}
filter = 'Plot';
}
modelNameString = utils.convertValueToEscapedSqlLiteral(filter);
modelSql = format('"%s"."feature_type" = %s', config.modelMapping.mapFeature, modelNameString);
if (treeSql) {
return format("(%s) AND (%s)", treeSql, modelSql);
}
return modelSql;
});
return '(' + clauses.join(") OR (") + ')';
};