2018-01-30 15:13:55 -06:00
|
|
|
import _ from 'lodash';
|
2018-07-04 05:22:45 -05:00
|
|
|
import sqlPart from './sql_part';
|
2018-01-30 15:13:55 -06:00
|
|
|
|
|
|
|
export default class PostgresQuery {
|
|
|
|
target: any;
|
|
|
|
selectModels: any[];
|
|
|
|
queryBuilder: any;
|
2018-07-04 05:22:45 -05:00
|
|
|
groupByParts: any[];
|
|
|
|
whereParts: any[];
|
2018-01-30 15:13:55 -06:00
|
|
|
templateSrv: any;
|
|
|
|
scopedVars: any;
|
|
|
|
|
|
|
|
/** @ngInject */
|
|
|
|
constructor(target, templateSrv?, scopedVars?) {
|
|
|
|
this.target = target;
|
|
|
|
this.templateSrv = templateSrv;
|
|
|
|
this.scopedVars = scopedVars;
|
|
|
|
|
|
|
|
target.schema = target.schema || 'public';
|
|
|
|
target.format = target.format || 'time_series';
|
|
|
|
target.timeColumn = target.timeColumn || 'time';
|
2018-02-08 03:25:32 -06:00
|
|
|
target.metricColumn = target.metricColumn || 'None';
|
2018-01-30 15:13:55 -06:00
|
|
|
|
2018-02-08 03:25:32 -06:00
|
|
|
target.groupBy = target.groupBy || [];
|
|
|
|
target.where = target.where || [];
|
2018-03-03 13:57:00 -06:00
|
|
|
target.select = target.select || [[{ type: 'column', params: ['value'] }]];
|
2018-01-30 15:13:55 -06:00
|
|
|
|
2018-03-14 12:09:47 -05:00
|
|
|
// give interpolateQueryStr access to this
|
|
|
|
this.interpolateQueryStr = this.interpolateQueryStr.bind(this);
|
2018-03-14 17:03:32 -05:00
|
|
|
|
|
|
|
this.updateProjection();
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:25:32 -06:00
|
|
|
quoteIdentifier(value) {
|
2018-07-04 05:22:45 -05:00
|
|
|
return '"' + value.replace('"', '""') + '"';
|
2018-02-08 03:25:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
quoteLiteral(value) {
|
2018-07-04 05:22:45 -05:00
|
|
|
return "'" + value.replace("'", "''") + "'";
|
2018-01-30 15:57:38 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 15:13:55 -06:00
|
|
|
updateProjection() {
|
|
|
|
this.selectModels = _.map(this.target.select, function(parts: any) {
|
2018-07-04 05:22:45 -05:00
|
|
|
return _.map(parts, sqlPart.create);
|
2018-01-30 15:13:55 -06:00
|
|
|
});
|
2018-07-04 05:22:45 -05:00
|
|
|
this.whereParts = _.map(this.target.where, sqlPart.create);
|
|
|
|
this.groupByParts = _.map(this.target.groupBy, sqlPart.create);
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
updatePersistedParts() {
|
|
|
|
this.target.select = _.map(this.selectModels, function(selectParts) {
|
|
|
|
return _.map(selectParts, function(part: any) {
|
|
|
|
return { type: part.def.type, params: part.params };
|
|
|
|
});
|
|
|
|
});
|
2018-07-04 05:22:45 -05:00
|
|
|
this.target.where = _.map(this.whereParts, function(part: any) {
|
|
|
|
return { type: part.def.type, params: part.params };
|
|
|
|
});
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hasGroupByTime() {
|
|
|
|
return _.find(this.target.groupBy, (g: any) => g.type === 'time');
|
|
|
|
}
|
|
|
|
|
|
|
|
addGroupBy(value) {
|
2018-03-04 12:35:43 -06:00
|
|
|
var stringParts = value.match(/^(\w+)(\((.*)\))?$/);
|
2018-01-30 15:13:55 -06:00
|
|
|
var typePart = stringParts[1];
|
2018-07-04 05:22:45 -05:00
|
|
|
var args = stringParts[3].split(',');
|
|
|
|
var partModel = sqlPart.create({ type: typePart, params: args });
|
2018-01-30 15:13:55 -06:00
|
|
|
var partCount = this.target.groupBy.length;
|
|
|
|
|
|
|
|
if (partCount === 0) {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
} else if (typePart === 'time') {
|
|
|
|
this.target.groupBy.splice(0, 0, partModel.part);
|
2018-03-04 12:35:43 -06:00
|
|
|
} else if (typePart === 'column') {
|
2018-01-30 15:13:55 -06:00
|
|
|
if (this.target.groupBy[partCount - 1].type === 'fill') {
|
|
|
|
this.target.groupBy.splice(partCount - 1, 0, partModel.part);
|
|
|
|
} else {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateProjection();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeGroupByPart(part, index) {
|
|
|
|
if (part.def.type === 'time') {
|
|
|
|
// remove aggregations
|
|
|
|
this.target.select = _.map(this.target.select, (s: any) => {
|
|
|
|
return _.filter(s, (part: any) => {
|
2018-07-04 05:22:45 -05:00
|
|
|
if (part.type === 'aggregate') {
|
2018-01-30 15:13:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.target.groupBy.splice(index, 1);
|
|
|
|
this.updateProjection();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSelect(index: number) {
|
|
|
|
this.target.select.splice(index, 1);
|
|
|
|
this.updateProjection();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSelectPart(selectParts, part) {
|
|
|
|
// if we remove the field remove the whole statement
|
2018-03-03 13:57:00 -06:00
|
|
|
if (part.def.type === 'column') {
|
2018-01-30 15:13:55 -06:00
|
|
|
if (this.selectModels.length > 1) {
|
|
|
|
var modelsIndex = _.indexOf(this.selectModels, selectParts);
|
|
|
|
this.selectModels.splice(modelsIndex, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var partIndex = _.indexOf(selectParts, part);
|
|
|
|
selectParts.splice(partIndex, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updatePersistedParts();
|
|
|
|
}
|
|
|
|
|
2018-07-04 05:22:45 -05:00
|
|
|
removeWherePart(whereParts, part) {
|
|
|
|
var partIndex = _.indexOf(whereParts, part);
|
|
|
|
whereParts.splice(partIndex, 1);
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:13:55 -06:00
|
|
|
addSelectPart(selectParts, type) {
|
2018-07-04 05:22:45 -05:00
|
|
|
var partModel = sqlPart.create({ type: type });
|
2018-01-30 15:13:55 -06:00
|
|
|
partModel.def.addStrategy(selectParts, partModel, this);
|
|
|
|
this.updatePersistedParts();
|
|
|
|
}
|
|
|
|
|
|
|
|
interpolateQueryStr(value, variable, defaultFormatFn) {
|
|
|
|
// if no multi or include all do not regexEscape
|
|
|
|
if (!variable.multi && !variable.includeAll) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
2018-03-14 12:09:47 -05:00
|
|
|
return this.quoteLiteral(value);
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
2018-03-14 12:09:47 -05:00
|
|
|
var escapedValues = _.map(value, this.quoteLiteral);
|
2018-03-13 17:33:40 -05:00
|
|
|
return '(' + escapedValues.join(',') + ')';
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render(interpolate?) {
|
|
|
|
var target = this.target;
|
|
|
|
|
|
|
|
if (target.rawQuery) {
|
|
|
|
if (interpolate) {
|
|
|
|
return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
|
|
|
|
} else {
|
|
|
|
return target.rawSql;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = 'SELECT ';
|
2018-03-03 13:57:00 -06:00
|
|
|
|
2018-03-04 12:35:43 -06:00
|
|
|
var timeGroup = this.hasGroupByTime();
|
|
|
|
|
|
|
|
if (timeGroup) {
|
|
|
|
var args;
|
2018-07-04 05:22:45 -05:00
|
|
|
if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') {
|
|
|
|
args = timeGroup.params.join(',');
|
2018-03-04 12:35:43 -06:00
|
|
|
} else {
|
|
|
|
args = timeGroup.params[0];
|
|
|
|
}
|
2018-05-21 05:10:18 -05:00
|
|
|
query += '$__timeGroup(' + this.quoteIdentifier(target.timeColumn) + ',' + args + ')';
|
2018-03-03 13:57:00 -06:00
|
|
|
} else {
|
2018-05-21 05:10:18 -05:00
|
|
|
query += this.quoteIdentifier(target.timeColumn) + ' AS time';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.target.metricColumn !== 'None') {
|
2018-07-04 05:22:45 -05:00
|
|
|
query += ',' + this.quoteIdentifier(this.target.metricColumn) + ' AS metric';
|
2018-03-03 13:57:00 -06:00
|
|
|
}
|
2018-01-30 15:13:55 -06:00
|
|
|
|
|
|
|
var i, y;
|
|
|
|
for (i = 0; i < this.selectModels.length; i++) {
|
|
|
|
let parts = this.selectModels[i];
|
|
|
|
var selectText = '';
|
|
|
|
for (y = 0; y < parts.length; y++) {
|
|
|
|
let part = parts[y];
|
|
|
|
selectText = part.render(selectText);
|
|
|
|
}
|
|
|
|
|
2018-05-21 05:10:18 -05:00
|
|
|
query += ', ' + selectText;
|
2018-03-04 16:32:23 -06:00
|
|
|
}
|
|
|
|
|
2018-03-09 11:09:59 -06:00
|
|
|
query += ' FROM ' + this.quoteIdentifier(target.schema) + '.' + this.quoteIdentifier(target.table) + ' WHERE ';
|
2018-02-08 03:25:32 -06:00
|
|
|
var conditions = _.map(target.where, (tag, index) => {
|
2018-07-04 05:22:45 -05:00
|
|
|
return tag.params.join(' ');
|
2018-01-30 15:13:55 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (conditions.length > 0) {
|
|
|
|
query += '(' + conditions.join(' ') + ') AND ';
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:57:38 -06:00
|
|
|
query += '$__timeFilter(' + this.quoteIdentifier(target.timeColumn) + ')';
|
2018-01-30 15:13:55 -06:00
|
|
|
|
|
|
|
var groupBySection = '';
|
|
|
|
for (i = 0; i < this.groupByParts.length; i++) {
|
|
|
|
var part = this.groupByParts[i];
|
|
|
|
if (i > 0) {
|
2018-03-03 13:57:00 -06:00
|
|
|
groupBySection += ', ';
|
|
|
|
}
|
|
|
|
if (part.def.type === 'time') {
|
2018-03-04 12:35:43 -06:00
|
|
|
groupBySection += '1';
|
2018-03-03 13:57:00 -06:00
|
|
|
} else {
|
|
|
|
groupBySection += part.render('');
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupBySection.length) {
|
|
|
|
query += ' GROUP BY ' + groupBySection;
|
2018-07-04 05:22:45 -05:00
|
|
|
if (this.target.metricColumn !== 'None') {
|
|
|
|
query += ',2';
|
2018-05-21 05:10:18 -05:00
|
|
|
}
|
2018-01-30 15:13:55 -06:00
|
|
|
}
|
|
|
|
|
2018-03-04 12:35:43 -06:00
|
|
|
query += ' ORDER BY 1';
|
2018-01-30 15:13:55 -06:00
|
|
|
|
2018-03-04 12:46:11 -06:00
|
|
|
this.target.rawSql = query;
|
2018-03-03 15:11:51 -06:00
|
|
|
if (interpolate) {
|
|
|
|
query = this.templateSrv.replace(query, this.scopedVars, this.interpolateQueryStr);
|
|
|
|
}
|
2018-01-30 15:13:55 -06:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
}
|