always quote template variables for mysql when multi-value is allowed (#9712)

* always quote template variables for mysql when multi-value is allowed

* handle include all option similar to multi value

* declare type

* adjust tests to quoting change

* dont specify type but let it be inferred

* fix test for variable with includeAll
This commit is contained in:
Sven Klemm
2017-10-31 12:23:50 +01:00
committed by Torkel Ödegaard
parent caa8b6100e
commit b6fafb13f5
2 changed files with 29 additions and 5 deletions

View File

@@ -15,9 +15,13 @@ export class MysqlDatasource {
this.responseParser = new ResponseParser(this.$q);
}
interpolateVariable(value) {
interpolateVariable(value, variable) {
if (typeof value === 'string') {
return value;
if (variable.multi || variable.includeAll) {
return '\'' + value + '\'';
} else {
return value;
}
}
if (typeof value === 'number') {

View File

@@ -2,6 +2,7 @@ import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
import moment from 'moment';
import helpers from 'test/specs/helpers';
import {MysqlDatasource} from '../datasource';
import {CustomVariable} from 'app/features/templating/custom_variable';
describe('MySQLDatasource', function() {
var ctx = new helpers.ServiceTestContext();
@@ -195,22 +196,41 @@ describe('MySQLDatasource', function() {
});
describe('When interpolating variables', () => {
beforeEach(function() {
ctx.variable = new CustomVariable({},{});
});
describe('and value is a string', () => {
it('should return an unquoted value', () => {
expect(ctx.ds.interpolateVariable('abc')).to.eql('abc');
expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('abc');
});
});
describe('and value is a number', () => {
it('should return an unquoted value', () => {
expect(ctx.ds.interpolateVariable(1000)).to.eql(1000);
expect(ctx.ds.interpolateVariable(1000, ctx.variable)).to.eql(1000);
});
});
describe('and value is an array of strings', () => {
it('should return comma separated quoted values', () => {
expect(ctx.ds.interpolateVariable(['a', 'b', 'c'])).to.eql('\'a\',\'b\',\'c\'');
expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).to.eql('\'a\',\'b\',\'c\'');
});
});
describe('and variable allows multi-value and value is a string', () => {
it('should return a quoted value', () => {
ctx.variable.multi = true;
expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('\'abc\'');
});
});
describe('and variable allows all and value is a string', () => {
it('should return a quoted value', () => {
ctx.variable.includeAll = true;
expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('\'abc\'');
});
});
});
});