fix(export): name query result exports after result tab

This commit is contained in:
zipg authored and GitHub committed 2026-09-22 01:09:48 +08:00
1 parent f28942421f
commit 529267cded
5 files changed
+46 -1

No files matched your search

@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { queryResultExportBaseName, sanitizeExportBaseName } from "@/lib/export/saveTextFile";
describe("queryResultExportBaseName", () => {
it("prefers the result label derived from the SQL's nearby comment", () => {
expect(queryResultExportBaseName("检查结果", "query 5")).toBe("检查结果");
});
it("falls back to the tab title when the result carries no label", () => {
expect(queryResultExportBaseName(undefined, "query 5")).toBe("query 5");
expect(queryResultExportBaseName("", "query 5")).toBe("query 5");
expect(queryResultExportBaseName(" ", "query 5")).toBe("query 5");
});
it("keeps the schema-qualified label so exported files stay identifiable", () => {
expect(queryResultExportBaseName("dbx.orders", "本地MySQL8.4@dbx")).toBe("dbx.orders");
});
it("returns undefined when neither source provides a name", () => {
expect(queryResultExportBaseName(undefined, undefined)).toBeUndefined();
expect(queryResultExportBaseName("", "")).toBeUndefined();
});
it("keeps the sanitized base name free of path separators", () => {
expect(sanitizeExportBaseName(queryResultExportBaseName("检查结果/2026", "query 5")!)).toBe("检查结果_2026");
});
});
@@ -39,6 +39,19 @@ export async function saveTextFile(content: string, defaultFileName: string, fil
return true;
}
/**
* Export file base name for a query result (#9894).
*
* The result tab is already named after the SQL's nearby comment (`-- name: x`
* or a plain leading comment) or after the `schema.table` that produced it, so
* an export of that result should reuse the same name instead of the generic
* query tab title ("query 3", "查询 3"). Table data tabs keep passing their
* table name, which is why the caller decides the fallback.
*/
export function queryResultExportBaseName(resultLabel: string | undefined, tabTitle: string | undefined): string | undefined {
return resultLabel?.trim() || tabTitle?.trim() || undefined;
}
export function sanitizeExportBaseName(value: string): string {
return replaceControlCharacters(
value