fix(query): use adjacent comments for result labels

This commit is contained in:
zipg authored and GitHub committed 2026-09-18 01:37:02 +08:00
1 parent 422bd931fc
commit 2e85c8b55d
3 files changed
+31 -25

No files matched your search

+14 -10
View File
@@ -2,21 +2,25 @@ import { describe, expect, it } from "vitest";
import { queryResultNameFromPreamble, queryResultSourceLabel } from "@/lib/sql/queryResultSource";
describe("queryResultNameFromPreamble", () => {
it("uses the nearest non-empty Name line comment", () => {
expect(queryResultNameFromPreamble("-- Name: Old name\n-- unrelated\r\n -- NAME : Latest name \r\n")).toBe("Latest name");
expect(queryResultNameFromPreamble("-- Name: kept\n-- Name: \n")).toBe("kept");
expect(queryResultNameFromPreamble("-- Name: explicit\n-- nearest ordinary comment\n")).toBe("explicit");
it("uses only the immediately preceding line comment", () => {
expect(queryResultNameFromPreamble("-- Name: Latest name\r\n")).toBe("Latest name");
expect(queryResultNameFromPreamble("-- unrelated\n")).toBe("unrelated");
expect(queryResultNameFromPreamble("-- indented\n ")).toBe("indented");
expect(queryResultNameFromPreamble("-- Name: Old name\n-- unrelated\n")).toBe("unrelated");
expect(queryResultNameFromPreamble("-- Name: explicit\n-- \n")).toBeUndefined();
});
it("falls back to the nearest non-empty line comment", () => {
expect(queryResultNameFromPreamble("-- older comment\n/*\n-- Name: block\n*/\n-- 当前时间\n")).toBe("当前时间");
expect(queryResultNameFromPreamble("/* -- block only */\n-- \n")).toBeUndefined();
it("stops at a blank line and ignores non-line comments", () => {
expect(queryResultNameFromPreamble("-- older comment\n\n")).toBeUndefined();
expect(queryResultNameFromPreamble("/* Name: block */\n")).toBeUndefined();
expect(queryResultNameFromPreamble("-- earlier comment\n/* block */\n")).toBeUndefined();
});
it("accepts hash comments for MySQL without treating them as portable SQL comments", () => {
expect(queryResultNameFromPreamble("# MySQL report\nSELECT 1", { databaseType: "mysql" })).toBe("MySQL report");
expect(queryResultNameFromPreamble("# temporary table\nSELECT 1", { databaseType: "sqlserver" })).toBeUndefined();
expect(queryResultNameFromPreamble("# portable comment\nSELECT 1")).toBeUndefined();
expect(queryResultNameFromPreamble("# MySQL report\n", { databaseType: "mysql" })).toBe("MySQL report");
expect(queryResultNameFromPreamble("# temporary table\n", { databaseType: "sqlserver" })).toBeUndefined();
expect(queryResultNameFromPreamble("# portable comment\n")).toBeUndefined();
expect(queryResultNameFromPreamble("# MySQL report\n\n", { databaseType: "mysql" })).toBeUndefined();
});
});