From 8c11f2efaaff8d2405ef09d1800df2a9c45e1f19 Mon Sep 17 00:00:00 2001 From: zipg Date: Mon, 13 Jul 2026 17:16:44 +0800 Subject: [PATCH] fix(editor): complete MySQL date functions --- .../sql/sqlCompletion.context.spec.ts | 58 +++++++++++++++++++ apps/desktop/src/lib/sql/sqlCompletion.ts | 2 + 2 files changed, 60 insertions(+) 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 dd414139c..b460b6473 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts @@ -14,6 +14,64 @@ describe("sqlCompletion keyword snippets", () => { }); }); +describe("sqlCompletion database functions", () => { + it("suggests MySQL Unix timestamp functions with function snippets", () => { + const fromUnixSql = "SELECT from_unix"; + const fromUnixItems = buildSqlCompletionItems(fromUnixSql, fromUnixSql.length, { + databaseType: "mysql", + tables: [], + columnsByTable: new Map(), + }); + const fromUnixTime = fromUnixItems.find((item) => item.label === "FROM_UNIXTIME"); + + expect(fromUnixItems[0]).toBe(fromUnixTime); + expect(fromUnixTime).toEqual( + expect.objectContaining({ + type: "function", + apply: "FROM_UNIXTIME(${unix_timestamp})", + }), + ); + + const unixTimestampSql = "SELECT unix_time"; + const unixTimestampItems = buildSqlCompletionItems(unixTimestampSql, unixTimestampSql.length, { + databaseType: "mysql", + tables: [], + columnsByTable: new Map(), + }); + + expect(unixTimestampItems[0]).toEqual( + expect.objectContaining({ + label: "UNIX_TIMESTAMP", + type: "function", + apply: "UNIX_TIMESTAMP()", + }), + ); + }); + + it("ranks MySQL function prefixes ahead of ordinary keyword prefixes", () => { + const sql = "SELECT uni"; + const items = buildSqlCompletionItems(sql, sql.length, { + databaseType: "mysql", + tables: [], + columnsByTable: new Map(), + }); + + expect(items.some((item) => item.type === "keyword")).toBe(true); + expect(items[0]).toEqual(expect.objectContaining({ label: "UNIX_TIMESTAMP", type: "function" })); + }); + + it("does not expose MySQL-only functions to other databases", () => { + const sql = "SELECT from_unix"; + const items = buildSqlCompletionItems(sql, sql.length, { + databaseType: "postgres", + tables: [], + columnsByTable: new Map(), + }); + + expect(items.some((item) => item.label === "FROM_UNIXTIME")).toBe(false); + }); +}); + describe("sqlCompletion quoted schema qualifiers", () => { it("parses quoted PostgreSQL schema names before a dot", () => { const sql = 'SELECT *\nFROM "order-management".'; diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index 3212bf27c..ae1de8152 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -883,6 +883,8 @@ const POSTGRES_FUNCTION_SIGNATURES = new Map([ const MYSQL_FUNCTION_SIGNATURES = new Map([ ["DATE_FORMAT", ["date", "format"]], + ["FROM_UNIXTIME", ["unix_timestamp"]], + ["UNIX_TIMESTAMP", []], ["JSON_EXTRACT", ["json", "path"]], ["JSON_UNQUOTE", ["json"]], ["GROUP_CONCAT", ["expression"]],