Эх сурвалжийг харах

[editor] Support a custom SQL reference provider in sqlUtils

Johan Ahlen 4 жил өмнө
parent
commit
a399148931

+ 2 - 1
desktop/core/src/desktop/js/apps/editor/components/aceEditor/AceEditor.vue

@@ -148,7 +148,8 @@
       this.aceLocationHandler = new AceLocationHandler({
         editor: editor,
         editorId: this.id,
-        executor: this.executor
+        executor: this.executor,
+        sqlReferenceProvider: this.sqlReferenceProvider
       });
       this.subTracker.addDisposable(this.aceLocationHandler);
 

+ 9 - 1
desktop/core/src/desktop/js/apps/editor/components/aceEditor/AceLocationHandler.ts

@@ -44,6 +44,7 @@ import {
   POST_TO_SYNTAX_WORKER_EVENT
 } from 'sql/sqlWorkerHandler';
 import { getFromLocalStorage } from 'utils/storageUtils';
+import { SqlReferenceProvider } from '../../../../sql/reference/types';
 
 export const REFRESH_STATEMENT_LOCATIONS_EVENT = 'editor.refresh.statement.locations';
 export const ACTIVE_STATEMENT_CHANGED_EVENT = 'editor.active.statement.changed';
@@ -108,6 +109,7 @@ export default class AceLocationHandler implements Disposable {
   updateTimeout = -1;
   cursorChangePaused = false;
   sqlSyntaxWorkerSub?: HueSubscription;
+  sqlReferenceProvider?: SqlReferenceProvider;
 
   activeStatement: ParsedSqlStatement | undefined;
   lastKnownStatements = {
@@ -120,11 +122,13 @@ export default class AceLocationHandler implements Disposable {
     editorId: string;
     executor: Executor;
     temporaryOnly?: boolean;
+    sqlReferenceProvider?: SqlReferenceProvider;
   }) {
     this.editor = options.editor;
     this.editorId = options.editorId;
     this.executor = options.executor;
     this.temporaryOnly = !!options.temporaryOnly;
+    this.sqlReferenceProvider = options.sqlReferenceProvider;
 
     this.attachStatementLocator();
     this.attachSqlWorker();
@@ -1141,7 +1145,11 @@ export default class AceLocationHandler implements Disposable {
               const uniqueValues: IdentifierChainEntry[] = [];
               for (let i = 0; i < possibleValues.length; i++) {
                 const entry = <IdentifierChainEntry>possibleValues[i];
-                entry.name = await sqlUtils.backTickIfNeeded(this.executor.connector(), entry.name);
+                entry.name = await sqlUtils.backTickIfNeeded(
+                  this.executor.connector(),
+                  entry.name,
+                  this.sqlReferenceProvider
+                );
                 const nameLower = entry.name.toLowerCase();
                 if (
                   nameLower === tokenValLower ||

+ 1 - 8
desktop/core/src/desktop/js/apps/editor/components/aceEditor/autocomplete/AceAutocomplete.vue

@@ -158,14 +158,7 @@
     setup(props) {
       const subTracker = new SubscriptionTracker();
 
-      const autocompleter = new SqlAutocompleter({
-        editorId: props.editorId,
-        executor: props.executor,
-        editor: props.editor,
-        temporaryOnly: props.temporaryOnly,
-        autocompleteParser: props.autocompleteParser,
-        sqlReferenceProvider: props.sqlReferenceProvider
-      });
+      const autocompleter = new SqlAutocompleter(props);
 
       const autocompleteResults = autocompleter.autocompleteResults;
 

+ 31 - 7
desktop/core/src/desktop/js/apps/editor/components/aceEditor/autocomplete/AutocompleteResults.ts

@@ -598,7 +598,11 @@ class AutocompleteResults {
           databaseSuggestions.push({
             value:
               prefix +
-              (await sqlUtils.backTickIfNeeded(this.executor.connector(), name)) +
+              (await sqlUtils.backTickIfNeeded(
+                this.executor.connector(),
+                name,
+                this.sqlReferenceProvider
+              )) +
               (suggestDatabases.appendDot ? '.' : ''),
             filterValue: name,
             meta: MetaLabels.Database,
@@ -664,7 +668,13 @@ class AutocompleteResults {
           }
           const name = tableEntry.name;
           tableSuggestions.push({
-            value: prefix + (await sqlUtils.backTickIfNeeded(this.executor.connector(), name)),
+            value:
+              prefix +
+              (await sqlUtils.backTickIfNeeded(
+                this.executor.connector(),
+                name,
+                this.sqlReferenceProvider
+              )),
             filterValue: name,
             tableName: name,
             meta: tableEntry.isView() ? MetaLabels.View : MetaLabels.Table,
@@ -793,7 +803,11 @@ class AutocompleteResults {
       const type = column.type && column.type !== 'COLREF' ? column.type : 'T';
       if (column.alias) {
         columnSuggestions.push({
-          value: await sqlUtils.backTickIfNeeded(this.executor.connector(), column.alias),
+          value: await sqlUtils.backTickIfNeeded(
+            this.executor.connector(),
+            column.alias,
+            this.sqlReferenceProvider
+          ),
           filterValue: column.alias,
           meta: type,
           category: Category.Column,
@@ -809,7 +823,8 @@ class AutocompleteResults {
         columnSuggestions.push({
           value: await sqlUtils.backTickIfNeeded(
             this.executor.connector(),
-            column.identifierChain[column.identifierChain.length - 1].name
+            column.identifierChain[column.identifierChain.length - 1].name,
+            this.sqlReferenceProvider
           ),
           filterValue: column.identifierChain[column.identifierChain.length - 1].name,
           meta: type,
@@ -840,7 +855,11 @@ class AutocompleteResults {
             typeof column.type !== 'undefined' && column.type !== 'COLREF' ? column.type : 'T';
           if (column.alias) {
             columnSuggestions.push({
-              value: await sqlUtils.backTickIfNeeded(connector, column.alias),
+              value: await sqlUtils.backTickIfNeeded(
+                connector,
+                column.alias,
+                this.sqlReferenceProvider
+              ),
               filterValue: column.alias,
               meta: type,
               category: Category.Column,
@@ -852,7 +871,8 @@ class AutocompleteResults {
             columnSuggestions.push({
               value: await sqlUtils.backTickIfNeeded(
                 connector,
-                column.identifierChain[column.identifierChain.length - 1].name
+                column.identifierChain[column.identifierChain.length - 1].name,
+                this.sqlReferenceProvider
               ),
               filterValue: column.identifierChain[column.identifierChain.length - 1].name,
               meta: type,
@@ -920,7 +940,11 @@ class AutocompleteResults {
         });
 
         for (const childEntry of childEntries) {
-          let name = await sqlUtils.backTickIfNeeded(this.executor.connector(), childEntry.name);
+          let name = await sqlUtils.backTickIfNeeded(
+            this.executor.connector(),
+            childEntry.name,
+            this.sqlReferenceProvider
+          );
           if (this.dialect() === HIVE_DIALECT && (childEntry.isArray() || childEntry.isMap())) {
             name += '[]';
           }

+ 9 - 4
desktop/core/src/desktop/js/sql/sqlUtils.ts

@@ -26,6 +26,7 @@ import {
   Suggestion
 } from 'apps/editor/components/aceEditor/autocomplete/AutocompleteResults';
 import { Compute, Connector, Namespace } from 'config/types';
+import { SqlReferenceProvider } from './reference/types';
 
 const identifierEquals = (a?: string, b?: string): boolean =>
   !!a &&
@@ -260,15 +261,19 @@ export const resolveCatalogEntry = (options: {
 
 export default {
   autocompleteFilter: autocompleteFilter,
-  backTickIfNeeded: async (connector: Connector, identifier: string): Promise<string> => {
+  backTickIfNeeded: async (
+    connector: Connector,
+    identifier: string,
+    sqlReferenceProvider?: SqlReferenceProvider
+  ): Promise<string> => {
     const quoteChar =
       (connector.dialect_properties && connector.dialect_properties.sql_identifier_quote) || '`';
     if (identifier.indexOf(quoteChar) === 0) {
       return identifier;
     }
-    const reservedKeywords = await sqlReferenceRepository.getReservedKeywords(
-      connector.dialect || 'generic'
-    );
+    const reservedKeywords = await (
+      sqlReferenceProvider || sqlReferenceRepository
+    ).getReservedKeywords(connector.dialect || 'generic');
     if (reservedKeywords.has(identifier.toUpperCase())) {
       return quoteChar + identifier + quoteChar;
     }