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。

fix(sql): suggest columns inside select-list function calls

Y.B.26e6db114ad0
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
index 27dcf6fcbe..e3daa75a0f 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
@@ -1815,3 +1815,32 @@ describe("line block statement boundary", () => {
expect(context.referencedTables.map((table) => table.name)).toEqual(expect.arrayContaining(["users"]));
});
});
+
+describe("select-list function argument completion", () => {
+ const options = {
+ databaseType: "mysql" as const,
+ tables: [{ name: "daily_statistic", type: "table" as const }],
+ columnsByTable: new Map([["daily_statistic", ["CNKI_CITATIONS", "CNKI_DOWNLOADS", "stat_date"].map((name) => ({ name, table: "daily_statistic" }))]]),
+ };
+
+ const aggregateTemplates = ["SELECT\n SUM(CNKI_CITATIONS) AS cnkiCitations,\n SUM(CNKI_DOW|) AS cnkiDownloads\nFROM\n daily_statistic", "SELECT stat_date, CNKI_CITATIONS, COUNT(CNKI_DOW|) FROM daily_statistic", "SELECT stat_date, CNKI_CITATIONS, ROUND(AVG(CNKI_DOW|), 2) FROM daily_statistic"];
+
+ it.each(aggregateTemplates)("suggests columns inside an aggregate after earlier projections: %s", (template) => {
+ const cursor = template.indexOf("|");
+ const sql = template.replace("|", "");
+ const items = buildSqlCompletionItems(sql, cursor, options);
+
+ expect(items.filter((item) => item.type === "column").map((item) => item.label)).toContain("CNKI_DOWNLOADS");
+ });
+
+ it("does not offer select-list-only items inside an aggregate", () => {
+ const sql = "SELECT stat_date, CNKI_CITATIONS, SUM() FROM daily_statistic";
+ const cursor = sql.indexOf("SUM(") + "SUM(".length;
+ const items = buildSqlCompletionItems(sql, cursor, options);
+
+ expect(items.filter((item) => item.type === "column").map((item) => item.label)).toContain("CNKI_DOWNLOADS");
+ expect(items.some((item) => item.type === "snippet" && item.label === "daily_statistic.*")).toBe(false);
+ expect(items.filter((item) => item.type === "column").every((item) => item.batchSelectionMode === undefined)).toBe(true);
+ expect(getSqlCompletionContext(sql, cursor, options).selectListColumnContext).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts
index 6c95baf7d9..5f050c931d 100644
--- a/apps/desktop/src/lib/sql/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts
@@ -2558,8 +2558,9 @@ export function getSqlCompletionContext(sql: string, cursor: number, options: Sq
}
// Check if we're in a context where columns are expected
- const selectListColumnContext = isInSelectListContext(beforeCursor);
- const inColumnContext = selectListColumnContext || isInColumnContext(beforeCursor, options.databaseType) || !!insertInfo;
+ const selectListScan = scanSelectListContext(beforeCursor);
+ const selectListColumnContext = selectListScan.inSelectList;
+ const inColumnContext = selectListColumnContext || selectListScan.inSelectListParens || isInColumnContext(beforeCursor, options.databaseType) || !!insertInfo;
const inJoinConditionContext = isInJoinConditionContext(beforeCursor);
const prioritizeSelectAliases = isInOrderOrGroupByContext(beforeCursor, options.databaseType);
const inCallRoutineContext = isCallRoutineContext(beforeCursor);
@@ -2884,11 +2885,21 @@ function isInColumnContext(beforeCursor: string, databaseType?: DatabaseType): b
}
function isInSelectListContext(beforeCursor: string): boolean {
+ return scanSelectListContext(beforeCursor).inSelectList;
+}
+
+/**
+ * `inSelectList` is true when the cursor sits directly in an open SELECT list.
+ * `inSelectListParens` is true when it sits inside parentheses nested in one,
+ * such as a function argument: `SELECT a, SUM(|`.
+ */
+function scanSelectListContext(beforeCursor: string): { inSelectList: boolean; inSelectListParens: boolean } {
let depth = 0;
let inSingleQuote = false;
let inDoubleQuote = false;
let inBacktick = false;
const selectOpenByDepth = new Map<number, boolean>();
+ const nestedInSelectByDepth = new Map<number, boolean>();
for (let i = 0; i < beforeCursor.length; i++) {
const ch = beforeCursor[i] ?? "";
@@ -2930,11 +2941,14 @@ function isInSelectListContext(beforeCursor: string): boolean {
continue;
}
if (ch === "(") {
+ const inheritsSelectList = selectOpenByDepth.get(depth) === true || nestedInSelectByDepth.get(depth) === true;
depth++;
+ nestedInSelectByDepth.set(depth, inheritsSelectList);
continue;
}
if (ch === ")") {
selectOpenByDepth.delete(depth);
+ nestedInSelectByDepth.delete(depth);
depth = Math.max(0, depth - 1);
continue;
}
@@ -2945,13 +2959,15 @@ function isInSelectListContext(beforeCursor: string): boolean {
const word = beforeCursor.slice(i, end).toLowerCase();
if (word === "select") {
selectOpenByDepth.set(depth, true);
+ nestedInSelectByDepth.delete(depth);
} else if (word === "from") {
selectOpenByDepth.set(depth, false);
+ nestedInSelectByDepth.delete(depth);
}
i = end - 1;
}
- return selectOpenByDepth.get(depth) === true;
+ return { inSelectList: selectOpenByDepth.get(depth) === true, inSelectListParens: nestedInSelectByDepth.get(depth) === true };
}
function isInJoinConditionContext(beforeCursor: string): boolean {