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(completion): rank CREATE OR REPLACE keyword over REPLACE function

zipg4ed3662da22a
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 238f641f9b..0990abf0f2 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts
@@ -51,6 +51,34 @@ describe("SQL completion replacement", () => {
});
describe("sqlCompletion keyword snippets", () => {
+ it.each(["mysql", "postgres", "oracle"] as const)("offers the REPLACE keyword in %s CREATE OR statements", (databaseType) => {
+ for (const text of ["CREATE OR ", "CREATE OR r", "CREATE OR repla", "-- heading\nCREATE /* comment */ OR repla"]) {
+ const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType });
+ expect(items[0], text).toMatchObject({ label: "REPLACE", type: "keyword" });
+ expect(items[0].apply ?? items[0].label).toBe("REPLACE");
+ }
+ });
+
+ it("preserves REPLACE function completion in SELECT expressions", () => {
+ const text = "SELECT repla";
+ const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType: "mysql" });
+ expect(items[0]).toMatchObject({ label: "REPLACE", type: "function" });
+ expect(items[0]?.apply).toMatch(/^REPLACE\(/);
+ });
+
+ it.each(["sqlserver", "sqlite"] as const)("does not suggest unsupported CREATE OR REPLACE syntax on %s", (databaseType) => {
+ const text = "CREATE OR repla";
+ const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType });
+ expect(items.some((item) => item.type === "keyword" && item.label === "REPLACE")).toBe(false);
+ });
+
+ it("honors lower-case keyword preferences for CREATE OR REPLACE", () => {
+ const text = "create or repla";
+ const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType: "postgres", keywordCase: "lower" });
+ expect(items[0]).toMatchObject({ label: "replace", type: "keyword" });
+ expect(items[0].apply ?? items[0].label).toBe("replace");
+ });
+
it("auto-opens and suggests SELECT when typing sel", () => {
const sql = "sel";
const items = buildSqlCompletionItems(sql, sql.length, {
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts
index f60e2165f7..cd3a1e2496 100644
--- a/apps/desktop/src/lib/sql/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts
@@ -3213,6 +3213,9 @@ function preferredKeywordsForCompletion(
databaseType: DatabaseType | undefined,
): string[] {
const keywords: string[] = [];
+ if (databaseType !== "sqlserver" && databaseType !== "sqlite" && /^create\s+or$/i.test(maskSqlLiteralsAndComments(beforeToken, databaseType).trim())) {
+ keywords.push("REPLACE");
+ }
if (selectListColumnContext && hasSelectListExpression(beforeCursor, databaseType)) keywords.push("FROM");
if (isAfterJoinModifierContext(beforeCursor, databaseType)) keywords.push("JOIN");
if (!exclusiveTableSuggestions && isAfterSelectBodyExpression(beforeToken, databaseType)) keywords.push("LIMIT");
diff --git a/docs/screenshots/issue-10596-accepted.jpg b/docs/screenshots/issue-10596-accepted.jpg
new file mode 100644
index 0000000000..81f6451057
Binary files /dev/null and b/docs/screenshots/issue-10596-accepted.jpg differ
diff --git a/docs/screenshots/issue-10596-after.jpg b/docs/screenshots/issue-10596-after.jpg
new file mode 100644
index 0000000000..03baa99498
Binary files /dev/null and b/docs/screenshots/issue-10596-after.jpg differ
diff --git a/docs/screenshots/issue-10596-before.jpg b/docs/screenshots/issue-10596-before.jpg
new file mode 100644
index 0000000000..2630a3d03c
Binary files /dev/null and b/docs/screenshots/issue-10596-before.jpg differ