Skip to content

Commit

Permalink
Merge pull request #47 from grafana/handle-numeric-template-variables
Browse files Browse the repository at this point in the history
Support numeric values when applying template variables to SQL queries
  • Loading branch information
kevinwcyu authored Jun 15, 2023
2 parents 07bd2a2 + 7b52a68 commit 7774bf8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/sql/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('filterQuery', () => {

const scopedVars: Record<string, any> = {
$simple: 'foo',
$numeric: 123,
$multiple: ['foo', 'bar'],
};
// simplified version of getTemplateSrv().replace
Expand All @@ -35,6 +36,15 @@ describe('applySQLTemplateVariables', () => {
expect(res.rawSQL).toEqual('select * from foo');
});

it('should replace a numeric var', () => {
const res = applySQLTemplateVariables(
{ ...mockQuery, rawSQL: 'select * from $numeric' },
scopedVars,
getTemplateSrv
);
expect(res.rawSQL).toEqual('select * from 123');
});

it('should replace a multiple var', () => {
const res = applySQLTemplateVariables(
{ ...mockQuery, rawSQL: 'select * from foo where var in ($multiple)' },
Expand Down
2 changes: 1 addition & 1 deletion src/sql/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function applySQLTemplateVariables(
}

function interpolateVariable(value: string | string[]) {
if (typeof value === 'string') {
if (typeof value === 'string' || typeof value === 'number') {
return value;
}

Expand Down

0 comments on commit 7774bf8

Please sign in to comment.