feat(query): support custom result names from SQL comments
This commit is contained in:
1 parent
b43480c6bc
commit
086e20f878
4 files changed
+40
-2
No files matched your search
@@ -1,5 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { queryResultSourceLabel } from "@/lib/sql/queryResultSource";
|
||||
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");
|
||||
});
|
||||
|
||||
it("ignores unrelated, malformed, and block comments", () => {
|
||||
expect(queryResultNameFromPreamble("-- Name without colon\n/*\n-- Name: block\n*/\n-- ordinary comment\n")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("queryResultSourceLabel", () => {
|
||||
it("uses the current database for an unqualified table", () => {
|
||||
|
||||
@@ -7,6 +7,16 @@ export interface QueryResultSourceLabelOptions {
|
||||
databaseType?: DatabaseType;
|
||||
}
|
||||
|
||||
export function queryResultNameFromPreamble(preamble: string): string | undefined {
|
||||
let name: string | undefined;
|
||||
const withoutBlockComments = preamble.replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
for (const line of withoutBlockComments.split(/\r?\n/)) {
|
||||
const candidate = line.match(/^\s*--\s*name\s*:\s*(.*)$/i)?.[1]?.trim();
|
||||
if (candidate) name = candidate;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRowSource["kind"]): SqlSemanticRowSource | undefined {
|
||||
return sources.filter((source) => source.kind === kind).sort((left, right) => left.sourceSpan.start - right.sourceSpan.start)[0];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user