Переглянути джерело

prepare sql part for special functions

Sven Klemm 7 роки тому
батько
коміт
c8a11d597f

+ 1 - 0
public/app/plugins/datasource/postgres/query_ctrl.ts

@@ -78,6 +78,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
     this.selectMenu = [
       { text: 'Aggregate', value: 'aggregate' },
       { text: 'Math', value: 'math' },
+      //  { text: 'Special', value: 'special' },
       { text: 'Alias', value: 'alias' },
       { text: 'Column', value: 'column' },
     ];

+ 39 - 0
public/app/plugins/datasource/postgres/sql_part.ts

@@ -52,6 +52,30 @@ function replaceAggregationAddStrategy(selectParts, partModel) {
   selectParts.splice(1, 0, partModel);
 }
 
+function replaceSpecialAddStrategy(selectParts, partModel) {
+  var hasAlias = false;
+
+  // look for existing aggregation
+  for (var i = 0; i < selectParts.length; i++) {
+    var part = selectParts[i];
+    if (part.def.type === 'special') {
+      selectParts[i] = partModel;
+      return;
+    }
+    if (part.def.type === 'alias') {
+      hasAlias = true;
+    }
+  }
+
+  // add alias if none exists yet
+  if (!hasAlias) {
+    var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
+    selectParts.push(aliasModel);
+  }
+
+  selectParts.splice(1, 0, partModel);
+}
+
 function addMathStrategy(selectParts, partModel) {
   var partCount = selectParts.length;
   if (partCount > 0) {
@@ -184,6 +208,21 @@ register({
   renderer: functionRenderer,
 });
 
+register({
+  type: 'special',
+  style: 'label',
+  params: [
+    {
+      name: 'function',
+      type: 'string',
+      options: ['increase', 'rate'],
+    },
+  ],
+  defaultParams: ['increase'],
+  addStrategy: replaceSpecialAddStrategy,
+  renderer: aggregateRenderer,
+});
+
 export default {
   create: createPart,
 };