feat(query): pin, rename and label result run tabs
This commit is contained in:
26 files changed
+319
-32
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { queryResultNameFromPreamble, queryResultSourceLabel } from "@/lib/sql/queryResultSource";
|
||||
import { queryResultNameFromPreamble, queryResultSourceLabel, queryResultSourceNameParts } from "@/lib/sql/queryResultSource";
|
||||
|
||||
describe("queryResultNameFromPreamble", () => {
|
||||
it("uses only the immediately preceding line comment", () => {
|
||||
@@ -65,3 +65,15 @@ describe("queryResultSourceLabel", () => {
|
||||
expect(queryResultSourceLabel("SELECT 1", { database: "app", databaseType: "mysql" })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("queryResultSourceNameParts", () => {
|
||||
it("keeps the qualifier separate even when the database name contains dots", () => {
|
||||
expect(queryResultSourceNameParts("SELECT * FROM data_monitor", { database: "cosimulation2.0", databaseType: "mysql" })).toEqual({ qualifier: "cosimulation2.0", name: "data_monitor" });
|
||||
});
|
||||
|
||||
it("prefers the explicit qualifier and drops an empty one", () => {
|
||||
expect(queryResultSourceNameParts("SELECT * FROM analytics.events", { database: "app", databaseType: "mysql" })).toEqual({ qualifier: "analytics", name: "events" });
|
||||
expect(queryResultSourceNameParts("SELECT * FROM events", { databaseType: "mysql" })).toEqual({ name: "events" });
|
||||
expect(queryResultSourceNameParts("SELECT 1", { database: "app", databaseType: "mysql" })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
+92
-1
@@ -10,6 +10,7 @@ import {
|
||||
resultGridCacheKey,
|
||||
resultGridColumnWidthCacheKey,
|
||||
resultGridInstanceKey,
|
||||
resultRunItems,
|
||||
resultSourceRange,
|
||||
statementExecutionMarkers,
|
||||
tabColorStyle,
|
||||
@@ -23,7 +24,7 @@ import {
|
||||
dirtyTabTitleStyle,
|
||||
} from "@/lib/tabs/tabPresentation";
|
||||
import { sqlTextFingerprint } from "@/lib/sql/sqlTextFingerprint";
|
||||
import type { ConnectionConfig, QueryResult, QueryTab } from "@/types/database";
|
||||
import type { ConnectionConfig, QueryResult, QueryResultRun, QueryTab } from "@/types/database";
|
||||
|
||||
const translations: Record<string, string> = {
|
||||
"tabs.tooltipConnection": "Connection:",
|
||||
@@ -176,6 +177,51 @@ describe("query result labels", () => {
|
||||
expect(item?.label).toBeUndefined();
|
||||
expect(item?.title).toBe("SELECT 1");
|
||||
});
|
||||
|
||||
it("shows only the object name when result set names exclude the database", () => {
|
||||
const results = [
|
||||
{
|
||||
columns: ["id"],
|
||||
rows: [[1]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
sourceLabel: "cosimulation2.0.data_monitor",
|
||||
sourceQualifier: "cosimulation2.0",
|
||||
sourceName: "data_monitor",
|
||||
sourceStatement: "SELECT * FROM data_monitor",
|
||||
},
|
||||
];
|
||||
|
||||
const [withDatabase] = tabularResultItems(results);
|
||||
const [withoutDatabase] = tabularResultItems(results, { includeSourceDatabase: false });
|
||||
|
||||
expect(withDatabase?.label).toBe("cosimulation2.0.data_monitor");
|
||||
expect(withoutDatabase?.label).toBe("data_monitor");
|
||||
expect(withoutDatabase?.displayLabel).toBe("data_monitor");
|
||||
// 完整名称(含库名)仍保留在悬浮提示中
|
||||
expect(withoutDatabase?.title).toBe("cosimulation2.0.data_monitor");
|
||||
});
|
||||
|
||||
it("keeps a custom result name even when the database is hidden", () => {
|
||||
const [item] = tabularResultItems(
|
||||
[
|
||||
{
|
||||
columns: ["id"],
|
||||
rows: [[1]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
sourceLabel: "My weekly report",
|
||||
sourceQualifier: "app",
|
||||
sourceName: "users",
|
||||
sourceStatement: "SELECT * FROM users",
|
||||
},
|
||||
],
|
||||
{ includeSourceDatabase: false },
|
||||
);
|
||||
|
||||
expect(item?.label).toBe("My weekly report");
|
||||
expect(item?.title).toBe("My weekly report");
|
||||
});
|
||||
});
|
||||
|
||||
describe("query result grid identity", () => {
|
||||
@@ -192,6 +238,51 @@ describe("query result grid identity", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("result run labels", () => {
|
||||
// 与 store 真实创建批次的方式保持一致:默认标题是 `Run N`,只有重命名/多库执行才标记 customTitle
|
||||
const run = (id: string, sequence: number, result?: QueryResult, overrides: Partial<QueryResultRun> = {}): QueryResultRun => ({ id, title: `Run ${sequence}`, sequence, sql: "SELECT * FROM users", createdAt: sequence, result, ...overrides });
|
||||
const sourceResult = (label: string, qualifier: string, name: string): QueryResult => ({ columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1, sourceLabel: label, sourceQualifier: qualifier, sourceName: name }) as QueryResult;
|
||||
|
||||
it("names result runs after their source table instead of the run ordinal", () => {
|
||||
const items = resultRunItems(
|
||||
queryTab({
|
||||
resultRuns: [run("run-1", 1, sourceResult("app.users", "app", "users")), run("run-2", 2, sourceResult("app.orders", "app", "orders"))],
|
||||
}) as QueryTab,
|
||||
);
|
||||
|
||||
expect(items.map((item) => item.sourceLabel)).toEqual(["app.users", "app.orders"]);
|
||||
});
|
||||
|
||||
it("suffixes repeated sources and follows the database-name setting", () => {
|
||||
const tab = queryTab({
|
||||
resultRuns: [run("run-1", 1, sourceResult("cosimulation2.0.users", "cosimulation2.0", "users")), run("run-2", 2, sourceResult("cosimulation2.0.users", "cosimulation2.0", "users")), run("run-3", 3)],
|
||||
}) as QueryTab;
|
||||
|
||||
expect(resultRunItems(tab).map((item) => item.sourceLabel)).toEqual(["cosimulation2.0.users", "cosimulation2.0.users (2)", undefined]);
|
||||
expect(resultRunItems(tab, { includeSourceDatabase: false }).map((item) => item.sourceLabel)).toEqual(["users", "users (2)", undefined]);
|
||||
});
|
||||
|
||||
it("keeps a renamed run title ahead of the source label", () => {
|
||||
const items = resultRunItems(queryTab({ resultRuns: [run("run-1", 1, sourceResult("app.users", "app", "users"), { title: "报表", customTitle: true })] }) as QueryTab);
|
||||
|
||||
expect(items[0]?.title).toBe("报表");
|
||||
expect(items[0]?.sourceLabel).toBe("app.users");
|
||||
});
|
||||
|
||||
it("replaces the default Run N title with the source label", () => {
|
||||
const items = resultRunItems(queryTab({ resultRuns: [run("run-1", 1, sourceResult("app.users", "app", "users"))] }) as QueryTab);
|
||||
|
||||
expect(items[0]?.title).toBe("");
|
||||
expect(items[0]?.sourceLabel).toBe("app.users");
|
||||
});
|
||||
|
||||
it("falls back to the run SQL for runs created before source labels were stored", () => {
|
||||
const items = resultRunItems(queryTab({ resultRuns: [run("run-1", 1)] }) as QueryTab, { database: "cosimulation2.0", databaseType: "mysql" });
|
||||
|
||||
expect(items[0]?.sourceLabel).toBe("cosimulation2.0.users");
|
||||
});
|
||||
});
|
||||
|
||||
describe("tab group presentation", () => {
|
||||
// 编号由 store 在标签列表变化时分配,渲染函数只读结果;这里把两步串起来,
|
||||
// 与真实调用顺序保持一致(#9938)。
|
||||
|
||||
@@ -11,9 +11,14 @@ export interface SavedQueryResultRun {
|
||||
sql: string;
|
||||
createdAt: number;
|
||||
pinned?: boolean;
|
||||
/** 标题是否为用户/多库执行显式指定;为假时结果标签改用来源名显示 */
|
||||
customTitle?: boolean;
|
||||
activeResultIndex?: number;
|
||||
resultCacheKey?: string;
|
||||
resultEvicted?: boolean;
|
||||
/** 结果来源(库名.表名 / 表名),用于结果标签命名;与结果 payload 分离,回收 payload 后仍可显示 */
|
||||
sourceLabel?: string;
|
||||
sourceName?: string;
|
||||
}
|
||||
|
||||
export interface SavedOpenTab {
|
||||
@@ -219,6 +224,9 @@ export function serializeOpenTabs(tabs: QueryTab[]): SavedOpenTab[] {
|
||||
sequence: run.sequence,
|
||||
sql: run.sql,
|
||||
createdAt: run.createdAt,
|
||||
...(run.sourceLabel ? { sourceLabel: run.sourceLabel } : {}),
|
||||
...(run.sourceName ? { sourceName: run.sourceName } : {}),
|
||||
...(run.customTitle ? { customTitle: true } : {}),
|
||||
...(run.pinned ? { pinned: true } : {}),
|
||||
activeResultIndex: run.activeResultIndex,
|
||||
...(run.resultCacheKey !== undefined ? { resultCacheKey: run.resultCacheKey } : {}),
|
||||
|
||||
@@ -46,6 +46,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"tabSortMode",
|
||||
"showColumnCommentsInHeader",
|
||||
"showColumnTypesInHeader",
|
||||
"showResultSourceDatabase",
|
||||
"dataGridShowTransposeFieldMetadata",
|
||||
"colorizeDataGridCellTypes",
|
||||
"dataGridTypeColorSchemes",
|
||||
|
||||
@@ -221,6 +221,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
|
||||
{ id: "query-result-max-rows", category: "data", titleKey: "settings.queryResultMaxRows", descriptionKey: "settings.queryResultMaxRowsDescription", targetId: "data" },
|
||||
{ id: "data-grid-header-comments", category: "data", titleKey: "settings.showColumnCommentsInHeader", descriptionKey: "settings.showColumnCommentsInHeaderDescription", targetId: "data" },
|
||||
{ id: "data-grid-header-types", category: "data", titleKey: "settings.showColumnTypesInHeader", descriptionKey: "settings.showColumnTypesInHeaderDescription", targetId: "data" },
|
||||
{ id: "result-source-database-name", category: "data", titleKey: "settings.showResultSourceDatabase", descriptionKey: "settings.showResultSourceDatabaseDescription", targetId: "data" },
|
||||
{ id: "data-grid-transpose-field-metadata", category: "data", titleKey: "settings.dataGridShowTransposeFieldMetadata", descriptionKey: "settings.dataGridShowTransposeFieldMetadataDescription", targetId: "data" },
|
||||
{ id: "data-grid-cell-type-colors", category: "data", titleKey: "settings.colorizeDataGridCellTypes", descriptionKey: "settings.colorizeDataGridCellTypesDescription", targetId: "data" },
|
||||
{ id: "data-grid-type-colors", category: "data", titleKey: "settings.dataGridTypeColorScheme", descriptionKey: "settings.dataGridTypeColorSchemeDescription", targetId: "data-grid-type-colors" },
|
||||
|
||||
@@ -140,6 +140,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
|
||||
data: [
|
||||
"showColumnCommentsInHeader",
|
||||
"showColumnTypesInHeader",
|
||||
"showResultSourceDatabase",
|
||||
"dataGridShowTransposeFieldMetadata",
|
||||
"colorizeDataGridCellTypes",
|
||||
"dataGridTypeColorSchemes",
|
||||
@@ -268,6 +269,7 @@ const PASS_THROUGH_BOOLEAN_KEYS = [
|
||||
"confirmUnsavedSqlClose",
|
||||
"showColumnCommentsInHeader",
|
||||
"showColumnTypesInHeader",
|
||||
"showResultSourceDatabase",
|
||||
"colorizeDataGridCellTypes",
|
||||
"showIndexIndicatorsInHeader",
|
||||
"compactColumnHeaderActions",
|
||||
|
||||
@@ -29,7 +29,18 @@ function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRow
|
||||
return sources.filter((source) => source.kind === kind).sort((left, right) => left.sourceSpan.start - right.sourceSpan.start)[0];
|
||||
}
|
||||
|
||||
export function queryResultSourceLabel(sql: string, options: QueryResultSourceLabelOptions = {}): string | undefined {
|
||||
export interface QueryResultSourceNameParts {
|
||||
/** 库名 / schema 等限定名;SQL 未显式限定时由当前库补齐 */
|
||||
qualifier?: string;
|
||||
/** 表名或写入目标对象名 */
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析结果集来源的限定名与对象名。结果集页签需要按设置决定是否展示限定名,
|
||||
* 而库名本身可能包含点(例如 `cosimulation2.0`),所以不能对拼好的标签做字符串切割。
|
||||
*/
|
||||
export function queryResultSourceNameParts(sql: string, options: QueryResultSourceLabelOptions = {}): QueryResultSourceNameParts | undefined {
|
||||
const statement = sql.trim();
|
||||
if (!statement) return undefined;
|
||||
|
||||
@@ -38,5 +49,11 @@ export function queryResultSourceLabel(sql: string, options: QueryResultSourceLa
|
||||
if (!source?.name) return undefined;
|
||||
|
||||
const qualifier = source.qualifierParts[source.qualifierParts.length - 1]?.trim() || options.database?.trim();
|
||||
return qualifier ? `${qualifier}.${source.name}` : source.name;
|
||||
return { qualifier: qualifier || undefined, name: source.name };
|
||||
}
|
||||
|
||||
export function queryResultSourceLabel(sql: string, options: QueryResultSourceLabelOptions = {}): string | undefined {
|
||||
const parts = queryResultSourceNameParts(sql, options);
|
||||
if (!parts) return undefined;
|
||||
return parts.qualifier ? `${parts.qualifier}.${parts.name}` : parts.name;
|
||||
}
|
||||
@@ -10,7 +10,8 @@ import type { SqlParameterOptions } from "@/lib/sql/sqlParameters";
|
||||
import { sqlTextFingerprint } from "@/lib/sql/sqlTextFingerprint";
|
||||
import { isQueryExecutionErrorResult } from "@/lib/query/queryResultError";
|
||||
import type { SqlErrorPosition } from "@/lib/backend/errorUtils";
|
||||
import type { BatchSqlExecution, ConnectionConfig, DatabaseType, QueryResult, QueryTab } from "@/types/database";
|
||||
import { queryResultSourceNameParts } from "@/lib/sql/queryResultSource";
|
||||
import type { BatchSqlExecution, ConnectionConfig, DatabaseType, QueryResult, QueryResultRun, QueryTab } from "@/types/database";
|
||||
|
||||
type Translate = (key: string, params?: Record<string, unknown>) => string;
|
||||
export type OutputView = "result" | "summary" | "explain" | "chart";
|
||||
@@ -440,20 +441,26 @@ export function queryResultExecutionSql(tab: Pick<QueryTab, "result" | "resultBa
|
||||
return tab.resultSortedSql || resultSqlForGrid(tab);
|
||||
}
|
||||
|
||||
export function tabularResultItems(results: QueryResult[] | undefined): { result: QueryResult; index: number; n: number; label?: string; displayLabel?: string; labelTruncated: boolean; title?: string }[] {
|
||||
export function tabularResultItems(results: QueryResult[] | undefined, options: { includeSourceDatabase?: boolean } = {}): { result: QueryResult; index: number; n: number; label?: string; displayLabel?: string; labelTruncated: boolean; title?: string }[] {
|
||||
if (!results) return [];
|
||||
return results
|
||||
.map((result, index) => ({ result, index }))
|
||||
.filter((item) => item.result.columns.length > 0 && item.result.server_message !== true)
|
||||
.map((item, ordinal) => {
|
||||
const label = queryResultStatementLabel(item.result);
|
||||
const displayLabel = label ? middleEllipsis(label) : undefined;
|
||||
const { sourceName, sourceQualifier } = item.result;
|
||||
// 只有在标签确实由“库名.对象名”拼成时才启用短名称,避免覆盖 `-- name: xxx` 之类的自定义名称
|
||||
const qualifiedLabel = sourceName ? (sourceQualifier ? `${sourceQualifier}.${sourceName}` : sourceName) : undefined;
|
||||
// 关闭“结果集名称包含数据库名”时,页签与结果列表只展示对象名;
|
||||
// 完整名称(含库名)仍保留在 title,供悬浮提示与搜索使用。
|
||||
const displaySource = options.includeSourceDatabase === false && sourceName && qualifiedLabel === label ? sourceName : label;
|
||||
const displayLabel = displaySource ? middleEllipsis(displaySource) : undefined;
|
||||
return {
|
||||
...item,
|
||||
n: ordinal + 1,
|
||||
label,
|
||||
label: displaySource,
|
||||
displayLabel,
|
||||
labelTruncated: !!label && displayLabel !== label,
|
||||
labelTruncated: !!displaySource && displayLabel !== displaySource,
|
||||
title: item.result.sourceLabel || item.result.sourceStatement,
|
||||
};
|
||||
});
|
||||
@@ -463,14 +470,52 @@ export function activeResultRun(tab: Pick<QueryTab, "resultRuns" | "activeResult
|
||||
return tab.resultRuns?.find((run) => run.id === tab.activeResultRunId);
|
||||
}
|
||||
|
||||
export function resultRunItems(tab: Pick<QueryTab, "resultRuns" | "activeResultRunId">): { id: string; title: string; sequence: number; active: boolean; pinned: boolean }[] {
|
||||
return (tab.resultRuns ?? []).map((run) => ({
|
||||
id: run.id,
|
||||
title: run.title,
|
||||
sequence: run.sequence,
|
||||
active: run.id === tab.activeResultRunId,
|
||||
pinned: run.pinned === true,
|
||||
}));
|
||||
/**
|
||||
* 执行批次默认显示名:取该批次主结果的来源(库名.表名 / 表名)。
|
||||
* 连表查询同样取 FROM 之后的第一个物理表,与结果集页签的来源解析保持一致。
|
||||
*/
|
||||
function resultRunSourceLabel(run: QueryResultRun, includeSourceDatabase: boolean, fallback: { database?: string; databaseType?: DatabaseType }): string | undefined {
|
||||
// 优先用批次自带的来源:非活动批次的结果 payload 会被回收,payload 里的来源会丢失
|
||||
const own = includeSourceDatabase ? run.sourceLabel || run.sourceName : run.sourceName || run.sourceLabel;
|
||||
if (own) return own;
|
||||
const candidates = run.result ? [run.result, ...(run.results ?? [])] : (run.results ?? []);
|
||||
for (const result of candidates) {
|
||||
if (!result) continue;
|
||||
// 批次级来源缺失时回退到结果本身;关闭“结果集名称包含数据库名”时优先用对象名
|
||||
const label = includeSourceDatabase ? result.sourceLabel || result.sourceName : result.sourceName || result.sourceLabel;
|
||||
if (label) return label;
|
||||
}
|
||||
// 历史批次(功能上线前创建的)没有来源信息:用批次 SQL 重新解析,连表查询同样取第一个物理表
|
||||
if (run.sql && fallback.databaseType) {
|
||||
const parts = queryResultSourceNameParts(run.sql, { database: fallback.database, databaseType: fallback.databaseType });
|
||||
if (parts) return includeSourceDatabase ? (parts.qualifier ? `${parts.qualifier}.${parts.name}` : parts.name) : parts.name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resultRunItems(tab: Pick<QueryTab, "resultRuns" | "activeResultRunId">, options: { includeSourceDatabase?: boolean; database?: string; databaseType?: DatabaseType } = {}): { id: string; title: string; sequence: number; active: boolean; pinned: boolean; sourceLabel?: string }[] {
|
||||
const includeSourceDatabase = options.includeSourceDatabase !== false;
|
||||
const fallback = { database: options.database, databaseType: options.databaseType };
|
||||
const seenBySource = new Map<string, number>();
|
||||
return (tab.resultRuns ?? []).map((run) => {
|
||||
const source = resultRunSourceLabel(run, includeSourceDatabase, fallback);
|
||||
let sourceLabel = source;
|
||||
if (source) {
|
||||
// 同一张表被查询多次时用序号后缀区分页签(users、users (2)…)
|
||||
const seen = (seenBySource.get(source) ?? 0) + 1;
|
||||
seenBySource.set(source, seen);
|
||||
if (seen > 1) sourceLabel = `${source} (${seen})`;
|
||||
}
|
||||
return {
|
||||
id: run.id,
|
||||
// 系统默认标题(Run N)由来源名取代;用户重命名/多库按目标命名过的标题优先
|
||||
title: run.customTitle ? run.title : "",
|
||||
sequence: run.sequence,
|
||||
active: run.id === tab.activeResultRunId,
|
||||
pinned: run.pinned === true,
|
||||
sourceLabel,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function resultGridCacheKey(tab: Pick<QueryTab, "id" | "activeResultRunId" | "activeResultIndex">): string {
|
||||
|
||||
@@ -74,6 +74,8 @@ interface ColumnarQueryResult {
|
||||
truncated?: boolean;
|
||||
has_more?: boolean;
|
||||
sourceLabel?: string;
|
||||
sourceQualifier?: string;
|
||||
sourceName?: string;
|
||||
sourceStatement?: string;
|
||||
sourceFrom?: number;
|
||||
sourceTo?: number;
|
||||
@@ -363,6 +365,8 @@ function stripSessionIds(result: QueryResult | undefined): QueryResult | undefin
|
||||
session_id: undefined,
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceQualifier: result.sourceQualifier,
|
||||
sourceName: result.sourceName,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
@@ -419,6 +423,8 @@ function toColumnarResult(result: QueryResult | undefined): ColumnarQueryResult
|
||||
truncated: result.truncated,
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceQualifier: result.sourceQualifier,
|
||||
sourceName: result.sourceName,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
@@ -449,6 +455,8 @@ function fromColumnarResult(result: ColumnarQueryResult | undefined): QueryResul
|
||||
session_id: undefined,
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceQualifier: result.sourceQualifier,
|
||||
sourceName: result.sourceName,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
|
||||
Reference in new issue
Block a user