From 99d603fe15595d7fe0c0cc86e911f6bef4bcd648 Mon Sep 17 00:00:00 2001 From: ordinary being Date: Sun, 4 Oct 2026 22:17:40 +0800 Subject: [PATCH] fix(db2): resolve the login schema for unqualified completion columns --- .../connectionStore.completion.spec.ts | 39 +++++++++++++++++++ apps/desktop/src/stores/connectionStore.ts | 13 ++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/stores/__tests__/connectionStore.completion.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.completion.spec.ts index 2de4564c70..71c95d60b4 100644 --- a/apps/desktop/src/stores/__tests__/connectionStore.completion.spec.ts +++ b/apps/desktop/src/stores/__tests__/connectionStore.completion.spec.ts @@ -132,6 +132,18 @@ function damengConnection(): ConnectionConfig { } as ConnectionConfig; } +function db2Connection(): ConnectionConfig { + return { + ...postgresConnection(), + id: "db2-1", + name: "DB2", + db_type: "db2", + port: 50000, + username: "DBX_TEST", + database: "", + } as ConnectionConfig; +} + function dorisConnection(): ConnectionConfig { return { ...postgresConnection(), @@ -926,6 +938,33 @@ describe("connectionStore completion assistant", () => { expect(cached).toEqual(first); }); + it("uses the DB2 login schema for unqualified column completion", async () => { + // DB2's CURRENT SCHEMA starts as the authorization ID of the session user, so an + // unqualified reference resolves against the login schema — the same convention + // as Dameng (see the test above). Without the fallback `listCompletionColumns` + // takes its schema-required early return and never asks for the columns at all, + // so a tab that has no schema selected gets an empty candidate list. + const completionAssistantSearch = vi.fn().mockRejectedValue(new Error("assistant unavailable")); + const getColumns = vi.fn().mockResolvedValue([{ name: "PMNUM", data_type: "VARCHAR", is_nullable: false, column_default: null, is_primary_key: false, extra: null, comment: null }]); + + vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false })); + vi.doMock("@/lib/backend/api", () => ({ + checkConnectionHealth: vi.fn().mockResolvedValue(undefined), + completionAssistantSearch, + getColumns, + })); + + const { useConnectionStore } = await import("@/stores/connectionStore"); + const store = useConnectionStore(); + store.connections = [db2Connection()]; + store.connectedIds.add("db2-1"); + + const first = await store.listCompletionColumns("db2-1", "", "pm"); + + expect(getColumns).toHaveBeenCalledWith("db2-1", "", "DBX_TEST", "pm", undefined, undefined); + expect(first).toEqual([expect.objectContaining({ name: "PMNUM", table: "pm", schema: "DBX_TEST" })]); + }); + it("rejects assistant columns returned for a different MySQL parent table", async () => { const completionAssistantSearch = vi.fn().mockResolvedValue({ candidates: [ diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts index 205d36f023..3b14a80ba5 100644 --- a/apps/desktop/src/stores/connectionStore.ts +++ b/apps/desktop/src/stores/connectionStore.ts @@ -9223,6 +9223,16 @@ export const useConnectionStore = defineStore("connection", () => { return deduped; } + /// Engines whose CURRENT SCHEMA defaults to the login identity. An unqualified + /// reference resolves against the login schema there, so `listCompletionColumns` + /// falls back to the username instead of letting its schema-required early return + /// discard the lookup. A query tab hits that state when it never picked a schema + /// (`jdbcDialect.ts` records a new query tab or a reopened `.sql` file as the + /// cases). Dameng was the first engine fixed for this (#8301); DB2's CURRENT + /// SCHEMA is documented as the authorization ID of the session user, so it needs + /// the same fallback. + const LOGIN_SCHEMA_COMPLETION_TYPES = new Set(["dameng", "db2"]); + async function listCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number; tableQuoted?: boolean; schemaQuoted?: boolean }, catalog?: string): Promise { const config = getConfig(connectionId); // Use the effective database type (e.g. a JDBC connection whose URL is @@ -9236,7 +9246,8 @@ export const useConnectionStore = defineStore("connection", () => { const uppercaseUnquotedIdentifier = oracleIdentifier || effectiveDbType === "saphana"; const completionTable = uppercaseUnquotedIdentifier && context?.tableQuoted === false ? table.toUpperCase() : table; const normalizedSchema = schema?.trim(); - const rawCompletionSchema = effectiveDbType === "spanner" ? normalizedSchema : normalizedSchema || (effectiveDbType === "dameng" ? config?.username?.trim() || undefined : undefined); + const loginSchema = effectiveDbType && LOGIN_SCHEMA_COMPLETION_TYPES.has(effectiveDbType) ? config?.username?.trim() || undefined : undefined; + const rawCompletionSchema = effectiveDbType === "spanner" ? normalizedSchema : normalizedSchema || loginSchema; const completionSchema = uppercaseUnquotedIdentifier && rawCompletionSchema && context?.schemaQuoted === false ? rawCompletionSchema.toUpperCase() : rawCompletionSchema; const usesCurrentSchema = usesOracleCurrentSchemaCompletion(effectiveDbType, completionSchema); const hasCompletionSchema = completionSchema != null && (completionSchema !== "" || effectiveDbType === "spanner");