Move categories CTE template out of aggregation class

This commit is contained in:
Daniel García Aubert
2017-09-14 11:27:03 +02:00
parent 2f4f719f55
commit a434015d5b
+55 -56
View File
@@ -130,6 +130,41 @@ const aggregationQueryTpl = ctx => `
ORDER BY value DESC
`;
const categoriesCTESqlTpl = ctx => `
WITH
${filteredQueryTpl({
_isFloatColumn: ctx.isFloatColumn,
_query: ctx.query,
_column: ctx.column,
_aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null
})},
${summaryQueryTpl({
_isFloatColumn: ctx.isFloatColumn,
_query: ctx.query,
_column: ctx.column,
_aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null
})},
${rankedCategoriesQueryTpl({
_query: ctx.query,
_column: ctx.column,
_aggregation: aggregationFnQueryTpl({
aggregation: ctx.aggregation,
aggregationColumn: ctx.aggregationColumn || 1
}),
_aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null
})},
${categoriesSummaryMinMaxQueryTpl({
_query: ctx.query,
_column: ctx.column
})},
${categoriesSummaryCountQueryTpl({
_query: ctx.query,
_column: ctx.column
})}
`;
const aggregationFnQueryTpl = ctx => `${ctx.aggregation}(${ctx.aggregationColumn})`;
const CATEGORIES_LIMIT = 6;
const VALID_OPERATIONS = {
@@ -209,32 +244,37 @@ Aggregation.prototype.sql = function(psql, override, callback) {
var aggregationSql;
if (!!override.ownFilter) {
aggregationSql = [
this.getCategoriesCTESql(
_query,
this.column,
this.aggregation,
this.aggregationColumn,
this._isFloatColumn
),
categoriesCTESqlTpl({
query: _query,
column: this.column,
aggregation: this.aggregation,
aggregationColumn: this.aggregationColumn,
isFloatColumn: this._isFloatColumn
}),
aggregationQueryTpl({
_isFloatColumn: this._isFloatColumn,
_query: _query,
_column: this.column,
_aggregation: this.getAggregationSql(),
_aggregation: aggregationFnQueryTpl({
aggregation: this.aggregation,
aggregationColumn: this.aggregationColumn || 1
}),
_limit: CATEGORIES_LIMIT
})
].join('\n');
} else {
aggregationSql = [
this.getCategoriesCTESql(
_query,
this.column,
this.aggregation,
this.aggregationColumn,
this._isFloatColumn
),
categoriesCTESqlTpl({
query: _query,
column: this.column,
aggregation: this.aggregation,
aggregationColumn: this.aggregationColumn,
isFloatColumn: this._isFloatColumn
}),
rankedAggregationQueryTpl({
_isFloatColumn: this._isFloatColumn,
_query: _query,
@@ -250,47 +290,6 @@ Aggregation.prototype.sql = function(psql, override, callback) {
return callback(null, aggregationSql);
};
Aggregation.prototype.getCategoriesCTESql = function() {
return `
WITH
${filteredQueryTpl({
_isFloatColumn: this._isFloatColumn,
_query: this.query,
_column: this.column,
_aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null
})},
${summaryQueryTpl({
_isFloatColumn: this._isFloatColumn,
_query: this.query,
_column: this.column,
_aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null
})},
${rankedCategoriesQueryTpl({
_query: this.query,
_column: this.column,
_aggregation: this.getAggregationSql(),
_aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null
})},
${categoriesSummaryMinMaxQueryTpl({
_query: this.query,
_column: this.column
})},
${categoriesSummaryCountQueryTpl({
_query: this.query,
_column: this.column
})}
`;
};
const aggregationFnQueryTpl = ctx => `${ctx._aggregationFn}(${ctx._aggregationColumn})`;
Aggregation.prototype.getAggregationSql = function() {
return aggregationFnQueryTpl({
_aggregationFn: this.aggregation,
_aggregationColumn: this.aggregationColumn || 1
});
};
Aggregation.prototype.format = function(result) {
var categories = [];
var count = 0;