浏览代码

mysql: added basic templating support

Torkel Ödegaard 8 年之前
父节点
当前提交
8f17a84f31
共有 1 个文件被更改,包括 13 次插入2 次删除
  1. 13 2
      public/app/plugins/datasource/mysql/datasource.ts

+ 13 - 2
public/app/plugins/datasource/mysql/datasource.ts

@@ -7,11 +7,22 @@ export class MysqlDatasource {
   name: any;
 
   /** @ngInject */
-  constructor(instanceSettings, private backendSrv, private $q) {
+  constructor(instanceSettings, private backendSrv, private $q, private templateSrv) {
     this.name = instanceSettings.name;
     this.id = instanceSettings.id;
   }
 
+  interpolateVariable(value) {
+    if (typeof value === 'string') {
+      return '\"' + value + '\"';
+    }
+
+    var quotedValues = _.map(value, function(val) {
+      return '\"' + val + '\"';
+    });
+    return  quotedValues.join(',');
+  }
+
   query(options) {
     var queries = _.filter(options.targets, item => {
       return item.hide !== true;
@@ -21,7 +32,7 @@ export class MysqlDatasource {
         intervalMs: options.intervalMs,
         maxDataPoints: options.maxDataPoints,
         datasourceId: this.id,
-        rawSql: item.rawSql,
+        rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
         format: item.format,
       };
     });