From 7600c6efcbeb0b5a103badd86806fe96aa756f34 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Fri, 6 Jul 2018 10:38:52 +0200 Subject: [PATCH] remove hardcoded $__timeFilter, make macros functional in where clause --- .../plugins/datasource/postgres/postgres_query.ts | 15 ++++++++++----- .../app/plugins/datasource/postgres/query_ctrl.ts | 11 +++++------ .../app/plugins/datasource/postgres/sql_part.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 4d90ad83e3b..36660ecdd42 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -63,7 +63,7 @@ export default class PostgresQuery { }); }); this.target.where = _.map(this.whereParts, function(part: any) { - return { type: part.def.type, params: part.params }; + return { type: part.def.type, name: part.name, params: part.params }; }); this.target.groupBy = _.map(this.groupByParts, function(part: any) { return { type: part.def.type, params: part.params }; @@ -196,15 +196,20 @@ export default class PostgresQuery { query += ' FROM ' + target.schema + '.' + target.table + ' WHERE '; var conditions = _.map(target.where, (tag, index) => { - return tag.params.join(' '); + switch (tag.type) { + case 'macro': + return tag.name + '(' + target.timeColumn + ')'; + break; + case 'expression': + return tag.params.join(' '); + break; + } }); if (conditions.length > 0) { - query += '(' + conditions.join(' AND ') + ') AND '; + query += conditions.join(' AND '); } - query += '$__timeFilter(' + target.timeColumn + ')'; - var groupBySection = ''; for (i = 0; i < this.groupByParts.length; i++) { var part = this.groupByParts[i]; diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 4aa2db90c1d..ab365561b79 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -287,18 +287,17 @@ export class PostgresQueryCtrl extends QueryCtrl { getWhereOptions() { var options = []; - options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: '$__timeFilter' })); - options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: '$__unixEpochFilter' })); - options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: 'Expression' })); + options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' })); + options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' })); + options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' })); return this.$q.when(options); } whereAddAction(part, index) { switch (this.whereAdd.type) { case 'macro': { - this.queryModel.whereParts.push( - sqlPart.create({ type: 'function', name: this.whereAdd.value, params: ['value', '=', 'value'] }) - ); + this.queryModel.whereParts.push(sqlPart.create({ type: 'macro', name: this.whereAdd.value, params: [] })); + break; } default: { this.queryModel.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); diff --git a/public/app/plugins/datasource/postgres/sql_part.ts b/public/app/plugins/datasource/postgres/sql_part.ts index bb29fde9fb4..598de56d4b1 100644 --- a/public/app/plugins/datasource/postgres/sql_part.ts +++ b/public/app/plugins/datasource/postgres/sql_part.ts @@ -115,6 +115,16 @@ register({ renderer: columnRenderer, }); +register({ + type: 'macro', + style: 'function', + label: 'Macro:', + addStrategy: addExpressionStrategy, + params: [], + defaultParams: [], + renderer: columnRenderer, +}); + register({ type: 'aggregate', style: 'label',