From 1ac8455dc28cc8f66c1f207f5f812f371dc3aeee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 6 Sep 2017 18:13:34 +0200 Subject: [PATCH] Use template strings to build histogram query --- lib/cartodb/models/dataview/histogram.js | 110 ++++++----------------- 1 file changed, 26 insertions(+), 84 deletions(-) diff --git a/lib/cartodb/models/dataview/histogram.js b/lib/cartodb/models/dataview/histogram.js index b732f712..60d6cd36 100644 --- a/lib/cartodb/models/dataview/histogram.js +++ b/lib/cartodb/models/dataview/histogram.js @@ -366,103 +366,45 @@ module.exports = class Histogram extends BaseDataview { } _buildQuery (psql, override, callback) { - var filteredQuery, basicsQuery, binsQuery; - var _column = this.column; - var _query = this.query; if (this.isDateHistogram(override)) { return this._buildDateHistogramQuery(psql, override, callback); } - if (this._columnType === 'date') { - _column = columnCastTpl({column: _column}); - } - - filteredQuery = filteredQueryTpl({ + const histogramSql = this.buildNumericHistogramQueryTpl({ + _override: override, + _column: this._columnType === 'date' ? columnCastTpl({ column: this.column }) : this.column, _isFloatColumn: this._columnType === 'float', - _query: _query, - _column: _column + _query: this.query, + _start: this.getBinStart(override), + _end: this.getBinEnd(override), + _minBins: BIN_MIN_NUMBER, + _maxBins: BIN_MAX_NUMBER, + _bins: override.bins }); - if (this._shouldOverride(override)) { - debug('overriding with %j', override); - basicsQuery = overrideBasicsQueryTpl({ - _query: _query, - _column: _column, - _start: this.getBinStart(override), - _end: this.getBinEnd(override) - }); - - binsQuery = [ - overrideBinsQueryTpl({ - _bins: override.bins - }) - ].join(',\n'); - } else { - basicsQuery = basicsQueryTpl({ - _query: _query, - _column: _column - }); - - if (this._shouldOverrideBins(override)) { - binsQuery = [ - overrideBinsQueryTpl({ - _bins: override.bins - }) - ].join(',\n'); - } else { - binsQuery = [ - iqrQueryTpl({ - _query: _query, - _column: _column - }), - binsQueryTpl({ - _query: _query, - _minBins: BIN_MIN_NUMBER, - _maxBins: BIN_MAX_NUMBER - }) - ].join(',\n'); - } - } - - var cteSql = [ - filteredQuery, - basicsQuery, - binsQuery, - nullsQueryTpl({ - _query: _query, - _column: _column - }) - ]; - - if (this._columnType === 'float') { - cteSql.push( - infinitiesQueryTpl({ - _query: _query, - _column: _column - }), - nansQueryTpl({ - _query: _query, - _column: _column - }) - ); - } - - var histogramSql = [ - "WITH", - cteSql.join(',\n'), - histogramQueryTpl({ - _isFloatColumn: this._columnType === 'float', - _query: _query, - _column: _column - }) - ].join('\n'); - debug(histogramSql); return callback(null, histogramSql); } + buildNumericHistogramQueryTpl (ctx) { + return ` + WITH + ${filteredQueryTpl(ctx)}, + ${this._shouldOverride(ctx._override) ? overrideBasicsQueryTpl(ctx) : basicsQueryTpl(ctx)}, + ${this._shouldOverride(ctx._override) ? + overrideBinsQueryTpl(ctx) : + this._shouldOverrideBins(ctx._override) ? + overrideBinsQueryTpl(ctx) : + `${iqrQueryTpl(ctx)}, ${binsQueryTpl(ctx)}` + }, + ${nullsQueryTpl(ctx)} + ${ctx._isFloatColumn ? `,${infinitiesQueryTpl(ctx)}, ${nansQueryTpl(ctx)}` : ''} + ${histogramQueryTpl(ctx)} + `; + } + _shouldOverride (override) { return override && override.hasOwnProperty('start') &&