Browse Source

[core] Add table ref autocomplete for multiple tables in the statement

Johan Ahlen 10 years ago
parent
commit
2979c25

+ 6 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete.js

@@ -50,6 +50,7 @@ Autocompleter.prototype.getTableReferenceIndex = function (statement) {
       tableRefsRaw = $.trim(tableRefsRaw.substring(0, upToMatch.index));
     }
     var tableRefs = tableRefsRaw.split(",");
+    tableRefs.sort();
     $.each(tableRefs, function(index, tableRefRaw) {
       var tableMatch = tableRefRaw.match(/ *([^ ]*) ?([^ ]*)? */);
       result[tableMatch[2] || tableMatch[1]] = tableMatch[1];
@@ -197,6 +198,11 @@ Autocompleter.prototype.autocomplete = function(beforeCursor, afterCursor, callb
       // SELECT column.
       // We use first and only table reference
       tableName = tableReferences[Object.keys(tableReferences)[0]];
+    } else if (Object.keys(tableReferences).length > 1) {
+      callback($.map(Object.keys(tableReferences), function(key, idx) {
+        return { value: key + ".", score: 1000 - idx, meta: tableReferences[key] == key ? 'table' : 'alias' };
+      }));
+      return;
     } else {
       // No table refs
       callback([]);

+ 3 - 4
desktop/core/src/desktop/static/desktop/spec/autocompleteSpec.js

@@ -263,8 +263,7 @@ describe("autocomplete.js", function() {
       });
     });
 
-    // TODO: Fix me
-    xit("should suggest aliases", function() {
+    it("should suggest aliases", function() {
       assertAutoComplete({
         serverResponses: {
           "http://baseUrl/testDb/testTableA" : {
@@ -275,8 +274,8 @@ describe("autocomplete.js", function() {
           }
         },
         beforeCursor: "SELECT ",
-        afterCursor: " FROM testTableA tta, testTableB ttb",
-        expectedSuggestions: ["tta", "ttb"]
+        afterCursor: " FROM testTableA tta, testTableB",
+        expectedSuggestions: ["testTableB.", "tta."]
       });
     });