Browse Source

[editor] Auto propose to add a LIMIT

Romain Rigaux 4 years ago
parent
commit
0168055627

+ 1 - 1
desktop/core/src/desktop/js/catalog/optimizer/SqlAnalyzer.ts

@@ -67,7 +67,7 @@ export default class SqlAnalyzer implements Optimizer {
             {
               riskTables: [],
               riskAnalysis: I18n('Query has no limit'),
-              riskId: 22, // To change
+              riskId: 17,
               risk: 'low',
               riskRecommendation: I18n('Append a limit clause to reduce the size of the result set')
             }

+ 38 - 1
desktop/core/src/desktop/js/ko/components/assist/ko.assistEditorContextPanel.js

@@ -660,9 +660,10 @@ class AssistEditorContextPanel {
         }
 
         if (
+          riskId === 22 &&
           location.type === 'whereClause' &&
           !location.subquery &&
-          (location.missing || riskId === 22)
+          location.missing
         ) {
           this.activeEditor().moveCursorToPosition({
             row: location.location.last_line - 1,
@@ -694,6 +695,42 @@ class AssistEditorContextPanel {
           return false;
         }
 
+        if (
+          riskId === 17 &&
+          location.type === 'limitClause' &&
+          !location.subquery &&
+          location.missing
+        ) {
+          this.activeEditor().moveCursorToPosition({
+            row: location.location.last_line - 1,
+            column: location.location.last_column - 1
+          });
+          this.activeEditor().clearSelection();
+
+          if (/\S$/.test(this.activeEditor().getTextBeforeCursor())) {
+            this.activeEditor().session.insert(this.activeEditor().getCursorPosition(), ' ');
+          }
+
+          const operation = 'LIMIT';
+          this.activeEditor().session.insert(
+            this.activeEditor().getCursorPosition(),
+            isLowerCase ? operation.toLowerCase() : operation
+          );
+          this.activeEditor().focus();
+
+          if (riskId === 17) {
+            huePubSub.publish('editor.autocomplete.temporary.sort.override', {
+              partitionColumnsFirst: true
+            });
+          }
+
+          window.setTimeout(() => {
+            this.activeEditor().execCommand('startAutocomplete');
+          }, 1);
+
+          return false;
+        }
+
         return true;
       });
     }