Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

feat(sql): respect formatter identifier casing in autocomplete

lxk95528b12df34017
diff --git a/apps/desktop/src/components/editor/useQueryEditorCompletion.ts b/apps/desktop/src/components/editor/useQueryEditorCompletion.ts
index 24cd2c4c1e..225e0ebdf0 100644
--- a/apps/desktop/src/components/editor/useQueryEditorCompletion.ts
+++ b/apps/desktop/src/components/editor/useQueryEditorCompletion.ts
@@ -844,6 +844,7 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
currentSchema: props.schema,
keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,
functionCase: settingsStore.editorSettings.sqlFormatter.functionCase,
+ identifierCase: settingsStore.editorSettings.sqlFormatter.identifierCase,
autoAliasTables: settingsStore.editorSettings.autoAliasTables,
tableCompletionSchemaQualification: settingsStore.editorSettings.tableCompletionSchemaQualification,
quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,
@@ -908,6 +909,7 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
currentSchema: props.schema,
keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,
functionCase: settingsStore.editorSettings.sqlFormatter.functionCase,
+ identifierCase: settingsStore.editorSettings.sqlFormatter.identifierCase,
autoAliasTables: settingsStore.editorSettings.autoAliasTables,
tableCompletionSchemaQualification: settingsStore.editorSettings.tableCompletionSchemaQualification,
quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,
@@ -1213,6 +1215,7 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
currentSchema: scope.schema,
keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,
functionCase: settingsStore.editorSettings.sqlFormatter.functionCase,
+ identifierCase: settingsStore.editorSettings.sqlFormatter.identifierCase,
autoAliasTables: settingsStore.editorSettings.autoAliasTables,
tableCompletionSchemaQualification: settingsStore.editorSettings.tableCompletionSchemaQualification,
quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,
@@ -1678,6 +1681,7 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
currentSchema: scope.schema,
keywordCase: settingsStore.editorSettings.sqlFormatter.keywordCase,
functionCase: settingsStore.editorSettings.sqlFormatter.functionCase,
+ identifierCase: settingsStore.editorSettings.sqlFormatter.identifierCase,
autoAliasTables: settingsStore.editorSettings.autoAliasTables,
tableCompletionSchemaQualification: settingsStore.editorSettings.tableCompletionSchemaQualification,
quoteIdentifiers: settingsStore.editorSettings.generateSqlQuoteIdentifiers,
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts
index a9fa3b8f74..d9f45a59d4 100644
--- a/apps/desktop/src/lib/sql/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts
@@ -1679,6 +1679,7 @@ export interface SqlCompletionProviderInput {
currentSchema?: string;
keywordCase?: SqlKeywordCase;
functionCase?: SqlKeywordCase;
+ identifierCase?: SqlKeywordCase;
autoAliasTables?: boolean;
tableCompletionSchemaQualification?: SqlTableCompletionSchemaQualification;
quoteIdentifiers?: boolean;
@@ -1701,6 +1702,7 @@ export function buildSqlCompletionItems(
currentSchema?: string;
keywordCase?: SqlKeywordCase;
functionCase?: SqlKeywordCase;
+ identifierCase?: SqlKeywordCase;
autoAliasTables?: boolean;
tableCompletionSchemaQualification?: SqlTableCompletionSchemaQualification;
quoteIdentifiers?: boolean;
@@ -1840,9 +1842,9 @@ class SqlCompletionProvider {
if (!context.exclusiveColumnSuggestions && context.suggestTables) {
const autoAliasTables = !!this.input.autoAliasTables && context.autoAliasTableCompletions && !context.tableCompletionTargetAliasUnsafe && supportsTableAliases(this.databaseType);
const schemaQualification = normalizeSqlTableCompletionSchemaQualification(this.input.tableCompletionSchemaQualification);
- this.items.push(...buildForeignKeyRelatedTableItems(context, completionTables, this.input.foreignKeysByTable, this.dialect, autoAliasTables, this.databaseType, this.input.currentSchema, schemaQualification));
- this.items.push(...buildReferencedTableItems(context, completionTables, this.dialect));
- this.items.push(...buildTableItems(context, completionTables, this.dialect, autoAliasTables, context.referencedTables, this.databaseType, this.input.currentSchema, schemaQualification));
+ this.items.push(...buildForeignKeyRelatedTableItems(context, completionTables, this.input.foreignKeysByTable, this.dialect, autoAliasTables, this.databaseType, this.input.currentSchema, schemaQualification, this.input.identifierCase));
+ this.items.push(...buildReferencedTableItems(context, completionTables, this.dialect, this.input.identifierCase));
+ this.items.push(...buildTableItems(context, completionTables, this.dialect, autoAliasTables, context.referencedTables, this.databaseType, this.input.currentSchema, schemaQualification, this.input.identifierCase));
if (this.databaseType === "clickhouse") {
this.items.push(...buildClickHouseFunctionItems(context.prefix, context.openingParenAfterCursor, "table", this.input.functionCompletionIncludeParams));
}
@@ -4174,6 +4176,43 @@ function quoteSelectStarColumnIdentifier(identifier: string, dialect?: SqlComple
return quoteSqlIdentifier(identifier, dialect);
}
+function applySqlIdentifierCase(value: string, identifierCase?: SqlKeywordCase): string {
+ if (!identifierCase || identifierCase === "preserve" || isQuotedIdentifier(value.trim())) return value;
+ if (identifierCase === "lower") return value.toLowerCase();
+ if (identifierCase === "upper") return value.toUpperCase();
+ return 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);
+}
+
+function applySqlIdentifierCaseToName(name: string, identifierCase?: SqlKeywordCase, dialect?: SqlCompletionApplyDialect): string {
+ if (!identifierCase || identifierCase === "preserve") return name;
+ const parts = splitQualifiedNameRawParts(name);
+ if (parts.length === 0) return name;
+ return parts
+ .map((part) => {
+ if (isQuotedIdentifier(part.trim())) return part;
+ const cased = applySqlIdentifierCase(part, identifierCase);
+ return quoteCasedIdentifier(part, cased, dialect);
+ })
+ .join(".");
+}
+
/**
* Build a normalized table-name -> set-of-schemas index used to detect when a
* bare table name is ambiguous across schemas. Shared by buildTableItems and
@@ -4208,6 +4247,7 @@ function resolveTableSchemaQualification(
currentSchema: string | undefined,
schemasByTableName: Map<string, Set<string>>,
schemaQualificationMode: SqlTableCompletionSchemaQualification,
+ identifierCase?: SqlKeywordCase,
): { ambiguousTableName: boolean; schemaQualification: boolean; defaultApplyName: string } {
const oracleSchemaQualification = isOracleCompletionDatabase(databaseType) && table.schema && table.schema.toUpperCase() !== "PUBLIC" && (!currentSchema || normalizeIdentifierPart(table.schema) !== normalizeIdentifierPart(currentSchema));
// A bare table name is ambiguous when metadata contains the same name in multiple schemas.
@@ -4215,7 +4255,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 defaultApplyName = schemaQualification ? `${quoteCompletionApplyIdentifier(table.schema!, dialect)}.${quoteCompletionApplyIdentifier(table.name, dialect)}` : quoteCompletionApplyIdentifier(table.name, dialect);
+ const tableName = applySqlIdentifierCase(table.name, identifierCase);
+ const quotedTable = quoteCasedIdentifier(table.name, tableName, dialect);
+ const defaultApplyName = schemaQualification && table.schema ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), dialect)}.${quotedTable}` : quotedTable;
return { ambiguousTableName, schemaQualification, defaultApplyName };
}
@@ -4232,7 +4274,7 @@ function resolveTableSchemaQualification(
* actually selects from, while a catalog table is only a name that matched
* (the catalog path scores initials and subsequence matches up to 2400).
*/
-function buildReferencedTableItems(context: SqlCompletionContext, completionTables: SqlCompletionTable[], dialect?: SqlCompletionApplyDialect): SqlCompletionItem[] {
+function buildReferencedTableItems(context: SqlCompletionContext, completionTables: SqlCompletionTable[], dialect?: SqlCompletionApplyDialect, identifierCase?: SqlKeywordCase): SqlCompletionItem[] {
const knownTables = new Set(completionTables.map((table) => normalizeIdentifierPart(table.name)));
const seen = new Set<string>();
const items: SqlCompletionItem[] = [];
@@ -4245,11 +4287,13 @@ function buildReferencedTableItems(context: SqlCompletionContext, completionTabl
const key = normalizeIdentifierPart(reference.name);
if (!key || knownTables.has(key) || seen.has(key)) continue;
seen.add(key);
+ const tableName = applySqlIdentifierCase(reference.name, identifierCase);
+ const applyName = quoteCasedIdentifier(reference.name, tableName, dialect);
items.push({
- label: reference.name,
+ label: tableName,
type: "table",
- detail: reference.schema ? `${reference.schema}.${reference.name}` : undefined,
- apply: quoteCompletionApplyIdentifier(reference.name, dialect),
+ detail: reference.schema ? `${reference.schema}.${tableName}` : undefined,
+ apply: applyName,
boost: computeBoost(reference.name, context.prefix) + 5_000,
});
}
@@ -4265,6 +4309,7 @@ function buildTableItems(
databaseType?: DatabaseType,
currentSchema?: string,
schemaQualificationMode: SqlTableCompletionSchemaQualification = "collision",
+ identifierCase?: SqlKeywordCase,
): SqlCompletionItem[] {
const { prefix } = context;
const qualifierSchema = context.qualifierParts?.[context.qualifierParts.length - 1] ?? context.qualifier?.split(".").filter(Boolean).pop();
@@ -4275,10 +4320,12 @@ function buildTableItems(
return matchingTables
.map((table) => {
const qualifiedByContext = !!qualifierSchema && !!table.schema && normalizeIdentifierPart(qualifierSchema) === normalizeIdentifierPart(table.schema);
- const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode);
- const unqualifiedApplyName = quoteCompletionApplyIdentifier(table.name, dialect);
+ const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode, identifierCase);
+ const tableName = applySqlIdentifierCase(table.name, identifierCase);
+ const unqualifiedApplyName = quoteCasedIdentifier(table.name, tableName, dialect);
const rawSuppliedApplyName = table.applyName?.trim();
const suppliedApplyName = dialect === "sqlserver" && rawSuppliedApplyName ? quoteCompletionApplyName(rawSuppliedApplyName, dialect) : rawSuppliedApplyName;
+ const casedSuppliedApplyName = suppliedApplyName ? applySqlIdentifierCaseToName(suppliedApplyName, identifierCase, dialect) : undefined;
const suppliedApplyNameIsQualified = suppliedApplyName?.includes(".") === true;
const applyName =
qualifiedByContext || schemaQualificationMode === "never"
@@ -4287,17 +4334,22 @@ function buildTableItems(
? defaultApplyName
: ambiguousTableName && table.schema && (!suppliedApplyName || !suppliedApplyNameIsQualified)
? defaultApplyName
- : (suppliedApplyName ?? defaultApplyName);
+ : (casedSuppliedApplyName ?? defaultApplyName);
const alias = autoAliasTables ? generateTableCompletionAlias(table.name, existingAliases) : "";
const schemaDetail = ambiguousTableName && table.schema ? `${table.schema}.${table.name}` : undefined;
const detail = table.detail && schemaDetail ? `${schemaDetail} ${table.detail}` : (table.detail ?? schemaDetail ?? (table.type === "table" ? undefined : table.type));
return {
- label: table.name,
+ label: tableName,
type: "table" as const,
detail,
apply: formatTableAliasApply(applyName, alias),
boost: computeBoost(table.name, prefix) + 1000 + (table.boost ?? 0),
- dedupeKey: schemaQualificationMode === "never" && table.schema ? `${quoteCompletionApplyIdentifier(table.schema, dialect)}.${unqualifiedApplyName}` : table.applyName || ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema) ? applyName : undefined,
+ dedupeKey:
+ schemaQualificationMode === "never" && table.schema
+ ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), dialect)}.${unqualifiedApplyName}`
+ : table.applyName || ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema)
+ ? applyName
+ : undefined,
};
})
.sort(compareCompletionItems)
@@ -4313,6 +4365,7 @@ function buildForeignKeyRelatedTableItems(
databaseType?: DatabaseType,
currentSchema?: string,
schemaQualificationMode: SqlTableCompletionSchemaQualification = "collision",
+ identifierCase?: SqlKeywordCase,
): SqlCompletionItem[] {
if (!foreignKeysByTable || context.referencedTables.length === 0) return [];
const candidates = new Map<string, { table: SqlCompletionTable; detail: string }>();
@@ -4345,19 +4398,21 @@ function buildForeignKeyRelatedTableItems(
return [...candidates.values()]
.map(({ table, detail }) => {
const qualifiedByContext = !!qualifierSchema && !!table.schema && normalizeIdentifierPart(qualifierSchema) === normalizeIdentifierPart(table.schema);
- const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode);
- const unqualifiedApplyName = quoteCompletionApplyIdentifier(table.name, dialect);
+ const { ambiguousTableName, defaultApplyName } = resolveTableSchemaQualification(table, dialect, databaseType, currentSchema, schemasByTableName, schemaQualificationMode, identifierCase);
+ const tableName = applySqlIdentifierCase(table.name, identifierCase);
+ const unqualifiedApplyName = quoteCasedIdentifier(table.name, tableName, dialect);
const applyName = qualifiedByContext || schemaQualificationMode === "never" ? unqualifiedApplyName : defaultApplyName;
const alias = autoAliasTables ? generateTableCompletionAlias(table.name, existingAliases) : "";
return {
- label: table.name,
+ label: tableName,
type: "table" as const,
detail,
apply: formatTableAliasApply(applyName, alias),
boost: computeBoost(table.name, context.prefix) + 3600,
// 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 ? `${quoteCompletionApplyIdentifier(table.schema, dialect)}.${unqualifiedApplyName}` : ambiguousTableName || (isOracleCompletionDatabase(databaseType) && table.schema) ? applyName : undefined,
+ dedupeKey:
+ schemaQualificationMode === "never" && table.schema ? `${quoteCasedIdentifier(table.schema, applySqlIdentifierCase(table.schema, identifierCase), 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.ts
index c1b9aab69e..42f9a9d065 100644
--- a/packages/app-tests/sqlCompletion.test.ts
+++ b/packages/app-tests/sqlCompletion.test.ts
@@ -4657,3 +4657,101 @@ test("recordCompletionSelection boosts future ranking", () => {
// user_profiles should now rank higher than users due to history boost
assert.equal(tableItems[0]?.label, "user_profiles");
});
+
+// --- Table completion identifierCase (#11400) ---
+
+test("table completions follow identifierCase lower (#11400)", () => {
+ const oracleTables = [
+ { name: "USERS", schema: "SCOTT", type: "table" as const },
+ { name: "DEPARTMENT", 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: true,
+ });
+ const userItem = items.find((item) => item.label === "users");
+ assert.ok(userItem, "should provide lowercase table label");
+ assert.equal(userItem?.apply, "users us", "should apply bare lowercase table name and alias in Oracle");
+});
+
+test("table completions follow identifierCase upper (#11400)", () => {
+ const pgTables = [{ name: "users", schema: "public", type: "table" as const }];
+ const items = buildSqlCompletionItems("select * from u", "select * from u".length, {
+ tables: pgTables,
+ columnsByTable: new Map(),
+ databaseType: "postgres",
+ currentSchema: "public",
+ identifierCase: "upper",
+ autoAliasTables: true,
+ });
+ const userItem = items.find((item) => item.label === "USERS");
+ assert.ok(userItem, "should provide uppercase table label");
+ assert.equal(userItem?.apply, "USERS us", "should apply bare uppercase table name and alias");
+});
+
+test("table completions preserve identifier case by default (#11400)", () => {
+ 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: "preserve",
+ autoAliasTables: false,
+ });
+ const userItem = items.find((item) => item.label === "USERS");
+ assert.ok(userItem, "should keep original uppercase label");
+ assert.equal(userItem?.apply, "USERS", "should apply original table name");
+});
+
+test("schema-qualified table completions follow identifierCase (#11400)", () => {
+ const collidingTables = [
+ { name: "ORDERS", schema: "SCOTT", type: "table" as const },
+ { name: "ORDERS", schema: "STAGING", type: "table" as const },
+ ];
+ const items = buildSqlCompletionItems("select * from o", "select * from o".length, {
+ tables: collidingTables,
+ columnsByTable: new Map(),
+ databaseType: "oracle",
+ currentSchema: "OTHER",
+ identifierCase: "lower",
+ autoAliasTables: false,
+ });
+ const scottOrder = items.find((item) => item.apply === "scott.orders");
+ const stagingOrder = items.find((item) => item.apply === "staging.orders");
+ assert.ok(scottOrder, "should qualify scott.orders in lowercase");
+ assert.ok(stagingOrder, "should qualify staging.orders in lowercase");
+});
+
+test("table completions with applyName follow identifierCase (#11400)", () => {
+ const tablesWithApplyName = [
+ { name: "DEPT_DICT", schema: "COMM", applyName: "COMM.DEPT_DICT", type: "table" as const },
+ ];
+ const items = buildSqlCompletionItems("select * from d", "select * from d".length, {
+ tables: tablesWithApplyName,
+ columnsByTable: new Map(),
+ databaseType: "oracle",
+ identifierCase: "lower",
+ });
+ const deptItem = items.find((item) => item.label === "dept_dict");
+ assert.ok(deptItem, "should provide lowercase table label");
+ assert.equal(deptItem?.apply, "comm.dept_dict", "should lowercase supplied applyName");
+});
+
+test("CTE table completions follow identifierCase (#11400)", () => {
+ const sql = "with MY_CTE as (select 1 from dual) select * from m";
+ const items = buildSqlCompletionItems(sql, sql.length, {
+ tables: [],
+ columnsByTable: new Map(),
+ databaseType: "oracle",
+ identifierCase: "lower",
+ });
+ const cteItem = items.find((item) => item.label === "my_cte");
+ assert.ok(cteItem, "should provide lowercase CTE table label");
+ assert.equal(cteItem?.apply, "my_cte", "should apply lowercase CTE name");
+});
+