Browse Source

refactor adding sqlPart

Sven Klemm 7 years ago
parent
commit
f85c9c012e

+ 0 - 4
public/app/core/components/sql_part/sql_part.ts

@@ -9,8 +9,6 @@ export class SqlPartDef {
   wrapOpen: string;
   wrapClose: string;
   separator: string;
-  category: any;
-  addStrategy: any;
 
   constructor(options: any) {
     this.type = options.type;
@@ -31,8 +29,6 @@ export class SqlPartDef {
     }
     this.params = options.params;
     this.defaultParams = options.defaultParams;
-    this.category = options.category;
-    this.addStrategy = options.addStrategy;
   }
 }
 

+ 1 - 1
public/app/plugins/datasource/postgres/partials/query.editor.html

@@ -44,7 +44,7 @@
       <div class="gf-form">
         <label class="dropdown"
                 dropdown-typeahead="ctrl.selectMenu"
-                dropdown-typeahead-on-select="ctrl.addSelectPart(selectParts, $item, $subItem)">
+                dropdown-typeahead-on-select="ctrl.addSelectPart(selectParts, $item)">
         </label>
       </div>
 

+ 38 - 3
public/app/plugins/datasource/postgres/query_ctrl.ts

@@ -222,9 +222,44 @@ export class PostgresQueryCtrl extends QueryCtrl {
     };
   }
 
-  addSelectPart(selectParts, cat, subitem) {
-    let partModel = sqlPart.create({ type: cat.value });
-    partModel.def.addStrategy(selectParts, partModel, this);
+  addSelectPart(selectParts, item) {
+    let partModel = sqlPart.create({ type: item.value });
+    let addAlias = false;
+
+    switch (item.value) {
+      case 'column':
+        let parts = _.map(selectParts, function(part: any) {
+          return sqlPart.create({ type: part.def.type, params: _.clone(part.params) });
+        });
+        this.selectModels.push(parts);
+        break;
+      case 'aggregate':
+      case 'special':
+        let index = _.findIndex(selectParts, (p: any) => p.def.type === item.value);
+        if (index !== -1) {
+          selectParts[index] = partModel;
+        } else {
+          selectParts.splice(1, 0, partModel);
+        }
+        if (!_.find(selectParts, (p: any) => p.def.type === 'alias')) {
+          addAlias = true;
+        }
+        break;
+      case 'alias':
+        addAlias = true;
+        break;
+    }
+
+    if (addAlias) {
+      // set initial alias name to column name
+      partModel = sqlPart.create({ type: 'alias', params: [selectParts[0].params[0]] });
+      if (selectParts[selectParts.length - 1].def.type === 'alias') {
+        selectParts[selectParts.length - 1] = partModel;
+      } else {
+        selectParts.push(partModel);
+      }
+    }
+
     this.updatePersistedParts();
     this.panelCtrl.refresh();
   }

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

@@ -16,109 +16,9 @@ function register(options: any) {
   index[options.type] = new SqlPartDef(options);
 }
 
-function replaceAggregationAddStrategy(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 === 'aggregate') {
-      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 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) {
-    // if last is math, replace it
-    if (selectParts[partCount - 1].def.type === 'math') {
-      selectParts[partCount - 1] = partModel;
-      return;
-    }
-    // if next to last is math, replace it
-    if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
-      selectParts[partCount - 2] = partModel;
-      return;
-    } else if (selectParts[partCount - 1].def.type === 'alias') {
-      // if last is alias add it before
-      selectParts.splice(partCount - 1, 0, partModel);
-      return;
-    }
-  }
-  selectParts.push(partModel);
-}
-
-function addAliasStrategy(selectParts, partModel) {
-  var partCount = selectParts.length;
-  if (partCount > 0) {
-    // if last is alias, replace it
-    if (selectParts[partCount - 1].def.type === 'alias') {
-      selectParts[partCount - 1] = partModel;
-      return;
-    }
-  }
-  selectParts.push(partModel);
-}
-
-function addColumnStrategy(selectParts, partModel, query) {
-  // copy all parts
-  var parts = _.map(selectParts, function(part: any) {
-    return createPart({ type: part.def.type, params: _.clone(part.params) });
-  });
-
-  query.selectModels.push(parts);
-}
-
-function addExpressionStrategy(selectParts, partModel, query) {
-  // copy all parts
-  var parts = _.map(selectParts, function(part: any) {
-    return createPart({ type: part.def.type, params: _.clone(part.params) });
-  });
-
-  query.selectModels.push(parts);
-}
-
 register({
   type: 'column',
   style: 'label',
-  addStrategy: addColumnStrategy,
   params: [{ type: 'column', dynamicLookup: true }],
   defaultParams: ['value'],
 });
@@ -127,7 +27,6 @@ register({
   type: 'expression',
   style: 'expression',
   label: 'Expr:',
-  addStrategy: addExpressionStrategy,
   params: [
     { name: 'left', type: 'string', dynamicLookup: true },
     { name: 'op', type: 'string', dynamicLookup: true },
@@ -140,7 +39,6 @@ register({
   type: 'macro',
   style: 'label',
   label: 'Macro:',
-  addStrategy: addExpressionStrategy,
   params: [],
   defaultParams: [],
 });
@@ -148,23 +46,13 @@ register({
 register({
   type: 'aggregate',
   style: 'label',
-  addStrategy: replaceAggregationAddStrategy,
   params: [{ name: 'name', type: 'string', dynamicLookup: true }],
   defaultParams: ['avg'],
 });
 
-register({
-  type: 'math',
-  style: 'label',
-  addStrategy: addMathStrategy,
-  params: [{ name: 'expr', type: 'string' }],
-  defaultParams: [' / 100'],
-});
-
 register({
   type: 'alias',
   style: 'label',
-  addStrategy: addAliasStrategy,
   params: [{ name: 'name', type: 'string', quote: 'double' }],
   defaultParams: ['alias'],
 });
@@ -199,7 +87,6 @@ register({
     },
   ],
   defaultParams: ['increase'],
-  addStrategy: replaceSpecialAddStrategy,
 });
 
 export default {