Refactor to reduce cyclomatic complexity

This commit is contained in:
Javier Goizueta 2016-05-12 18:47:24 +02:00
parent 85788f42a6
commit b574489950

View File

@ -206,6 +206,7 @@ function apply_filters_to_query(query, filters) {
function zoom_level_for_bounding_box(query, bounding_box) {
// TODO: implement
// jshint unused:false
return '100'; // force use of base table
}
@ -227,6 +228,7 @@ function zoom_level_for_bounding_box(query, bounding_box) {
// doesnn't prevent substitution inside literals).
// But the transformation will currently only be applied to simple queries
// of the form detected by the overviews_supported_query function.
// TODO: document recent changes (options bounding_box, zoom_level, filters...)
OverviewsQueryRewriter.prototype.query = function(query, data, options) {
options = options || {};
@ -245,23 +247,9 @@ OverviewsQueryRewriter.prototype.query = function(query, data, options) {
var rewritten_query;
var zoom_level_expression = this.options.zoom_level;
var zoom_level;
if ( _.has(options, 'bounding_box') ) {
// The presence of this key, even if it has a null value indicates the
// query cannot handle dynamic zoom expressinos (is not for the tiler)
zoom_level = zoom_level_for_bounding_box(unfiltered_query, options.bounding_box);
} else if ( options.zoom_level ) {
zoom_level = options.zoom_level;
}
if ( !zoom_level && !zoom_level_expression ) {
zoom_level = '0';
}
var zoom_level = zoom_level_for_query(unfiltered_query, zoom_level_expression, options);
if ( zoom_level || zoom_level === '0' || zoom_level === 0 ) {
rewritten_query = overviews_query_with_definite_zoom(unfiltered_query, overviews, zoom_level);
} else {
rewritten_query = overviews_query_with_zoom_expression(unfiltered_query, overviews, zoom_level_expression);
}
rewritten_query = overviews_query(unfiltered_query, overviews, zoom_level, zoom_level_expression);
if ( rewritten_query === unfiltered_query ) {
// could not or didn't need to alter the query
@ -273,6 +261,29 @@ OverviewsQueryRewriter.prototype.query = function(query, data, options) {
return rewritten_query;
};
function zoom_level_for_query(query, zoom_level_expression, options) {
var zoom_level = null;
if ( _.has(options, 'bounding_box') ) {
// The presence of this key, even if it has a null value indicates the
// query cannot handle dynamic zoom expressinos (is not for the tiler)
zoom_level = zoom_level_for_bounding_box(query, options.bounding_box);
} else if ( _.has(options, 'zoom_level') ) {
zoom_level = options.zoom_level;
}
if ( zoom_level === null && !zoom_level_expression ) {
zoom_level = '0';
}
return zoom_level;
}
function overviews_query(query, overviews, zoom_level, zoom_level_expression) {
if ( zoom_level || zoom_level === '0' || zoom_level === 0 ) {
return overviews_query_with_definite_zoom(query, overviews, zoom_level);
} else {
return overviews_query_with_zoom_expression(query, overviews, zoom_level_expression);
}
}
OverviewsQueryRewriter.prototype.is_supported_query = function(sql) {
var basic_query =
/\s*SELECT\s+[\*a-z0-9_,\s]+?\s+FROM\s+((\"[^"]+\"|[a-z0-9_]+)\.)?(\"[^"]+\"|[a-z0-9_]+)\s*;?\s*/i;