feat(grid): show column index indicators in data headers
This commit is contained in:
16 files changed
+272
-5
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildColumnIndexMap, columnIndexMetadataRequestCurrent, columnIndexNameKey, columnIndexTableIdentity } from "@/lib/dataGrid/dataGridColumnIndexIcon";
|
||||
import type { IndexInfo } from "@/types/database";
|
||||
|
||||
function indexInfo(overrides: Partial<IndexInfo>): IndexInfo {
|
||||
return {
|
||||
name: "idx_users_email",
|
||||
columns: ["email"],
|
||||
is_unique: false,
|
||||
is_primary: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("buildColumnIndexMap", () => {
|
||||
it("uses column metadata for custom-named and composite primary keys", () => {
|
||||
const map = buildColumnIndexMap([indexInfo({ name: "PK_USERS", columns: ["User_ID", "Tenant_ID"], is_unique: true }), indexInfo({ columns: ["user_id"] })], ["user_id", "tenant_id"]);
|
||||
|
||||
expect(map.get(columnIndexNameKey("USER_ID"))).toBe("primary");
|
||||
expect(map.get(columnIndexNameKey("tenant_id"))).toBe("primary");
|
||||
});
|
||||
|
||||
it("keeps the highest-priority kind with case-insensitive names", () => {
|
||||
const map = buildColumnIndexMap([indexInfo({ columns: ["Email"] }), indexInfo({ name: "uq_email", columns: ["email"], is_unique: true })]);
|
||||
expect(map.get(columnIndexNameKey("EMAIL"))).toBe("unique");
|
||||
});
|
||||
});
|
||||
|
||||
describe("column index metadata request guard", () => {
|
||||
it("includes the full table identity and rejects stale responses", () => {
|
||||
const users = columnIndexTableIdentity({
|
||||
connectionId: "c1",
|
||||
database: "db",
|
||||
schema: "public",
|
||||
tableName: "users",
|
||||
})!;
|
||||
const orders = columnIndexTableIdentity({
|
||||
connectionId: "c1",
|
||||
database: "db",
|
||||
schema: "public",
|
||||
tableName: "orders",
|
||||
})!;
|
||||
|
||||
expect(users).not.toBe(orders);
|
||||
expect(
|
||||
columnIndexMetadataRequestCurrent({
|
||||
requestGeneration: 2,
|
||||
currentGeneration: 2,
|
||||
requestIdentity: users,
|
||||
currentIdentity: users,
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
columnIndexMetadataRequestCurrent({
|
||||
requestGeneration: 2,
|
||||
currentGeneration: 3,
|
||||
requestIdentity: users,
|
||||
currentIdentity: users,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
columnIndexMetadataRequestCurrent({
|
||||
requestGeneration: 2,
|
||||
currentGeneration: 2,
|
||||
requestIdentity: users,
|
||||
currentIdentity: orders,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
import type { IndexInfo } from "@/types/database";
|
||||
|
||||
/** 列的索引类型分类 */
|
||||
export type ColumnIndexKind = "primary" | "unique" | "index" | "none";
|
||||
|
||||
export function columnIndexNameKey(name: string): string {
|
||||
return name.trim().toLowerCase();
|
||||
}
|
||||
|
||||
/** 从主键和索引列表构建规范化列名 → 最高优先级索引类型的映射 */
|
||||
export function buildColumnIndexMap(indexes: IndexInfo[], primaryKeyColumns: readonly string[] = []): Map<string, ColumnIndexKind> {
|
||||
const map = new Map<string, ColumnIndexKind>();
|
||||
for (const column of primaryKeyColumns) {
|
||||
const key = columnIndexNameKey(column);
|
||||
if (key) map.set(key, "primary");
|
||||
}
|
||||
for (const idx of indexes) {
|
||||
const kind: ColumnIndexKind = idx.is_primary ? "primary" : idx.is_unique ? "unique" : "index";
|
||||
for (const col of idx.columns) {
|
||||
const key = columnIndexNameKey(col);
|
||||
if (!key) continue;
|
||||
const existing = map.get(key);
|
||||
if (!existing || columnIndexPriority(kind) > columnIndexPriority(existing)) {
|
||||
map.set(key, kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
export function columnIndexTableIdentity(options: { connectionId?: string; database?: string; catalog?: string; schema?: string; tableName?: string }): string | undefined {
|
||||
if (!options.connectionId || !options.tableName) return undefined;
|
||||
return JSON.stringify([options.connectionId, options.database ?? "", options.catalog ?? "", options.schema ?? "", options.tableName]);
|
||||
}
|
||||
|
||||
export function columnIndexMetadataRequestCurrent(options: { requestGeneration: number; currentGeneration: number; requestIdentity: string; currentIdentity?: string }): boolean {
|
||||
return options.requestGeneration === options.currentGeneration && options.requestIdentity === options.currentIdentity;
|
||||
}
|
||||
|
||||
function columnIndexPriority(kind: ColumnIndexKind): number {
|
||||
switch (kind) {
|
||||
case "primary":
|
||||
return 3;
|
||||
case "unique":
|
||||
return 2;
|
||||
case "index":
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** 索引类型对应的颜色类名(橙色=主键,红色=唯一,绿色=普通) */
|
||||
export function columnIndexColorClass(kind: ColumnIndexKind): string {
|
||||
switch (kind) {
|
||||
case "primary":
|
||||
return "text-orange-500";
|
||||
case "unique":
|
||||
return "text-red-500";
|
||||
case "index":
|
||||
return "text-green-500";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"tabLayout",
|
||||
"showColumnCommentsInHeader",
|
||||
"showColumnTypesInHeader",
|
||||
"showIndexIndicatorsInHeader",
|
||||
"compactColumnHeaderActions",
|
||||
"dataGridQuickEntry",
|
||||
"dataGridAutoTransposeSingleRow",
|
||||
|
||||
@@ -168,6 +168,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
|
||||
{ id: "data-page-size", category: "data", titleKey: "settings.tableOpenPageSize", descriptionKey: "settings.tableOpenPageSizeDescription", targetId: "data" },
|
||||
{ id: "appearance-header-comments", category: "appearance", titleKey: "settings.showColumnCommentsInHeader", descriptionKey: "settings.showColumnCommentsInHeaderDescription", targetId: "appearance" },
|
||||
{ id: "appearance-header-types", category: "appearance", titleKey: "settings.showColumnTypesInHeader", descriptionKey: "settings.showColumnTypesInHeaderDescription", targetId: "appearance" },
|
||||
{ id: "appearance-index-indicators", category: "appearance", titleKey: "settings.showIndexIndicatorsInHeader", descriptionKey: "settings.showIndexIndicatorsInHeaderDescription", targetId: "appearance" },
|
||||
{ id: "appearance-compact-header-actions", category: "appearance", titleKey: "settings.compactColumnHeaderActions", descriptionKey: "settings.compactColumnHeaderActionsDescription", targetId: "appearance" },
|
||||
{ id: "appearance-auto-total", category: "appearance", titleKey: "settings.autoCalculateTotalRows", descriptionKey: "settings.autoCalculateTotalRowsDescription", targetId: "appearance" },
|
||||
{ id: "appearance-infinite-scroll", category: "appearance", titleKey: "settings.infiniteScroll", descriptionKey: "settings.infiniteScrollDescription", targetId: "appearance" },
|
||||
|
||||
Reference in new issue
Block a user