fix(navigation): use target db for cross-database table jump

This commit is contained in:
zipg authored and GitHub committed 2026-08-06 20:10:44 +08:00
1 parent a71c767174
commit 3e6b3d5321
3 files changed
+96 -5

No files matched your search

@@ -114,7 +114,7 @@ import { metadataSchemaForConnection, sqlSnippetDatabaseTypeForConnection } from
import { usesLocalOnlyEditorCompletionMetadata, usesOnDemandOnlyEditorColumnMetadata } from "@/lib/metadata/completionMetadataPolicy"; import { usesLocalOnlyEditorCompletionMetadata, usesOnDemandOnlyEditorColumnMetadata } from "@/lib/metadata/completionMetadataPolicy";
import { loadObjectDdl } from "@/lib/metadata/objectDdlCache"; import { loadObjectDdl } from "@/lib/metadata/objectDdlCache";
import { loadObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache"; import { loadObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache";
import { queryContextObjectActions, queryContextObjectRoute, queryTableCandidateAtSqlPosition, resolveQueryContextCandidateDatabase, resolveQueryContextObjectTarget, type QueryContextObjectAction } from "@/lib/sql/queryCursorTableTarget"; import { queryContextObjectActions, queryContextObjectRoute, queryTableCandidateAtSqlPosition, queryTableNavigationTargetAtSqlPosition, resolveQueryContextCandidateDatabase, resolveQueryContextObjectTarget, type QueryContextObjectAction } from "@/lib/sql/queryCursorTableTarget";
import * as api from "@/lib/backend/api"; import * as api from "@/lib/backend/api";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime"; import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { isMacOS } from "@/lib/backend/platform"; import { isMacOS } from "@/lib/backend/platform";
@@ -4512,6 +4512,18 @@ onMounted(async () => {
const objectSchemaHint = identity.parts.length >= 3 ? identity.schema : identity.parts.length === 1 ? props.schema : undefined; const objectSchemaHint = identity.parts.length >= 3 ? identity.schema : identity.parts.length === 1 ? props.schema : undefined;
const isRoutineCall = identity.role === "routine_call"; const isRoutineCall = identity.role === "routine_call";
const isRelationColumnList = identity.role === "relation_column_list"; const isRelationColumnList = identity.role === "relation_column_list";
const relationNavigationTarget = (target: SqlObjectNavigationTarget) =>
queryTableNavigationTargetAtSqlPosition(
{
connectionId: props.connectionId!,
database: props.database!,
schema: props.schema,
databaseType: props.databaseType,
sql: doc,
position: pos,
},
target,
);
// 1. Local table cache (sync). Relation column lists always prefer tables over routines. // 1. Local table cache (sync). Relation column lists always prefer tables over routines.
if (cachedTables.length === 0) { if (cachedTables.length === 0) {
@@ -4520,7 +4532,7 @@ onMounted(async () => {
let matchedTable = matchTable(identifier, cachedTables); let matchedTable = matchTable(identifier, cachedTables);
if (matchedTable) { if (matchedTable) {
emit("clickTable", sqlObjectNavigationTarget(matchedTable)); emit("clickTable", relationNavigationTarget(matchedTable));
return; return;
} }
@@ -4581,7 +4593,7 @@ onMounted(async () => {
cachedTables = await connectionStore.listCompletionTables(props.connectionId!, props.database!, tableLookupFilter, MAX_COMPLETION_TABLES, props.schema, false, props.schema, props.catalog); cachedTables = await connectionStore.listCompletionTables(props.connectionId!, props.database!, tableLookupFilter, MAX_COMPLETION_TABLES, props.schema, false, props.schema, props.catalog);
matchedTable = matchTable(identifier, cachedTables); matchedTable = matchTable(identifier, cachedTables);
if (matchedTable) { if (matchedTable) {
emit("clickTable", sqlObjectNavigationTarget(matchedTable)); emit("clickTable", relationNavigationTarget(matchedTable));
return; return;
} }
} else if (!usesLocalOnlyCompletionMetadata() && isRoutineCall) { } else if (!usesLocalOnlyCompletionMetadata() && isRoutineCall) {
@@ -4590,7 +4602,7 @@ onMounted(async () => {
cachedTables = await connectionStore.listCompletionTables(props.connectionId!, props.database!, tableLookupFilter, 20, props.schema, false, props.schema, props.catalog); cachedTables = await connectionStore.listCompletionTables(props.connectionId!, props.database!, tableLookupFilter, 20, props.schema, false, props.schema, props.catalog);
matchedTable = matchTable(identifier, cachedTables); matchedTable = matchTable(identifier, cachedTables);
if (matchedTable) { if (matchedTable) {
emit("clickTable", sqlObjectNavigationTarget(matchedTable)); emit("clickTable", relationNavigationTarget(matchedTable));
return; return;
} }
} }
@@ -4629,7 +4641,7 @@ onMounted(async () => {
const matchedRef = matchTable(identifier, referencedTables); const matchedRef = matchTable(identifier, referencedTables);
if (matchedRef) { if (matchedRef) {
emit("clickTable", sqlObjectNavigationTarget(matchedRef)); emit("clickTable", relationNavigationTarget(matchedRef));
return; return;
} }
const colName = identifierParts[identifierParts.length - 1] ?? identifier; const colName = identifierParts[identifierParts.length - 1] ?? identifier;
@@ -78,6 +78,20 @@ export function queryTableCandidateAtSqlPosition(input: QueryTableCandidateAtPos
return { connectionId: input.connectionId, database, schema, tableName }; return { connectionId: input.connectionId, database, schema, tableName };
} }
export function queryTableNavigationTargetAtSqlPosition(input: QueryTableCandidateAtPositionInput, target: SqlObjectNavigationTarget): SqlObjectNavigationTarget {
const parts = extractQualifiedIdentifierPartsAt(input.sql, input.position);
if (parts.length < 2) return sqlObjectNavigationTarget(target);
const candidate = queryTableCandidateAtSqlPosition(input);
if (!candidate) return sqlObjectNavigationTarget(target);
return sqlObjectNavigationTarget({
...target,
database: candidate.database,
schema: candidate.schema,
});
}
export function resolveQueryContextCandidateDatabase(candidate: QueryCursorTableCandidate, databases: readonly string[]): QueryCursorTableCandidate { export function resolveQueryContextCandidateDatabase(candidate: QueryCursorTableCandidate, databases: readonly string[]): QueryCursorTableCandidate {
const database = databases.find((name) => sameIdentifier(name, candidate.database)); const database = databases.find((name) => sameIdentifier(name, candidate.database));
return database && database !== candidate.database ? { ...candidate, database } : candidate; return database && database !== candidate.database ? { ...candidate, database } : candidate;
+65
View File
@@ -8,6 +8,7 @@ import {
queryContextTargetFromCandidate, queryContextTargetFromCandidate,
queryCursorTableCandidate, queryCursorTableCandidate,
queryTableCandidateAtSqlPosition, queryTableCandidateAtSqlPosition,
queryTableNavigationTargetAtSqlPosition,
resolveQueryContextCandidateDatabase, resolveQueryContextCandidateDatabase,
resolveQueryContextObjectTarget, resolveQueryContextObjectTarget,
} from "../../apps/desktop/src/lib/sql/queryCursorTableTarget.ts"; } from "../../apps/desktop/src/lib/sql/queryCursorTableTarget.ts";
@@ -87,6 +88,70 @@ test("builds database-qualified candidates for multi-database non-schema engines
}); });
}); });
test("maps qualified relation navigation to database or schema by dialect", () => {
const mysqlSql = "select * from promotion.p_settlement_account";
assert.deepEqual(
queryTableNavigationTargetAtSqlPosition(
{
connectionId: "conn-1",
database: "nova",
databaseType: "mysql",
sql: mysqlSql,
position: mysqlSql.indexOf("p_settlement_account"),
},
{ name: "p_settlement_account", schema: "promotion", type: "table" },
),
{
name: "p_settlement_account",
database: "promotion",
type: "table",
},
);
const postgresSql = "select * from reporting.orders";
assert.deepEqual(
queryTableNavigationTargetAtSqlPosition(
{
connectionId: "conn-1",
database: "app",
schema: "public",
databaseType: "postgres",
sql: postgresSql,
position: postgresSql.indexOf("orders"),
},
{ name: "orders", schema: "reporting", type: "view" },
),
{
name: "orders",
database: "app",
schema: "reporting",
type: "view",
},
);
});
test("keeps metadata scope for unqualified relation navigation", () => {
const sql = "select * from orders";
assert.deepEqual(
queryTableNavigationTargetAtSqlPosition(
{
connectionId: "conn-1",
database: "app",
schema: "public",
databaseType: "postgres",
sql,
position: sql.indexOf("orders"),
},
{ name: "Orders", schema: "archive", type: "table" },
),
{
name: "Orders",
schema: "archive",
type: "table",
},
);
});
test("builds three-part candidates at an explicit context-menu position", () => { test("builds three-part candidates at an explicit context-menu position", () => {
const sql = 'select * from "warehouse"."reporting"."Daily Sales"'; const sql = 'select * from "warehouse"."reporting"."Daily Sales"';