fix(db2): resolve the login schema for unqualified completion columns

This commit is contained in:
ordinary being authored and GitHub committed 2026-10-04 22:17:40 +08:00
1 parent 87e3c0634a
commit 99d603fe15
2 files changed
+51 -1

No files matched your search

@@ -132,6 +132,18 @@ function damengConnection(): ConnectionConfig {
} as 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 { function dorisConnection(): ConnectionConfig {
return { return {
...postgresConnection(), ...postgresConnection(),
@@ -926,6 +938,33 @@ describe("connectionStore completion assistant", () => {
expect(cached).toEqual(first); 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 () => { it("rejects assistant columns returned for a different MySQL parent table", async () => {
const completionAssistantSearch = vi.fn().mockResolvedValue({ const completionAssistantSearch = vi.fn().mockResolvedValue({
candidates: [ candidates: [
+12 -1
View File
@@ -9223,6 +9223,16 @@ export const useConnectionStore = defineStore("connection", () => {
return deduped; 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<SqlCompletionColumn[]> { async function listCompletionColumns(connectionId: string, database: string, table: string, schema?: string, context?: { clientSessionId?: string; version?: number; tableQuoted?: boolean; schemaQuoted?: boolean }, catalog?: string): Promise<SqlCompletionColumn[]> {
const config = getConfig(connectionId); const config = getConfig(connectionId);
// Use the effective database type (e.g. a JDBC connection whose URL is // 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 uppercaseUnquotedIdentifier = oracleIdentifier || effectiveDbType === "saphana";
const completionTable = uppercaseUnquotedIdentifier && context?.tableQuoted === false ? table.toUpperCase() : table; const completionTable = uppercaseUnquotedIdentifier && context?.tableQuoted === false ? table.toUpperCase() : table;
const normalizedSchema = schema?.trim(); 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 completionSchema = uppercaseUnquotedIdentifier && rawCompletionSchema && context?.schemaQuoted === false ? rawCompletionSchema.toUpperCase() : rawCompletionSchema;
const usesCurrentSchema = usesOracleCurrentSchemaCompletion(effectiveDbType, completionSchema); const usesCurrentSchema = usesOracleCurrentSchemaCompletion(effectiveDbType, completionSchema);
const hasCompletionSchema = completionSchema != null && (completionSchema !== "" || effectiveDbType === "spanner"); const hasCompletionSchema = completionSchema != null && (completionSchema !== "" || effectiveDbType === "spanner");