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(sql): offer keyword completion on full word match for case conversion

lxk955971a95aa80ae
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 0990abf0f2..2538c11eb5 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
@@ -90,6 +90,147 @@ describe("sqlCompletion keyword snippets", () => {
expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "select *", type: "snippet" }), expect.objectContaining({ label: "SELECT", type: "keyword" })]));
});
+ it("offers uppercase keyword completion when typing full lowercase keywords for case conversion (#8325)", () => {
+ const testCases = [
+ { sql: "select", word: "SELECT" },
+ { sql: "SELECT id from", word: "FROM" },
+ { sql: "SELECT * FROM users where", word: "WHERE" },
+ { sql: "SELECT * FROM users WHERE id = 1 and", word: "AND" },
+ { sql: "SELECT * FROM users WHERE id = 1 or", word: "OR" },
+ { sql: "SELECT * FROM users WHERE id is", word: "IS" },
+ { sql: "SELECT * FROM users WHERE id is null", word: "NULL" },
+ { sql: "SELECT * FROM users WHERE id not", word: "NOT" },
+ { sql: "SELECT * FROM users WHERE id between", word: "BETWEEN" },
+ { sql: "SELECT * FROM users WHERE name like", word: "LIKE" },
+ { sql: "SELECT * FROM users WHERE id in", word: "IN" },
+ { sql: "SELECT * FROM users order", word: "ORDER" },
+ { sql: "SELECT * FROM users ORDER by", word: "BY" },
+ { sql: "SELECT * FROM users group", word: "GROUP" },
+ { sql: "SELECT * FROM users GROUP by", word: "BY" },
+ { sql: "SELECT * FROM users GROUP BY id having", word: "HAVING" },
+ { sql: "SELECT * FROM users limit", word: "LIMIT" },
+ { sql: "SELECT * FROM users LIMIT 10 offset", word: "OFFSET" },
+ { sql: "SELECT * FROM users join", word: "JOIN" },
+ { sql: "SELECT * FROM users inner", word: "INNER" },
+ { sql: "SELECT * FROM users left", word: "LEFT" },
+ { sql: "SELECT * FROM users right", word: "RIGHT" },
+ { sql: "SELECT * FROM users full", word: "FULL" },
+ { sql: "SELECT * FROM users cross", word: "CROSS" },
+ { sql: "SELECT * FROM users JOIN orders on", word: "ON" },
+ { sql: "UPDATE users set", word: "SET" },
+ { sql: "INSERT into", word: "INTO" },
+ { sql: "INSERT INTO users values", word: "VALUES" },
+ { sql: "SELECT distinct", word: "DISTINCT" },
+ { sql: "SELECT id as", word: "AS" },
+ { sql: "SELECT CASE when", word: "WHEN" },
+ { sql: "SELECT CASE WHEN 1 then", word: "THEN" },
+ { sql: "SELECT CASE WHEN 1 THEN 2 else", word: "ELSE" },
+ { sql: "SELECT CASE WHEN 1 THEN 2 ELSE 3 end", word: "END" },
+ { sql: "CREATE table", word: "TABLE" },
+ { sql: "DROP table", word: "TABLE" },
+ { sql: "ALTER table", word: "TABLE" },
+ ];
+
+ for (const { sql, word } of testCases) {
+ const legacy = getSqlCompletionContext(sql, sql.length);
+ const semanticContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(sql, sql.length), legacy);
+ const items = buildSqlCompletionItemsFromContext(semanticContext, {
+ tables: [{ name: "users" }, { name: "orders" }],
+ columnsByTable: new Map([
+ [
+ "users",
+ [
+ { name: "id", table: "users" },
+ { name: "name", table: "users" },
+ ],
+ ],
+ [
+ "orders",
+ [
+ { name: "id", table: "orders" },
+ { name: "user_id", table: "orders" },
+ ],
+ ],
+ ]),
+ keywordCase: "upper",
+ });
+
+ const keywordItem = items.find((item) => item.label === word && item.type === "keyword");
+ expect(keywordItem, `Expected keyword ${word} in completion for "${sql}"`).toBeDefined();
+ expect(keywordItem?.exactMatch, `Expected exactMatch flag for ${word}`).toBe(true);
+ expect(
+ items.slice(0, 2).map((item) => item.label),
+ `Expected ${word} in top suggestions for "${sql}"`,
+ ).toContain(word);
+ }
+ });
+
+ it("offers lowercase keyword completion when typing full uppercase keywords with keywordCase 'lower' (#8325)", () => {
+ const sql = "SELECT * FROM users WHERE";
+ const legacy = getSqlCompletionContext(sql, sql.length);
+ const semanticContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(sql, sql.length), legacy);
+ const items = buildSqlCompletionItemsFromContext(semanticContext, {
+ tables: [{ name: "users" }],
+ columnsByTable: new Map([
+ [
+ "users",
+ [
+ { name: "id", table: "users" },
+ { name: "name", table: "users" },
+ ],
+ ],
+ ]),
+ keywordCase: "lower",
+ });
+
+ const whereItem = items.find((item) => item.label === "where" && item.type === "keyword");
+ expect(whereItem).toBeDefined();
+ expect(whereItem?.exactMatch).toBe(true);
+ expect(items[0]?.label).toBe("where");
+ });
+
+ it("keeps multi-word keywords available alongside individual keywords (#8325)", () => {
+ const orderSql = "SELECT * FROM users order";
+ const orderLegacy = getSqlCompletionContext(orderSql, orderSql.length);
+ const orderContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(orderSql, orderSql.length), orderLegacy);
+ const orderItems = buildSqlCompletionItemsFromContext(orderContext, {
+ tables: [{ name: "users" }],
+ columnsByTable: new Map([
+ [
+ "users",
+ [
+ { name: "id", table: "users" },
+ { name: "name", table: "users" },
+ ],
+ ],
+ ]),
+ keywordCase: "upper",
+ });
+
+ expect(orderItems.some((item) => item.label === "ORDER" && item.type === "keyword")).toBe(true);
+ expect(orderItems.some((item) => item.label === "ORDER BY" && item.type === "keyword")).toBe(true);
+
+ const groupSql = "SELECT * FROM users group";
+ const groupLegacy = getSqlCompletionContext(groupSql, groupSql.length);
+ const groupContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(groupSql, groupSql.length), groupLegacy);
+ const groupItems = buildSqlCompletionItemsFromContext(groupContext, {
+ tables: [{ name: "users" }],
+ columnsByTable: new Map([
+ [
+ "users",
+ [
+ { name: "id", table: "users" },
+ { name: "name", table: "users" },
+ ],
+ ],
+ ]),
+ keywordCase: "upper",
+ });
+
+ expect(groupItems.some((item) => item.label === "GROUP" && item.type === "keyword")).toBe(true);
+ expect(groupItems.some((item) => item.label === "GROUP BY" && item.type === "keyword")).toBe(true);
+ });
+
it("offers DuckDB-specific query and statement keywords", () => {
const expectedKeywords = ["QUALIFY", "SUMMARIZE", "PIVOT", "UNPIVOT", "ASOF", "POSITIONAL", "FROM"];
diff --git a/apps/desktop/src/lib/sql/semantic/model.ts b/apps/desktop/src/lib/sql/semantic/model.ts
index 8d4b207e6a..85cca23b8b 100644
--- a/apps/desktop/src/lib/sql/semantic/model.ts
+++ b/apps/desktop/src/lib/sql/semantic/model.ts
@@ -1026,20 +1026,17 @@ function buildCursorIntent(tokens: readonly SqlSemanticToken[], cursor: number,
return { kind: "alias_column", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, targetSourceId: targetSource.id, expectedObjectKinds: ["column"], confidence: "high" };
}
- // A table introducer only introduces the slot that follows it, so both checks read
- // the word before the identifier being completed (wordBeforeTrailing) or before its
- // whole qualified name (wordBeforeReplacement). `previous` must not be used here: it
- // is the word at the cursor, so typing `update`/`from`/`join` in full made the
- // keyword introduce its own slot and dropped its own completion (issue #10415).
if (TABLE_INTRODUCERS.has(wordBeforeReplacement) || TABLE_INTRODUCERS.has(wordBeforeTrailing) || tableListContinuation) {
return { kind: "table", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, expectedObjectKinds: ["table", "view"], confidence: "high" };
}
- if (previous === "call" || previous === "exec" || previous === "execute") {
+ const routineIntroducer = wordBeforeTrailing || wordBeforeReplacement;
+ if (routineIntroducer === "call" || routineIntroducer === "exec" || routineIntroducer === "execute") {
return { kind: "routine", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, expectedObjectKinds: ["routine", "procedure", "function"], confidence: "high" };
}
- if (previous === "set") {
+ const updateColumnIntroducer = wordBeforeTrailing || wordBeforeReplacement;
+ if (updateColumnIntroducer === "set") {
return {
kind: "update_column",
prefix: trailing.prefix,
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts
index 70d9fadeca..f59c9d2003 100644
--- a/apps/desktop/src/lib/sql/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts
@@ -42,6 +42,9 @@ const SQL_KEYWORDS = [
"INNER",
"OUTER",
"ON",
+ "GROUP",
+ "ORDER",
+ "BY",
"GROUP BY",
"ORDER BY",
"ASC",
@@ -301,6 +304,9 @@ const COMMON_SQL_KEYWORDS = [
"INNER",
"OUTER",
"ON",
+ "GROUP",
+ "ORDER",
+ "BY",
"GROUP BY",
"ORDER BY",
"ASC",
@@ -724,6 +730,9 @@ const HIGH_FREQUENCY_KEYWORDS = new Set([
"ON",
"IN",
"AS",
+ "GROUP",
+ "ORDER",
+ "BY",
"GROUP BY",
"ORDER BY",
"LEFT",
@@ -5764,7 +5773,7 @@ function buildKeywordPrefixContinuationItems(prefix: string, context: SqlComplet
const normalizedPrefix = prefix.toLowerCase();
return buildKeywordItems(prefix, context, databaseType, keywordCase).filter((item) => {
const normalizedLabel = item.label.toLowerCase();
- return normalizedLabel.length > normalizedPrefix.length && normalizedLabel.startsWith(normalizedPrefix);
+ return (normalizedLabel.length > normalizedPrefix.length && normalizedLabel.startsWith(normalizedPrefix)) || (normalizedLabel === normalizedPrefix && item.label !== prefix);
});
}