feat(query): pin, rename and label result run tabs
This commit is contained in:
26 files changed
+319
-32
No files matched your search
@@ -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