Browse Source

[Trino] Update drag and drop suggestions to use double quotes instead of backticks (#3853)

Ayush Goyal 1 year ago
parent
commit
ce781c6fb2
1 changed files with 35 additions and 8 deletions
  1. 35 8
      desktop/core/src/desktop/js/sql/sqlUtils.ts

+ 35 - 8
desktop/core/src/desktop/js/sql/sqlUtils.ts

@@ -267,24 +267,51 @@ export default {
     sqlReferenceProvider?: SqlReferenceProvider,
     sqlReferenceProvider?: SqlReferenceProvider,
     forceAppendBacktick?: boolean
     forceAppendBacktick?: boolean
   ): Promise<string> => {
   ): Promise<string> => {
+    // Determine the quote character based on the connector dialect
+    let quoteChar =
+      (connector.dialect_properties && connector.dialect_properties.sql_identifier_quote) || '`';
+
+    // Use double quotes for Trino
+    if (connector.dialect === 'trino') {
+      quoteChar = '"';
+    }
+
     if (forceAppendBacktick) {
     if (forceAppendBacktick) {
-      return identifier + '`';
+      return identifier + quoteChar;
     }
     }
-    const quoteChar =
-      (connector.dialect_properties && connector.dialect_properties.sql_identifier_quote) || '`';
-    if (identifier.indexOf(quoteChar) === 0) {
+
+    // Check if identifier is already quoted
+    if (identifier.startsWith(quoteChar) && identifier.endsWith(quoteChar)) {
       return identifier;
       return identifier;
     }
     }
+    // Check for multi-part identifiers (e.g., catalog.schema)
+    if (identifier.includes('.')) {
+      // Split the identifier into parts and quote each part separately
+      return identifier
+        .split('.')
+        .map(part => {
+          // Quote each part if it is not already quoted
+          if (part.startsWith(quoteChar) && part.endsWith(quoteChar)) {
+            return part;
+          } else {
+            return `${quoteChar}${part}${quoteChar}`;
+          }
+        })
+        .join('.');
+    }
+
     const reservedKeywords = await (
     const reservedKeywords = await (
       sqlReferenceProvider || sqlReferenceRepository
       sqlReferenceProvider || sqlReferenceRepository
     ).getReservedKeywords(connector.dialect || 'generic');
     ).getReservedKeywords(connector.dialect || 'generic');
-    if (reservedKeywords.has(identifier.toUpperCase())) {
-      return quoteChar + identifier + quoteChar;
-    }
 
 
-    if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(identifier)) {
+    // Quote the identifier if it is a reserved keyword or not a valid SQL identifier
+    if (
+      reservedKeywords.has(identifier.toUpperCase()) ||
+      !/^[A-Za-z][A-Za-z0-9_]*$/.test(identifier)
+    ) {
       return quoteChar + identifier + quoteChar;
       return quoteChar + identifier + quoteChar;
     }
     }
+
     return identifier;
     return identifier;
   },
   },
   locationEquals: (a?: ParsedLocation, b?: ParsedLocation): boolean =>
   locationEquals: (a?: ParsedLocation, b?: ParsedLocation): boolean =>