MSSQL/MySQL: Fix variable interpolation (#56879)

* MSSQL/MySQL: Fix variable interpolation

* Escape string vars
This commit is contained in:
Victor Marin 2022-10-14 11:52:08 +03:00 committed by GitHub
parent e819ed0f51
commit 902a230867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 3 additions and 40 deletions

View File

@ -55,10 +55,9 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
interpolateVariable = (value: string | string[] | number, variable: VariableWithMultiSupport) => {
if (typeof value === 'string') {
if (variable.multi || variable.includeAll) {
const result = this.getQueryModel().quoteLiteral(value);
return result;
return this.getQueryModel().quoteLiteral(value);
} else {
return value;
return String(value).replace(/'/g, "''");
}
}
@ -98,20 +97,14 @@ export abstract class SqlDatasource extends DataSourceWithBackend<SQLQuery, SQLO
target: SQLQuery,
scopedVars: ScopedVars
): Record<string, string | DataSourceRef | SQLQuery['format']> {
const queryModel = this.getQueryModel(target, this.templateSrv, scopedVars);
const rawSql = this.clean(queryModel.interpolate());
return {
refId: target.refId,
datasource: this.getRef(),
rawSql,
rawSql: this.templateSrv.replace(target.rawSql, scopedVars, this.interpolateVariable),
format: target.format,
};
}
clean(value: string) {
return value.replace(/''/g, "'");
}
async metricFindQuery(query: string, optionalOptions?: MetricFindQueryOptions): Promise<MetricFindValue[]> {
const rawSql = this.templateSrv.replace(
query,

View File

@ -166,7 +166,6 @@ export interface ValidationResults {
}
export interface SqlQueryModel {
interpolate: () => string;
quoteLiteral: (v: string) => string;
}

View File

@ -2,7 +2,6 @@ import { ScopedVars } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
import { applyQueryDefaults } from 'app/features/plugins/sql/defaults';
import { SQLQuery, SqlQueryModel } from 'app/features/plugins/sql/types';
import { FormatRegistryID } from 'app/features/templating/formatRegistry';
export class MSSqlQueryModel implements SqlQueryModel {
target: SQLQuery;
@ -15,10 +14,6 @@ export class MSSqlQueryModel implements SqlQueryModel {
this.scopedVars = scopedVars;
}
interpolate() {
return this.templateSrv?.replace(this.target.rawSql, this.scopedVars, FormatRegistryID.sqlString) || '';
}
quoteLiteral(value: string) {
return "'" + value.replace(/'/g, "''") + "'";
}

View File

@ -1,5 +1,3 @@
import { map } from 'lodash';
import { ScopedVars } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
@ -33,28 +31,6 @@ export default class MySQLQueryModel {
return "'" + value.replace(/'/g, "''") + "'";
}
escapeLiteral(value: string) {
return String(value).replace(/'/g, "''");
}
format = (value: string, variable: { multi: boolean; includeAll: boolean }) => {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return this.escapeLiteral(value);
}
if (typeof value === 'string') {
return this.quoteLiteral(value);
}
const escapedValues = map(value, this.quoteLiteral);
return escapedValues.join(',');
};
interpolate() {
return this.templateSrv!.replace(this.target.rawSql, this.scopedVars, this.format);
}
getDatabase() {
return this.target.dataset;
}