Bläddra i källkod

HUE-1757 [dbquery] Magic table autocompletion

Modify the codemirror-sql-hint.js to support magic table autocompletion.
Abraham Elmahrek 12 år sedan
förälder
incheckning
c8be55b

+ 20 - 1
apps/rdbms/src/rdbms/templates/execute.mako

@@ -325,6 +325,15 @@ ${ commonheader(_('Query'), app_name, user) | n,unicode }
 
     var AUTOCOMPLETE_SET = CodeMirror.sqlHint;
 
+    CodeMirror.onAutocomplete = function (data, from, to) {
+      if (CodeMirror.tableFieldMagic) {
+        codeMirror.replaceRange("  ", from, from);
+        from.ch = from.ch + 1;
+        codeMirror.setCursor(from);
+        codeMirror.execCommand("autocomplete");
+      }
+    };
+
     CodeMirror.commands.autocomplete = function (cm) {
       $(document.body).on("contextmenu", function (e) {
         e.preventDefault(); // prevents native menu on FF for Mac from being shown
@@ -348,12 +357,19 @@ ${ commonheader(_('Query'), app_name, user) | n,unicode }
               options['tables'][table] = [];
             }
           });
+          CodeMirror.possibleTable = false;
+          CodeMirror.tableFieldMagic = false;
+          CodeMirror.possibleSoloField = false;
           var _before = codeMirror.getRange({line: 0, ch: 0}, {line: codeMirror.getCursor().line, ch: codeMirror.getCursor().ch}).replace(/(\r\n|\n|\r)/gm, " ");
+          if (_before.toUpperCase().indexOf(" FROM ") > -1 && _before.toUpperCase().indexOf(" ON ") == -1 && _before.toUpperCase().indexOf(" WHERE ") == -1) {
+            CodeMirror.possibleTable = true;
+          }
           if (_before.toUpperCase().indexOf("SELECT ") > -1 && _before.toUpperCase().indexOf(" FROM ") == -1 && !CodeMirror.fromDot) {
             if (codeMirror.getValue().toUpperCase().indexOf("FROM ") > -1) {
               fieldsAutocomplete(codeMirror, options);
             } else {
-              CodeMirror.showHint(codeMirror, AUTOCOMPLETE_SET);
+              CodeMirror.tableFieldMagic = true;
+              CodeMirror.showHint(codeMirror, AUTOCOMPLETE_SET, options);
             }
           }
           else {
@@ -369,6 +385,7 @@ ${ commonheader(_('Query'), app_name, user) | n,unicode }
     }
 
     function fieldsAutocomplete(cm, options) {
+      CodeMirror.possibleSoloField = true;
       try {
         var _possibleTables = $.trim(codeMirror.getValue(" ").substr(codeMirror.getValue().toUpperCase().indexOf("FROM ") + 4)).split(" ");
         var _foundTable = "";
@@ -379,10 +396,12 @@ ${ commonheader(_('Query'), app_name, user) | n,unicode }
         }
         if (_foundTable != "" && _foundTable in options['tables']) {
           if (rdbms_tableHasAlias(viewModel.server().name(), _foundTable, codeMirror.getValue())) {
+            CodeMirror.possibleSoloField = false;
             CodeMirror.showHint(cm, AUTOCOMPLETE_SET, options);
           } else {
             rdbms_getTableColumns(viewModel.server().name(), viewModel.database(), _foundTable, codeMirror.getValue(),
                 function (columns) {
+                  CodeMirror.table = _foundTable;
                   options['tables'][_foundTable] = columns.split(' ');
                   CodeMirror.showHint(cm, AUTOCOMPLETE_SET, options);
                 });

+ 26 - 7
desktop/core/static/js/codemirror-sql-hint.js

@@ -85,20 +85,39 @@
 
     var search = token.string.trim();
 
-    addMatches(result, search, keywords,
-        function(w) {return w.toUpperCase();});
+    var from = CodeMirror.Pos(cur.line, token.start);
+    var to = CodeMirror.Pos(cur.line, token.end);
 
-    addMatches(result, search, tables,
-        function(w) {return w;});
+    if (CodeMirror.possibleSoloField && CodeMirror.table && CodeMirror.table in tables) {
+      var columns = tables[CodeMirror.table];
+      addMatches(result, search, columns, function(w) {return w;});
 
-    if(search.lastIndexOf('.') === 0) {
+      // Token search/replace replaces "SELECT  FROM test" with "SELECTtestFROM test".
+      // Changing the start position and end position fixes that.
+      if (!search) {
+        from = cur;
+        to = cur;
+      }
+    } else if(search.lastIndexOf('.') === 0) {
       columnCompletion(result, editor);
+    } else if (CodeMirror.possibleTable) {
+      addMatches(result, search, tables,
+        function(w) {return w;});
+    } else {
+      if (CodeMirror.tableFieldMagic) {
+        addMatches(result, search, tables, function(w) {
+          return "<i class='fa fa-magic'></i> FROM " + w.trim();
+        });
+      } else {
+        addMatches(result, search, keywords,
+          function(w) {return w.toUpperCase();});
+      }
     }
 
     return {
       list: result,
-        from: CodeMirror.Pos(cur.line, token.start),
-        to: CodeMirror.Pos(cur.line, token.end)
+      from: from,
+      to: to
     };
   }
   CodeMirror.sqlHint = sqlHint;