fix(editor): preserve view type in object navigation

This commit is contained in:
t8y2 committed 2026-07-14 18:41:26 +08:00
1 parent 39aa506e44
commit 69dd363417
8 files changed
+108 -25

No files matched your search

+23 -2
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { extractIdentifierAt, extractIdentifierDetailsAt, isSqlKeyword, matchTable, splitQualifiedIdentifier } from "@/lib/sql/sqlNavigation";
import { extractIdentifierAt, extractIdentifierDetailsAt, isSqlKeyword, matchTable, splitQualifiedIdentifier, sqlObjectHoverDetail, sqlObjectNavigationTableType, sqlObjectNavigationTarget } from "@/lib/sql/sqlNavigation";
describe("extractIdentifierAt", () => {
it("extracts unquoted qualified identifiers", () => {
@@ -52,9 +52,10 @@ describe("splitQualifiedIdentifier", () => {
describe("matchTable", () => {
it("matches schema-qualified table identifiers", () => {
const table = { schema: "MAAC00", name: "Accounts" };
const table = { schema: "MAAC00", name: "Accounts", type: "view" as const };
expect(matchTable("maac00.accounts", [table])).toBe(table);
expect(matchTable("maac00.accounts", [table])?.type).toBe("view");
});
it("matches catalog.schema.table identifiers against schema-scoped tables", () => {
@@ -73,3 +74,23 @@ describe("matchTable", () => {
expect(matchTable("u.users", [{ schema: "public", name: "users" }])).toBeNull();
});
});
describe("SQL object navigation metadata", () => {
it("preserves view type and schema for command-click navigation", () => {
expect(sqlObjectNavigationTarget({ name: "active_users", schema: "dbo", type: "view" })).toEqual({
name: "active_users",
schema: "dbo",
type: "view",
});
});
it("uses the object type in hover details", () => {
expect(sqlObjectHoverDetail({ name: "active_users", schema: "dbo", type: "view" })).toBe("view in dbo");
expect(sqlObjectHoverDetail({ name: "users", schema: "dbo", type: "table" })).toBe("table in dbo");
});
it("maps navigation types to table metadata types", () => {
expect(sqlObjectNavigationTableType({ name: "active_users", type: "view" })).toBe("VIEW");
expect(sqlObjectNavigationTableType({ name: "users", type: "table" })).toBe("TABLE");
});
});
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { tableObjectSourceKind } from "@/lib/table/tableObjectSourceKind";
describe("tableObjectSourceKind", () => {
it("routes views to object-source DDL", () => {
expect(tableObjectSourceKind("VIEW")).toBe("VIEW");
expect(tableObjectSourceKind("materialized view")).toBe("MATERIALIZED_VIEW");
});
it("keeps regular tables on table DDL generation", () => {
expect(tableObjectSourceKind("BASE TABLE")).toBeUndefined();
expect(tableObjectSourceKind("TABLE")).toBeUndefined();
});
});
+24 -1
View File
@@ -128,6 +128,29 @@ export interface ExtractedSqlIdentifier {
quoted: boolean;
}
export interface SqlObjectNavigationTarget {
name: string;
schema?: string;
type?: "table" | "view";
}
export function sqlObjectNavigationTarget(table: SqlObjectNavigationTarget): SqlObjectNavigationTarget {
return {
name: table.name,
...(table.schema ? { schema: table.schema } : {}),
...(table.type ? { type: table.type } : {}),
};
}
export function sqlObjectHoverDetail(table: SqlObjectNavigationTarget): string {
const objectType = table.type === "view" ? "view" : "table";
return table.schema ? `${objectType} in ${table.schema}` : objectType;
}
export function sqlObjectNavigationTableType(table: SqlObjectNavigationTarget): "TABLE" | "VIEW" {
return table.type === "view" ? "VIEW" : "TABLE";
}
function isIdentifierChar(char: string | undefined): boolean {
return !!char && /^[A-Za-z0-9_$]$/.test(char);
}
@@ -237,7 +260,7 @@ export function splitQualifiedIdentifier(identifier: string): string[] {
}
/** Match identifier against known table names (case-insensitive). Supports qualified identifiers like schema.table. */
export function matchTable(identifier: string, tables: Array<{ name: string; schema?: string }>): { name: string; schema?: string } | null {
export function matchTable<T extends { name: string; schema?: string }>(identifier: string, tables: T[]): T | null {
const parts = splitQualifiedIdentifier(identifier);
const normalizedIdentifier = parts.length > 0 ? parts.join(".").toLowerCase() : identifier.toLowerCase();
@@ -0,0 +1,11 @@
import type { ObjectSourceKind } from "@/types/database";
export function tableObjectSourceKind(tableType: string | null | undefined): ObjectSourceKind | undefined {
const normalized = tableType
?.trim()
.toUpperCase()
.replace(/[\s-]+/g, "_");
if (normalized === "VIEW") return "VIEW";
if (normalized === "MATERIALIZED_VIEW") return "MATERIALIZED_VIEW";
return undefined;
}