Files
grafana/public/app/plugins/datasource/mysql/MySqlQueryModel.ts
Scott Lepper 53933972b6 mysql query editor - angular to react (#50343)
mysql conversion to react

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
2022-07-15 16:49:24 -04:00

62 lines
1.6 KiB
TypeScript

import { map } from 'lodash';
import { ScopedVars } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
import { MySQLQuery } from './types';
export default class MySQLQueryModel {
target: Partial<MySQLQuery>;
templateSrv?: TemplateSrv;
scopedVars?: ScopedVars;
constructor(target: Partial<MySQLQuery>, templateSrv?: TemplateSrv, scopedVars?: ScopedVars) {
this.target = target;
this.templateSrv = templateSrv;
this.scopedVars = scopedVars;
}
// remove identifier quoting from identifier to use in metadata queries
unquoteIdentifier(value: string) {
if (value[0] === '"' && value[value.length - 1] === '"') {
return value.substring(1, value.length - 1).replace(/""/g, '"');
} else {
return value;
}
}
quoteIdentifier(value: string) {
return '"' + value.replace(/"/g, '""') + '"';
}
quoteLiteral(value: string) {
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;
}
}