25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): keep catalog identifier case in completion for non-folding dialects (#12947)
Loc Nguyen2cf099542368
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.tsindex ba6e210cb3..01604027ce 100644--- a/apps/desktop/src/lib/sql/sqlCompletion.ts+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts@@ -4586,28 +4586,22 @@ function quoteSelectStarColumnIdentifier(identifier: string, dialect?: SqlComplereturn quoteSqlIdentifier(identifier, dialect);}-function applySqlIdentifierCase(value: string, identifierCase?: SqlKeywordCase): string {+// Only dialects that fold unquoted identifiers (Oracle to upper, PostgreSQL to+// lower) can re-case a name and still resolve the same object. Elsewhere, e.g.+// MySQL on Linux, `settle_mz` and `SETTLE_MZ` are different tables (#12938).+function applySqlIdentifierCase(value: string, identifierCase?: SqlKeywordCase, dialect?: SqlCompletionApplyDialect): string {if (!identifierCase || identifierCase === "preserve" || isQuotedIdentifier(value.trim())) return value;- if (identifierCase === "lower") return value.toLowerCase();- if (identifierCase === "upper") return value.toUpperCase();- return value;+ if (dialect !== "oracle" && dialect !== "upper" && dialect !== "postgres") return value;+ if (quoteCompletionApplyIdentifier(value, dialect) !== value) return value;+ const cased = identifierCase === "lower" ? value.toLowerCase() : identifierCase === "upper" ? value.toUpperCase() : value;+ const pattern = dialect === "postgres" ? /^[A-Za-z_][A-Za-z0-9_$]*$/ : /^[A-Za-z][A-Za-z0-9_$#]*$/;+ return pattern.test(cased) && !POSTGRES_IDENTIFIER_KEYWORDS.has(cased.toLowerCase()) ? cased : value;}function quoteCasedIdentifier(originalName: string, casedName: string, dialect?: SqlCompletionApplyDialect): string {if (isQuotedIdentifier(originalName.trim())) return originalName;- const originalQuoted = quoteCompletionApplyIdentifier(originalName, dialect);- if (originalQuoted === originalName) {- if (dialect === "oracle" || dialect === "upper") {- if (/^[A-Za-z][A-Za-z0-9_$#]*$/.test(casedName) && !POSTGRES_IDENTIFIER_KEYWORDS.has(casedName.toLowerCase())) {- return casedName;- }- } else if (dialect === "postgres") {- if (/^[A-Za-z_][A-Za-z0-9_$]*$/.test(casedName) && !POSTGRES_IDENTIFIER_KEYWORDS.has(casedName.toLowerCase())) {- return casedName;- }- }- }- return quoteCompletionApplyIdentifier(casedName, dialect);+ // applySqlIdentifierCase only returns a different spelling when it is safe unquoted.+ return casedName !== originalName ? casedName : quoteCompletionApplyIdentifier(originalName, dialect);}function applySqlIdentifierCaseToName(name: string, identifierCase?: SqlKeywordCase, dialect?: SqlCompletionApplyDialect): string {@@ -4617,7 +4611,7 @@ function applySqlIdentifierCaseToName(name: string, identifierCase?: SqlKeywordCreturn parts.map((part) => {if (isQuotedIdentifier(part.trim())) return part;- const cased = applySqlIdentifierCase(part, identifierCase);+ const cased = applySqlIdentifierCase(part, identifierCase, dialect);return quoteCasedIdentifier(part, cased, dialect);}).join(".");@@ -4665,9 +4659,9 @@ function resolveTableSchemaQualification(// generic/PostgreSQL/SQL Server paths only when metadata is ambiguous.const ambiguousTableName = !isOracleCompletionDatabase(databaseType) && (schemasByTableName.get(normalizeIdentifierPart(table.name))?.size ?? 0) > 1;const schemaQualification = !!table.schema && (schemaQualificationMode === "always" || (schemaQualificationMode === "collision" && (oracleSchemaQualification || ambiguousTableName)));- const tableName = applySqlIdentifierCase(table.name, identifierCase);+ const tableName = applySqlIdentifierCase(table.name, identifierCase, dialect);const quotedTable = quoteCasedIdentifier(table.name, tableName, dialect);- const defaultApplyName = schemaQualification && table.schema ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), dialect)}.${quotedTable}` : quotedTable;+ const defaultApplyName = schemaQualification && table.schema ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase, dialect), dialect)}.${quotedTable}` : quotedTable;return { ambiguousTableName, schemaQualification, defaultApplyName };}@@ -4697,7 +4691,7 @@ function buildReferencedTableItems(context: SqlCompletionContext, completionTablconst key = normalizeIdentifierPart(reference.name);if (!key || knownTables.has(key) || seen.has(key)) continue;seen.add(key);- const tableName = applySqlIdentifierCase(reference.name, identifierCase);+ const tableName = applySqlIdentifierCase(reference.name, identifierCase, dialect);const applyName = quoteCasedIdentifier(reference.name, tableName, dialect);items.push({label: tableName,@@ -4773,7 +4767,7 @@ function buildTableItems(.map((table) => {const qualifiedByContext = !!qualifierSchema && !!table.schema && normalizeIdentifierPart(qualifierSchema) === normalizeIdentifierPart(table.schema);const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode, identifierCase);- const tableName = applySqlIdentifierCase(table.name, identifierCase);+ const tableName = applySqlIdentifierCase(table.name, identifierCase, dialect);const unqualifiedApplyName = quoteCasedIdentifier(table.name, tableName, dialect);const rawSuppliedApplyName = table.applyName?.trim();const suppliedApplyName = dialect === "sqlserver" && rawSuppliedApplyName ? quoteCompletionApplyName(rawSuppliedApplyName, dialect) : rawSuppliedApplyName;@@ -4804,7 +4798,7 @@ function buildTableItems(comment,dedupeKey:schemaQualificationMode === "never" && table.schema- ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), dialect)}.${unqualifiedApplyName}`+ ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase, dialect), dialect)}.${unqualifiedApplyName}`: table.applyName || ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema)? applyName: undefined,@@ -4857,7 +4851,7 @@ function buildForeignKeyRelatedTableItems(.map(({ table, detail }) => {const qualifiedByContext = !!qualifierSchema && !!table.schema && normalizeIdentifierPart(qualifierSchema) === normalizeIdentifierPart(table.schema);const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode, identifierCase);- const tableName = applySqlIdentifierCase(table.name, identifierCase);+ const tableName = applySqlIdentifierCase(table.name, identifierCase, dialect);const unqualifiedApplyName = quoteCasedIdentifier(table.name, tableName, dialect);const applyName = qualifiedByContext || schemaQualificationMode === "never" ? unqualifiedApplyName : defaultApplyName;const alias = autoAliasTables ? generateTableCompletionAlias(table.name, existingAliases) : "";@@ -4874,7 +4868,11 @@ function buildForeignKeyRelatedTableItems(// Mirror buildTableItems' dedupeKey so an FK candidate and the regular// candidate for the same schema-qualified table collapse to one entry.dedupeKey:- schemaQualificationMode === "never" && table.schema ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), dialect)}.${unqualifiedApplyName}` : ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema) ? applyName : undefined,+ schemaQualificationMode === "never" && table.schema+ ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase, dialect), dialect)}.${unqualifiedApplyName}`+ : ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema)+ ? applyName+ : undefined,};}).sort(compareCompletionItems);diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.tsindex f7fcee9fa9..07f172a390 100644--- a/packages/app-tests/sqlCompletion.test.ts+++ b/packages/app-tests/sqlCompletion.test.ts@@ -4724,6 +4724,95 @@ test("table completions preserve identifier case by default (#11400)", () => {assert.equal(userItem?.apply, "USERS", "should apply original table name");});+test("MySQL table completions preserve catalog case under identifierCase lower (#12938)", () => {+ const mysqlTables = [{ name: "SETTLE_MZ", schema: "tmp_fj", type: "table" as const }];+ const items = buildSqlCompletionItems("select * from s", "select * from s".length, {+ tables: mysqlTables,+ columnsByTable: new Map(),+ databaseType: "mysql",+ currentSchema: "tmp_fj",+ identifierCase: "lower",+ autoAliasTables: false,+ });+ const tableItem = items.find((item) => item.label === "SETTLE_MZ");+ assert.ok(tableItem, "should provide table item with original uppercase label");+ assert.equal(tableItem?.apply, "SETTLE_MZ", "should preserve original table casing in MySQL");+});++test("MySQL schema-qualified table completions preserve catalog case (#12938)", () => {+ const mysqlTables = [+ { name: "SETTLE_MZ", schema: "tmp_fj", type: "table" as const },+ { name: "SETTLE_MZ", schema: "other_schema", type: "table" as const },+ ];+ const items = buildSqlCompletionItems("select * from s", "select * from s".length, {+ tables: mysqlTables,+ columnsByTable: new Map(),+ databaseType: "mysql",+ currentSchema: "other_schema",+ identifierCase: "lower",+ autoAliasTables: false,+ });+ const settleItem = items.find((item) => item.apply === "tmp_fj.SETTLE_MZ");+ assert.ok(settleItem, "should qualify tmp_fj.SETTLE_MZ preserving original table case");+});++test("PostgreSQL mixed-case table completions preserve case and quotes under identifierCase (#12938)", () => {+ const pgTables = [{ name: "Users", schema: "public", type: "table" as const }];+ const itemsLower = buildSqlCompletionItems("select * from u", "select * from u".length, {+ tables: pgTables,+ columnsByTable: new Map(),+ databaseType: "postgres",+ currentSchema: "public",+ identifierCase: "lower",+ autoAliasTables: false,+ });+ const userItemLower = itemsLower.find((item) => item.label === "Users");+ assert.ok(userItemLower, "should keep original mixed-case label");+ assert.equal(userItemLower?.apply, '"Users"', "should keep double quotes and original case for mixed-case Postgres table");++ const itemsUpper = buildSqlCompletionItems("select * from u", "select * from u".length, {+ tables: pgTables,+ columnsByTable: new Map(),+ databaseType: "postgres",+ currentSchema: "public",+ identifierCase: "upper",+ autoAliasTables: false,+ });+ const userItemUpper = itemsUpper.find((item) => item.label === "Users");+ assert.ok(userItemUpper, "should keep original mixed-case label");+ assert.equal(userItemUpper?.apply, '"Users"', "should not emit unquoted users or quoted USERS for mixed-case Postgres table");+});++test("SQL Server table completions preserve catalog case under identifierCase (#12938)", () => {+ const sqlserverTables = [{ name: "SETTLE_MZ", schema: "dbo", type: "table" as const }];+ const items = buildSqlCompletionItems("select * from s", "select * from s".length, {+ tables: sqlserverTables,+ columnsByTable: new Map(),+ databaseType: "sqlserver",+ currentSchema: "dbo",+ identifierCase: "lower",+ autoAliasTables: false,+ });+ const tableItem = items.find((item) => item.label === "SETTLE_MZ");+ assert.ok(tableItem, "should keep original table label in SQL Server");+ assert.equal(tableItem?.apply, "SETTLE_MZ", "should preserve original table casing in SQL Server");+});++test("Oracle mixed-case table completions preserve case and quotes under identifierCase (#12938)", () => {+ const oracleTables = [{ name: "Users", schema: "SCOTT", type: "table" as const }];+ const items = buildSqlCompletionItems("select * from u", "select * from u".length, {+ tables: oracleTables,+ columnsByTable: new Map(),+ databaseType: "oracle",+ currentSchema: "SCOTT",+ identifierCase: "lower",+ autoAliasTables: false,+ });+ const userItem = items.find((item) => item.label === "Users");+ assert.ok(userItem, "should keep original mixed-case label in Oracle");+ assert.equal(userItem?.apply, '"Users"', "should keep double quotes and original case for mixed-case Oracle table");+});+test("schema-qualified table completions follow identifierCase (#11400)", () => {const collidingTables = [{ name: "ORDERS", schema: "SCOTT", type: "table" as const },