feat(editor): resolve table hover across schemas in current database
This commit is contained in:
1 parent
90ad4e62e6
commit
254b9baaff
21 files changed
+675
-29
No files matched your search
@@ -0,0 +1,261 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { matchHoverTableCandidates, normalizeTableHoverLookupMode, pickHoverTableMatch, resolveHoverTableLookupTarget } from "@/lib/editor/hoverTableLookup";
|
||||
|
||||
describe("normalizeTableHoverLookupMode", () => {
|
||||
it("defaults to fallback", () => {
|
||||
expect(normalizeTableHoverLookupMode(undefined)).toBe("fallback");
|
||||
expect(normalizeTableHoverLookupMode("invalid")).toBe("fallback");
|
||||
});
|
||||
|
||||
it("preserves valid modes", () => {
|
||||
expect(normalizeTableHoverLookupMode("current")).toBe("current");
|
||||
expect(normalizeTableHoverLookupMode("fallback")).toBe("fallback");
|
||||
expect(normalizeTableHoverLookupMode("always")).toBe("always");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveHoverTableLookupTarget", () => {
|
||||
it("uses schema.table qualifier regardless of mode", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["other", "users"],
|
||||
mode: "current",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
database: "app",
|
||||
schema: "other",
|
||||
tableName: "users",
|
||||
schemaFromQualifier: true,
|
||||
allowGlobalFallback: false,
|
||||
preferGlobalFirst: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("uses db.schema.table qualifier", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["analytics", "reporting", "users"],
|
||||
mode: "fallback",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
database: "analytics",
|
||||
schema: "reporting",
|
||||
tableName: "users",
|
||||
schemaFromQualifier: true,
|
||||
allowGlobalFallback: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("treats MySQL-style db.table as database qualifier", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
databaseType: "mysql",
|
||||
tableName: "users",
|
||||
identifierParts: ["other_db", "users"],
|
||||
mode: "fallback",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
database: "other_db",
|
||||
schema: undefined,
|
||||
schemaFromQualifier: true,
|
||||
allowGlobalFallback: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("prefers semantic schema over identifier parts", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["t"],
|
||||
semanticSchema: "reporting",
|
||||
mode: "current",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
schema: "reporting",
|
||||
schemaFromQualifier: true,
|
||||
allowGlobalFallback: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("scopes unqualified names to the selected schema in current mode", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["users"],
|
||||
mode: "current",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
schema: "public",
|
||||
schemaFromQualifier: false,
|
||||
allowGlobalFallback: false,
|
||||
preferGlobalFirst: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("allows global fallback for unqualified names in fallback mode", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["users"],
|
||||
mode: "fallback",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
schema: "public",
|
||||
allowGlobalFallback: true,
|
||||
preferGlobalFirst: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("prefers global search first in always mode", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["users"],
|
||||
mode: "always",
|
||||
});
|
||||
expect(target).toMatchObject({
|
||||
schema: undefined,
|
||||
allowGlobalFallback: true,
|
||||
preferGlobalFirst: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("pickHoverTableMatch", () => {
|
||||
const tables = [
|
||||
{ name: "users", schema: "reporting" },
|
||||
{ name: "users", schema: "public" },
|
||||
{ name: "orders", schema: "public" },
|
||||
];
|
||||
|
||||
it("prefers the selected schema when multiple schemas match", () => {
|
||||
expect(pickHoverTableMatch("users", tables, "public")).toEqual({ name: "users", schema: "public" });
|
||||
});
|
||||
|
||||
it("falls back to the first name match when preferred schema is absent", () => {
|
||||
expect(pickHoverTableMatch("users", tables, "missing")).toEqual({ name: "users", schema: "reporting" });
|
||||
expect(pickHoverTableMatch("users", tables)).toEqual({ name: "users", schema: "reporting" });
|
||||
});
|
||||
|
||||
it("returns null when the table name is missing", () => {
|
||||
expect(pickHoverTableMatch("missing", tables, "public")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("matchHoverTableCandidates", () => {
|
||||
const tables = [
|
||||
{ name: "users", schema: "reporting" },
|
||||
{ name: "users", schema: "public" },
|
||||
{ name: "orders", schema: "sales" },
|
||||
];
|
||||
|
||||
it("prefers the toolbar schema for unqualified names even when another schema appears first", () => {
|
||||
expect(
|
||||
matchHoverTableCandidates(tables, {
|
||||
lookups: ["users"],
|
||||
tableName: "users",
|
||||
preferredSchema: "public",
|
||||
}),
|
||||
).toEqual({ name: "users", schema: "public" });
|
||||
});
|
||||
|
||||
it("resolves qualified schema.table without using preferredSchema", () => {
|
||||
expect(
|
||||
matchHoverTableCandidates(tables, {
|
||||
lookups: ["reporting.users"],
|
||||
tableName: "users",
|
||||
preferredSchema: "public",
|
||||
}),
|
||||
).toEqual({ name: "users", schema: "reporting" });
|
||||
});
|
||||
|
||||
it("uses semantic qualified lookup before unqualified preferred-schema match", () => {
|
||||
expect(
|
||||
matchHoverTableCandidates(tables, {
|
||||
lookups: ["sales.orders", "orders"],
|
||||
tableName: "orders",
|
||||
preferredSchema: "public",
|
||||
}),
|
||||
).toEqual({ name: "orders", schema: "sales" });
|
||||
});
|
||||
|
||||
it("ignores unqualified lookups in the lookups list and still prefers schema", () => {
|
||||
// Unqualified entries must not short-circuit via matchTable's first-name-wins behavior.
|
||||
expect(
|
||||
matchHoverTableCandidates(tables, {
|
||||
lookups: ["users", "public.users"],
|
||||
tableName: "users",
|
||||
preferredSchema: "public",
|
||||
}),
|
||||
).toEqual({ name: "users", schema: "public" });
|
||||
});
|
||||
|
||||
it("returns null when nothing matches", () => {
|
||||
expect(
|
||||
matchHoverTableCandidates(tables, {
|
||||
lookups: ["missing.table"],
|
||||
tableName: "missing",
|
||||
preferredSchema: "public",
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveHoverTableLookupTarget cache-scope modes", () => {
|
||||
it("fallback keeps selected schema for the first local pass", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["users"],
|
||||
mode: "fallback",
|
||||
});
|
||||
expect(target.schema).toBe("public");
|
||||
expect(target.preferGlobalFirst).toBe(false);
|
||||
expect(target.allowGlobalFallback).toBe(true);
|
||||
});
|
||||
|
||||
it("always clears schema so local lookup scans same-database schemas", () => {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["users"],
|
||||
mode: "always",
|
||||
});
|
||||
expect(target.schema).toBeUndefined();
|
||||
expect(target.preferGlobalFirst).toBe(true);
|
||||
});
|
||||
|
||||
it("qualified names never enable same-database global fallback", () => {
|
||||
for (const mode of ["current", "fallback", "always"] as const) {
|
||||
const target = resolveHoverTableLookupTarget({
|
||||
database: "app",
|
||||
schema: "public",
|
||||
databaseType: "postgres",
|
||||
tableName: "users",
|
||||
identifierParts: ["other", "users"],
|
||||
mode,
|
||||
});
|
||||
expect(target.allowGlobalFallback).toBe(false);
|
||||
expect(target.preferGlobalFirst).toBe(false);
|
||||
expect(target.schema).toBe("other");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,130 @@
|
||||
import { isSchemaAware, isSingleDatabase } from "@/lib/database/databaseFeatureSupport";
|
||||
import { matchTable, splitQualifiedIdentifier } from "@/lib/sql/sqlNavigation";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
|
||||
export const TABLE_HOVER_LOOKUP_MODES = ["current", "fallback", "always"] as const;
|
||||
export type TableHoverLookupMode = (typeof TABLE_HOVER_LOOKUP_MODES)[number];
|
||||
|
||||
export function normalizeTableHoverLookupMode(value: unknown, fallback: TableHoverLookupMode = "fallback"): TableHoverLookupMode {
|
||||
return value === "current" || value === "fallback" || value === "always" ? value : fallback;
|
||||
}
|
||||
|
||||
export interface ResolveHoverTableLookupTargetInput {
|
||||
database: string;
|
||||
/** Currently selected schema from the editor toolbar. */
|
||||
schema?: string;
|
||||
catalog?: string;
|
||||
databaseType?: DatabaseType;
|
||||
tableName: string;
|
||||
/** Identifier segments from the hovered/clicked token (e.g. `["other", "users"]`). */
|
||||
identifierParts: string[];
|
||||
semanticDatabase?: string;
|
||||
semanticSchema?: string;
|
||||
mode: TableHoverLookupMode;
|
||||
}
|
||||
|
||||
export interface HoverTableLookupTarget {
|
||||
database: string;
|
||||
schema?: string;
|
||||
catalog?: string;
|
||||
tableName: string;
|
||||
/** Schema/database came from SQL qualifier or semantic model — do not global-search. */
|
||||
schemaFromQualifier: boolean;
|
||||
/** After a scoped miss, retry with globalSearch by exact table name. */
|
||||
allowGlobalFallback: boolean;
|
||||
/** Unqualified + always mode: search across schemas first. */
|
||||
preferGlobalFirst: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the metadata scope for table hover / Ctrl+click navigation.
|
||||
*
|
||||
* Qualified names (`schema.table`, `db.schema.table`, MySQL-style `db.table`)
|
||||
* always use the SQL qualifier. Unqualified names follow `mode`:
|
||||
* - `current`: only the selected schema
|
||||
* - `fallback`: selected schema first, then name-filtered global search
|
||||
* - `always`: name-filtered global search first
|
||||
*
|
||||
* "Global" here means same-database cross-schema, not cross-database.
|
||||
*/
|
||||
export function resolveHoverTableLookupTarget(input: ResolveHoverTableLookupTargetInput): HoverTableLookupTarget {
|
||||
const tableName = input.tableName;
|
||||
let database = input.semanticDatabase ?? input.database;
|
||||
let schema = input.semanticSchema;
|
||||
let schemaFromQualifier = !!input.semanticSchema;
|
||||
|
||||
if (!schemaFromQualifier && input.identifierParts.length >= 2) {
|
||||
const parts = input.identifierParts;
|
||||
if (parts.length >= 3) {
|
||||
database = parts[parts.length - 3]!;
|
||||
schema = parts[parts.length - 2];
|
||||
schemaFromQualifier = true;
|
||||
} else {
|
||||
if (input.databaseType && !isSchemaAware(input.databaseType) && !isSingleDatabase(input.databaseType)) {
|
||||
database = parts[0]!;
|
||||
schema = undefined;
|
||||
} else {
|
||||
schema = parts[0];
|
||||
}
|
||||
schemaFromQualifier = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!schemaFromQualifier) {
|
||||
schema = input.schema;
|
||||
}
|
||||
|
||||
const unqualified = !schemaFromQualifier;
|
||||
const preferGlobalFirst = unqualified && input.mode === "always";
|
||||
const allowGlobalFallback = unqualified && (input.mode === "fallback" || input.mode === "always");
|
||||
|
||||
return {
|
||||
database,
|
||||
schema: preferGlobalFirst ? undefined : schema,
|
||||
catalog: input.catalog,
|
||||
tableName,
|
||||
schemaFromQualifier,
|
||||
allowGlobalFallback,
|
||||
preferGlobalFirst,
|
||||
};
|
||||
}
|
||||
|
||||
/** Prefer the selected schema when the same table name exists in multiple schemas. */
|
||||
export function pickHoverTableMatch<T extends { name: string; schema?: string }>(tableName: string, tables: readonly T[], preferredSchema?: string): T | null {
|
||||
const nameLower = tableName.toLowerCase();
|
||||
const nameMatches = tables.filter((table) => table.name.toLowerCase() === nameLower);
|
||||
if (nameMatches.length === 0) return null;
|
||||
if (preferredSchema) {
|
||||
const preferred = nameMatches.find((table) => table.schema?.toLowerCase() === preferredSchema.toLowerCase());
|
||||
if (preferred) return preferred;
|
||||
}
|
||||
return nameMatches[0] ?? null;
|
||||
}
|
||||
|
||||
export interface MatchHoverTableCandidatesOptions {
|
||||
/** Qualified forms to try first (e.g. `schema.table` from the token or semantic model). */
|
||||
lookups?: string[];
|
||||
tableName: string;
|
||||
preferredSchema?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match hover/navigation table candidates.
|
||||
*
|
||||
* Qualified lookups use {@link matchTable}. Unqualified names go through
|
||||
* {@link pickHoverTableMatch} so the toolbar schema wins over arbitrary order.
|
||||
*/
|
||||
export function matchHoverTableCandidates<T extends { name: string; schema?: string; database?: string }>(tables: readonly T[], options: MatchHoverTableCandidatesOptions): T | null {
|
||||
const seen = new Set<string>();
|
||||
for (const lookup of options.lookups ?? []) {
|
||||
const trimmed = lookup.trim();
|
||||
if (!trimmed) continue;
|
||||
const key = trimmed.toLowerCase();
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
if (splitQualifiedIdentifier(trimmed).length < 2) continue;
|
||||
const matched = matchTable(trimmed, tables);
|
||||
if (matched) return matched;
|
||||
}
|
||||
return pickHoverTableMatch(options.tableName, tables, options.preferredSchema);
|
||||
}
|
||||
@@ -126,6 +126,10 @@ describe("EDITOR_SETTINGS_DRAFT_KEYS", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("completionTriggerMode");
|
||||
});
|
||||
|
||||
it("includes tableHoverLookupMode", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tableHoverLookupMode");
|
||||
});
|
||||
|
||||
it("includes the SQL variable substitution master switch", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("sqlVariableSubstitutionEnabled");
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
|
||||
import { normalizeQueryResultMaxRows } from "@/lib/dataGrid/queryResultRowLimit";
|
||||
import { normalizeExternalSqlEditorMaxMb } from "@/lib/sql/sqlFileOpen";
|
||||
import { normalizeCompletionTriggerMode } from "@/lib/sql/sqlCompletionTriggerPolicy";
|
||||
import { normalizeTableHoverLookupMode } from "@/lib/editor/hoverTableLookup";
|
||||
import { normalizeRedisKeyTemplates } from "@/lib/redis/redisKeyTemplates";
|
||||
|
||||
export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
@@ -85,6 +86,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"generateSqlQuoteIdentifiers",
|
||||
"formatSqlOnSqlFileSave",
|
||||
"showTableDdlHoverPreview",
|
||||
"tableHoverLookupMode",
|
||||
"updateNotificationsEnabled",
|
||||
"autoDownloadUpdates",
|
||||
"sidebarObjectInfoMode",
|
||||
@@ -138,6 +140,7 @@ function normalizedDraftValue(key: EditorSettingsDraftKey, value: unknown): unkn
|
||||
if (key === "queryResultMaxRows") return normalizeQueryResultMaxRowsDraft(value);
|
||||
if (key === "externalSqlEditorMaxMb") return normalizeExternalSqlEditorMaxMb(value);
|
||||
if (key === "completionTriggerMode") return normalizeCompletionTriggerMode(value);
|
||||
if (key === "tableHoverLookupMode") return normalizeTableHoverLookupMode(value);
|
||||
if (key === "redisKeyTemplates") return normalizeRedisKeyTemplates(value);
|
||||
if (key === "backgroundImage") return normalizeBackgroundImageSettings(value);
|
||||
return value;
|
||||
|
||||
@@ -138,6 +138,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
|
||||
{ id: "editor-generate-sql-quote-identifiers", category: "editor", titleKey: "settings.generateSqlQuoteIdentifiers", descriptionKey: "settings.generateSqlQuoteIdentifiersDescription", targetId: "editor" },
|
||||
{ id: "editor-format-sql-on-sql-file-save", category: "editor", titleKey: "settings.formatSqlOnSqlFileSave", descriptionKey: "settings.formatSqlOnSqlFileSaveDescription", targetId: "editor" },
|
||||
{ id: "editor-table-ddl-hover-preview", category: "editor", titleKey: "settings.showTableDdlHoverPreview", descriptionKey: "settings.showTableDdlHoverPreviewDescription", targetId: "editor" },
|
||||
{ id: "editor-table-hover-lookup-mode", category: "editor", titleKey: "settings.tableHoverLookupMode", descriptionKey: "settings.tableHoverLookupModeDescription", targetId: "editor" },
|
||||
{ id: "editor-diagnostics", category: "editor", titleKey: "settings.sqlSemanticDiagnosticsEnabled", descriptionKey: "settings.sqlSemanticDiagnosticsEnabledDescription", targetId: "editor" },
|
||||
{ id: "editor-sql-variables", category: "editor", titleKey: "settings.sqlVariableSyntax", descriptionKey: "settings.sqlVariableSyntaxDescription", targetId: "editor" },
|
||||
{ id: "editor-saved-sql-target", category: "editor", titleKey: "settings.savedSqlOpenTarget", targetId: "editor" },
|
||||
|
||||
@@ -100,6 +100,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
|
||||
"generateSqlQuoteIdentifiers",
|
||||
"formatSqlOnSqlFileSave",
|
||||
"showTableDdlHoverPreview",
|
||||
"tableHoverLookupMode",
|
||||
"sqlVariableSubstitutionEnabled",
|
||||
"sqlVariableSyntaxOverrides",
|
||||
],
|
||||
|
||||
@@ -643,7 +643,7 @@ export function splitQualifiedIdentifier(identifier: string): string[] {
|
||||
}
|
||||
|
||||
/** Match identifier against known table names (case-insensitive). Supports qualified identifiers like schema.table. */
|
||||
export function matchTable<T extends { name: string; database?: string; schema?: string }>(identifier: string, tables: T[]): T | null {
|
||||
export function matchTable<T extends { name: string; database?: string; schema?: string }>(identifier: string, tables: readonly T[]): T | null {
|
||||
const parts = splitQualifiedIdentifier(identifier);
|
||||
const normalizedIdentifier = parts.length > 0 ? parts.join(".").toLowerCase() : identifier.toLowerCase();
|
||||
|
||||
|
||||
Reference in new issue
Block a user