fix(query): use adjacent comments for result labels
This commit is contained in:
3 files changed
+31
-25
No files matched your search
+14
-10
@@ -2,21 +2,25 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { queryResultNameFromPreamble, queryResultSourceLabel } from "@/lib/sql/queryResultSource";
|
import { queryResultNameFromPreamble, queryResultSourceLabel } from "@/lib/sql/queryResultSource";
|
||||||
|
|
||||||
describe("queryResultNameFromPreamble", () => {
|
describe("queryResultNameFromPreamble", () => {
|
||||||
it("uses the nearest non-empty Name line comment", () => {
|
it("uses only the immediately preceding line comment", () => {
|
||||||
expect(queryResultNameFromPreamble("-- Name: Old name\n-- unrelated\r\n -- NAME : Latest name \r\n")).toBe("Latest name");
|
expect(queryResultNameFromPreamble("-- Name: Latest name\r\n")).toBe("Latest name");
|
||||||
expect(queryResultNameFromPreamble("-- Name: kept\n-- Name: \n")).toBe("kept");
|
expect(queryResultNameFromPreamble("-- unrelated\n")).toBe("unrelated");
|
||||||
expect(queryResultNameFromPreamble("-- Name: explicit\n-- nearest ordinary comment\n")).toBe("explicit");
|
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", () => {
|
it("stops at a blank line and ignores non-line comments", () => {
|
||||||
expect(queryResultNameFromPreamble("-- older comment\n/*\n-- Name: block\n*/\n-- 当前时间\n")).toBe("当前时间");
|
expect(queryResultNameFromPreamble("-- older comment\n\n")).toBeUndefined();
|
||||||
expect(queryResultNameFromPreamble("/* -- block only */\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", () => {
|
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("# MySQL report\n", { databaseType: "mysql" })).toBe("MySQL report");
|
||||||
expect(queryResultNameFromPreamble("# temporary table\nSELECT 1", { databaseType: "sqlserver" })).toBeUndefined();
|
expect(queryResultNameFromPreamble("# temporary table\n", { databaseType: "sqlserver" })).toBeUndefined();
|
||||||
expect(queryResultNameFromPreamble("# portable comment\nSELECT 1")).toBeUndefined();
|
expect(queryResultNameFromPreamble("# portable comment\n")).toBeUndefined();
|
||||||
|
expect(queryResultNameFromPreamble("# MySQL report\n\n", { databaseType: "mysql" })).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -10,21 +10,19 @@ export interface QueryResultSourceLabelOptions {
|
|||||||
const HASH_COMMENT_DATABASE_TYPES = new Set<DatabaseType>(["mysql"]);
|
const HASH_COMMENT_DATABASE_TYPES = new Set<DatabaseType>(["mysql"]);
|
||||||
|
|
||||||
export function queryResultNameFromPreamble(preamble: string, options: Pick<QueryResultSourceLabelOptions, "databaseType"> = {}): string | undefined {
|
export function queryResultNameFromPreamble(preamble: string, options: Pick<QueryResultSourceLabelOptions, "databaseType"> = {}): string | undefined {
|
||||||
let name: string | undefined;
|
const beforeStatementLine = preamble.replace(/[ \t]*$/, "");
|
||||||
let fallback: string | undefined;
|
const previousLine =
|
||||||
const withoutBlockComments = preamble.replace(/\/\*[\s\S]*?\*\//g, "");
|
beforeStatementLine
|
||||||
for (const line of withoutBlockComments.split(/\r?\n/)) {
|
.replace(/\r?\n$/, "")
|
||||||
const commentMatch = line.match(/^\s*(--|#)\s*(.*)$/);
|
.split(/\r?\n/)
|
||||||
if (commentMatch?.[1] === "#" && !HASH_COMMENT_DATABASE_TYPES.has(options.databaseType!)) continue;
|
.pop() ?? "";
|
||||||
const comment = commentMatch?.[2]?.trim();
|
const commentMatch = previousLine.match(/^\s*(--|#)\s*(.*)$/);
|
||||||
if (!comment) continue;
|
if (commentMatch?.[1] === "#" && !HASH_COMMENT_DATABASE_TYPES.has(options.databaseType!)) return undefined;
|
||||||
|
const comment = commentMatch?.[2]?.trim();
|
||||||
|
if (!comment) return undefined;
|
||||||
|
|
||||||
const nameMatch = comment.match(/^name\s*:\s*(.*)$/i);
|
const nameMatch = comment.match(/^name\s*:\s*(.*)$/i);
|
||||||
const candidate = nameMatch?.[1]?.trim();
|
return nameMatch ? nameMatch[1]?.trim() || undefined : comment;
|
||||||
if (candidate) name = candidate;
|
|
||||||
else if (!nameMatch) fallback = comment;
|
|
||||||
}
|
|
||||||
return name ?? fallback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRowSource["kind"]): SqlSemanticRowSource | undefined {
|
function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRowSource["kind"]): SqlSemanticRowSource | undefined {
|
||||||
|
|||||||
+5
-1
@@ -9088,7 +9088,7 @@ test("query results keep readable table source labels with active database conte
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test("cursor execution uses the preceding comment from the full query draft", async () => {
|
test("cursor execution uses only the immediately preceding comment from the full query draft", async () => {
|
||||||
const restoreStorage = installMemoryStorage();
|
const restoreStorage = installMemoryStorage();
|
||||||
setActivePinia(createPinia());
|
setActivePinia(createPinia());
|
||||||
const connectionStore = useConnectionStore();
|
const connectionStore = useConnectionStore();
|
||||||
@@ -9123,6 +9123,10 @@ test("cursor execution uses the preceding comment from the full query draft", as
|
|||||||
await store.executeTabSql(tabId, selectedSql, { sourceOffset: tab.sql.indexOf(selectedSql) });
|
await store.executeTabSql(tabId, selectedSql, { sourceOffset: tab.sql.indexOf(selectedSql) });
|
||||||
assert.equal(currentSql, selectedSql);
|
assert.equal(currentSql, selectedSql);
|
||||||
assert.equal(tab.result?.sourceLabel, "Orders report");
|
assert.equal(tab.result?.sourceLabel, "Orders report");
|
||||||
|
|
||||||
|
tab.sql = "-- Users report\nSELECT 1;\n\n-- Orders report\n\nSELECT 2;";
|
||||||
|
await store.executeTabSql(tabId, selectedSql, { sourceOffset: tab.sql.indexOf(selectedSql) });
|
||||||
|
assert.equal(tab.result?.sourceLabel, undefined);
|
||||||
} finally {
|
} finally {
|
||||||
globalThis.fetch = originalFetch;
|
globalThis.fetch = originalFetch;
|
||||||
restoreStorage();
|
restoreStorage();
|
||||||
|
|||||||
Reference in new issue
Block a user