fix(editor): complete MySQL date functions

This commit is contained in:
zipg 2026-07-13 17:16:44 +08:00 committed by GitHub
parent 16724074c0
commit 8c11f2efaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

View File

@ -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".';

View File

@ -883,6 +883,8 @@ const POSTGRES_FUNCTION_SIGNATURES = new Map<string, string[]>([
const MYSQL_FUNCTION_SIGNATURES = new Map<string, string[]>([
["DATE_FORMAT", ["date", "format"]],
["FROM_UNIXTIME", ["unix_timestamp"]],
["UNIX_TIMESTAMP", []],
["JSON_EXTRACT", ["json", "path"]],
["JSON_UNQUOTE", ["json"]],
["GROUP_CONCAT", ["expression"]],