From fee36b2b3530eccd3821d41b0a51e0b3046b2544 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Wed, 4 Jul 2018 12:22:45 +0200 Subject: [PATCH] include where constraints in query generation --- .../datasource/postgres/postgres_query.ts | 60 +++++++++---------- .../plugins/datasource/postgres/query_ctrl.ts | 28 +++------ 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index cdb51c00561..4e021d37591 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -1,11 +1,12 @@ import _ from 'lodash'; -import queryPart from './query_part'; +import sqlPart from './sql_part'; export default class PostgresQuery { target: any; selectModels: any[]; queryBuilder: any; - groupByParts: any; + groupByParts: any[]; + whereParts: any[]; templateSrv: any; scopedVars: any; @@ -31,18 +32,19 @@ export default class PostgresQuery { } quoteIdentifier(value) { - return '"' + value.replace('"','""') + '"'; + return '"' + value.replace('"', '""') + '"'; } quoteLiteral(value) { - return "'" + value.replace("'","''") + "'"; + return "'" + value.replace("'", "''") + "'"; } updateProjection() { this.selectModels = _.map(this.target.select, function(parts: any) { - return _.map(parts, queryPart.create); + return _.map(parts, sqlPart.create); }); - this.groupByParts = _.map(this.target.groupBy, queryPart.create); + this.whereParts = _.map(this.target.where, sqlPart.create); + this.groupByParts = _.map(this.target.groupBy, sqlPart.create); } updatePersistedParts() { @@ -51,6 +53,9 @@ export default class PostgresQuery { return { type: part.def.type, params: part.params }; }); }); + this.target.where = _.map(this.whereParts, function(part: any) { + return { type: part.def.type, params: part.params }; + }); } hasGroupByTime() { @@ -60,8 +65,8 @@ export default class PostgresQuery { addGroupBy(value) { var stringParts = value.match(/^(\w+)(\((.*)\))?$/); var typePart = stringParts[1]; - var args = stringParts[3].split(","); - var partModel = queryPart.create({ type: typePart, params: args }); + var args = stringParts[3].split(','); + var partModel = sqlPart.create({ type: typePart, params: args }); var partCount = this.target.groupBy.length; if (partCount === 0) { @@ -86,7 +91,7 @@ export default class PostgresQuery { // remove aggregations this.target.select = _.map(this.target.select, (s: any) => { return _.filter(s, (part: any) => { - if (part.type === "aggregate") { + if (part.type === 'aggregate') { return false; } return true; @@ -118,25 +123,15 @@ export default class PostgresQuery { this.updatePersistedParts(); } - addSelectPart(selectParts, type) { - var partModel = queryPart.create({ type: type }); - partModel.def.addStrategy(selectParts, partModel, this); - this.updatePersistedParts(); + removeWherePart(whereParts, part) { + var partIndex = _.indexOf(whereParts, part); + whereParts.splice(partIndex, 1); } - private renderWhereConstraint(constraint, index, interpolate) { - var str = ''; - var operator = constraint.operator; - var value = constraint.value; - if (index > 0) { - str = (constraint.condition || 'AND') + ' '; - } - - if (interpolate) { - value = this.templateSrv.replace(value, this.scopedVars); - } - - return str + constraint.key + ' ' + operator + ' ' + value; + addSelectPart(selectParts, type) { + var partModel = sqlPart.create({ type: type }); + partModel.def.addStrategy(selectParts, partModel, this); + this.updatePersistedParts(); } interpolateQueryStr(value, variable, defaultFormatFn) { @@ -170,8 +165,8 @@ export default class PostgresQuery { if (timeGroup) { var args; - if (timeGroup.params.length > 1 && timeGroup.params[1] !== "none") { - args = timeGroup.params.join(","); + if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') { + args = timeGroup.params.join(','); } else { args = timeGroup.params[0]; } @@ -181,7 +176,7 @@ export default class PostgresQuery { } if (this.target.metricColumn !== 'None') { - query += "," + this.quoteIdentifier(this.target.metricColumn) + " AS metric"; + query += ',' + this.quoteIdentifier(this.target.metricColumn) + ' AS metric'; } var i, y; @@ -198,7 +193,7 @@ export default class PostgresQuery { query += ' FROM ' + this.quoteIdentifier(target.schema) + '.' + this.quoteIdentifier(target.table) + ' WHERE '; var conditions = _.map(target.where, (tag, index) => { - return this.renderWhereConstraint(tag, index, false); + return tag.params.join(' '); }); if (conditions.length > 0) { @@ -222,8 +217,8 @@ export default class PostgresQuery { if (groupBySection.length) { query += ' GROUP BY ' + groupBySection; - if (this.target.metricColumn !== "None") { - query += ",2"; + if (this.target.metricColumn !== 'None') { + query += ',2'; } } @@ -235,5 +230,4 @@ export default class PostgresQuery { } return query; } - } diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 1eb8b3ef80a..ecec978ae58 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -2,7 +2,7 @@ import _ from 'lodash'; import { PostgresQueryBuilder } from './query_builder'; import { QueryCtrl } from 'app/plugins/sdk'; import PostgresQuery from './postgres_query'; -import sqlPart from './query_part'; +import sqlPart from './sql_part'; export interface QueryMeta { sql: string; @@ -29,7 +29,6 @@ export class PostgresQueryCtrl extends QueryCtrl { showHelp: boolean; schemaSegment: any; tableSegment: any; - whereSegments: any; whereAdd: any; timeColumnSegment: any; metricColumnSegment: any; @@ -245,18 +244,8 @@ export class PostgresQueryCtrl extends QueryCtrl { } buildWhereSegments() { - this.whereSegments = []; - this.whereSegments.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); - // for (let constraint of this.target.where) { - // - // this.whereSegments.push(sqlPart.create({type: 'column',params: ['1']})); - // if (constraint.condition) { - // this.whereSegments.push(this.uiSegmentSrv.newCondition(constraint.condition)); - // } - // this.whereSegments.push(this.uiSegmentSrv.newKey(constraint.key)); - // this.whereSegments.push(this.uiSegmentSrv.newOperator(constraint.operator)); - // this.whereSegments.push(this.uiSegmentSrv.newKeyValue(constraint.value)); - // } + // this.whereSegments = []; + // this.whereSegments.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); } handleWherePartEvent(whereParts, part, evt, index) { @@ -276,7 +265,7 @@ export class PostgresQueryCtrl extends QueryCtrl { case 'op': return this.$q.when(this.uiSegmentSrv.newOperators(['=', '!=', '<', '<=', '>', '>=', 'IN'])); default: - return Promise.resolve([]); + return this.$q.when([]); } } case 'part-param-changed': { @@ -284,7 +273,7 @@ export class PostgresQueryCtrl extends QueryCtrl { break; } case 'action': { - whereParts.splice(whereParts.indexOf(part), 1); + this.queryModel.removeWherePart(part, index); this.panelCtrl.refresh(); break; } @@ -299,21 +288,20 @@ export class PostgresQueryCtrl extends QueryCtrl { 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' })); - return Promise.resolve(options); + return this.$q.when(options); } whereAddAction(part, index) { switch (this.whereAdd.type) { case 'macro': { - this.whereSegments.push( + this.queryModel.whereParts.push( sqlPart.create({ type: 'function', name: this.whereAdd.value, params: ['value', '=', 'value'] }) ); } default: { - this.whereSegments.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); + this.queryModel.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); } } - this.whereAdd = this.uiSegmentSrv.newPlusButton(); this.panelCtrl.refresh(); }