Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(query): name result tabs from SQL comments

Evee561bd156f89
diff --git a/apps/desktop/src/lib/__tests__/sql/queryResultSource.spec.ts b/apps/desktop/src/lib/__tests__/sql/queryResultSource.spec.ts
index 9e0d1958b0..c153c5ecdc 100644
--- a/apps/desktop/src/lib/__tests__/sql/queryResultSource.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/queryResultSource.spec.ts
@@ -5,10 +5,12 @@ 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("ignores unrelated, malformed, and block comments", () => {
- expect(queryResultNameFromPreamble("-- Name without colon\n/*\n-- Name: block\n*/\n-- ordinary comment\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();
});
});
diff --git a/apps/desktop/src/lib/sql/queryResultSource.ts b/apps/desktop/src/lib/sql/queryResultSource.ts
index f221ff0802..9afd9429c5 100644
--- a/apps/desktop/src/lib/sql/queryResultSource.ts
+++ b/apps/desktop/src/lib/sql/queryResultSource.ts
@@ -9,12 +9,18 @@ export interface QueryResultSourceLabelOptions {
export function queryResultNameFromPreamble(preamble: string): string | undefined {
let name: string | undefined;
+ let fallback: 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();
+ const comment = line.match(/^\s*--\s*(.*)$/)?.[1]?.trim();
+ if (!comment) continue;
+
+ const nameMatch = comment.match(/^name\s*:\s*(.*)$/i);
+ const candidate = nameMatch?.[1]?.trim();
if (candidate) name = candidate;
+ else if (!nameMatch) fallback = comment;
}
- return name;
+ return name ?? fallback;
}
function firstSourceOfKind(sources: SqlSemanticRowSource[], kind: SqlSemanticRowSource["kind"]): SqlSemanticRowSource | undefined {