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。

fix(editor): prefer the current schema when resolving right-click objects (#13018)

dienasoe702666c5230
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index da6fe144d1..f74c2c275e 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -972,7 +972,8 @@ function syncContextMenuStateAtEvent(currentView: EditorViewType, event: MouseEv
// Right-click must stay instant: resolve from completion/tree caches and keep the legacy table fallback when metadata is unavailable.
const candidate = resolveQueryContextCandidateDatabase(parsedCandidate, connectionStore.lookupLocalCompletionDatabases(parsedCandidate.connectionId, parsedCandidate.database, MAX_COMPLETION_TABLES));
const tables = connectionStore.lookupLocalCompletionTables(candidate.connectionId, candidate.database, candidate.tableName, MAX_COMPLETION_TABLES, candidate.schema, props.catalog);
- contextObjectTarget.value = resolveQueryContextObjectTarget(candidate, tables);
+ const preferredSchema = props.schema ?? connectionStore.getConfig(props.connectionId)?.default_schema;
+ contextObjectTarget.value = resolveQueryContextObjectTarget(candidate, tables, preferredSchema);
}
function focusEditor() {
diff --git a/apps/desktop/src/lib/__tests__/sql/queryCursorTableTarget.spec.ts b/apps/desktop/src/lib/__tests__/sql/queryCursorTableTarget.spec.ts
new file mode 100644
index 0000000000..fe54dbb3de
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/sql/queryCursorTableTarget.spec.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+import { queryContextObjectActions, resolveQueryContextObjectTarget } from "@/lib/sql/queryCursorTableTarget";
+import type { SqlCompletionTable } from "@/lib/sql/sqlCompletion";
+import type { SqlObjectNavigationType } from "@/lib/sql/sqlNavigation";
+
+function candidate(overrides: Partial<{ schema: string }> = {}) {
+ return { connectionId: "conn-1", database: "APP", tableName: "ORDERS", ...overrides };
+}
+
+function table(name: string, schema: string | undefined, type: SqlObjectNavigationType): SqlCompletionTable {
+ return { name, schema, type };
+}
+
+describe("resolveQueryContextObjectTarget", () => {
+ it("prefers the query tab schema over a same-named table in another schema", () => {
+ // #12865: without a schema in the SQL, the first cache entry used to win and
+ // a view in the current schema could be resolved as a table elsewhere.
+ const target = resolveQueryContextObjectTarget(candidate(), [table("ORDERS", "OTHER", "table"), table("ORDERS", "APP", "view")], "APP");
+
+ expect(target.type).toBe("view");
+ expect(target.schema).toBe("APP");
+ });
+
+ it("keeps the first match when no preferred schema is available", () => {
+ const target = resolveQueryContextObjectTarget(candidate(), [table("ORDERS", "OTHER", "table"), table("ORDERS", "APP", "view")]);
+
+ expect(target.type).toBe("table");
+ expect(target.schema).toBe("OTHER");
+ });
+
+ it("keeps an explicit schema in the SQL above the preferred schema", () => {
+ const target = resolveQueryContextObjectTarget(candidate({ schema: "OTHER" }), [table("ORDERS", "APP", "view"), table("ORDERS", "OTHER", "table")], "APP");
+
+ expect(target.type).toBe("table");
+ expect(target.schema).toBe("OTHER");
+ });
+
+ it("falls back to a schema-less match and then the first match", () => {
+ const schemaLess = resolveQueryContextObjectTarget(candidate(), [table("ORDERS", "OTHER", "table"), table("ORDERS", undefined, "view")], "APP");
+ expect(schemaLess.type).toBe("view");
+ expect(schemaLess.schema).toBeUndefined();
+
+ const first = resolveQueryContextObjectTarget(candidate(), [table("ORDERS", "OTHER", "table")], "APP");
+ expect(first.type).toBe("table");
+ expect(first.schema).toBe("OTHER");
+ });
+
+ it("exposes view actions for a view target", () => {
+ expect(queryContextObjectActions("view")).toEqual(expect.arrayContaining(["edit-view", "view-source", "view-ddl"]));
+ expect(queryContextObjectActions("table")).not.toContain("edit-view");
+ });
+});
diff --git a/apps/desktop/src/lib/sql/queryCursorTableTarget.ts b/apps/desktop/src/lib/sql/queryCursorTableTarget.ts
index db06dd889f..be007b37cd 100644
--- a/apps/desktop/src/lib/sql/queryCursorTableTarget.ts
+++ b/apps/desktop/src/lib/sql/queryCursorTableTarget.ts
@@ -100,9 +100,18 @@ export function resolveQueryContextCandidateDatabase(candidate: QueryCursorTable
return database && database !== candidate.database ? { ...candidate, database } : candidate;
}
-export function resolveQueryContextObjectTarget(candidate: QueryCursorTableCandidate, tables: readonly SqlCompletionTable[]): SqlObjectNavigationTarget {
+export function resolveQueryContextObjectTarget(candidate: QueryCursorTableCandidate, tables: readonly SqlCompletionTable[], preferredSchema?: string): SqlObjectNavigationTarget {
const nameMatches = tables.filter((table) => sameIdentifier(table.name, candidate.tableName));
- const match = candidate.schema ? (nameMatches.find((table) => sameIdentifier(table.schema, candidate.schema)) ?? nameMatches.find((table) => !table.schema)) : nameMatches[0];
+ // An unqualified name is ambiguous across schemas. When the SQL did not name a
+ // schema, prefer the query tab's schema (or the connection default) instead of
+ // the first cache entry: otherwise a view in the current schema can be resolved
+ // as a same-named table in another schema (#12865). An explicit schema in the
+ // SQL still wins.
+ const match = candidate.schema
+ ? (nameMatches.find((table) => sameIdentifier(table.schema, candidate.schema)) ?? nameMatches.find((table) => !table.schema))
+ : preferredSchema
+ ? (nameMatches.find((table) => sameIdentifier(table.schema, preferredSchema)) ?? nameMatches.find((table) => !table.schema) ?? nameMatches[0])
+ : nameMatches[0];
return sqlObjectNavigationTarget({
name: match?.name ?? candidate.tableName,
database: candidate.database,