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): backtick all mysql reserved-word columns

Andreas Kristiansen614463d713b0
diff --git a/apps/desktop/src/lib/__tests__/sql/mysqlReservedIdentifierQuoting.spec.ts b/apps/desktop/src/lib/__tests__/sql/mysqlReservedIdentifierQuoting.spec.ts
new file mode 100644
index 0000000000..e20890a404
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/sql/mysqlReservedIdentifierQuoting.spec.ts
@@ -0,0 +1,85 @@
+import { describe, expect, it } from "vitest";
+import { buildSqlCompletionItems, prepareSqlCompletionReplacement } from "@/lib/sql/sqlCompletion";
+import { quoteTableIdentifierIfNeeded } from "@/lib/table/tableSelectSql";
+
+// MySQL reserved words used as column names must arrive backticked — from
+// completion applies and from generated data-tab SQL alike. These specs pin
+// the reserved-word list in sqlIdentifier.ts against the reported scenario
+// (a `call` column on MySQL completing bare and failing to parse).
+const mysqlInput = {
+ databaseType: "mysql" as const,
+ tables: [{ name: "dxcc_exceptions", type: "table" as const }],
+ columnsByTable: new Map([
+ [
+ "dxcc_exceptions",
+ [
+ { name: "call", table: "dxcc_exceptions" },
+ { name: "status", table: "dxcc_exceptions" },
+ ],
+ ],
+ ]),
+};
+
+describe("mysql reserved-word column completion quoting", () => {
+ it("backticks a `call` column suggested after WHERE", () => {
+ const sql = "SELECT * FROM dxcc_exceptions WHERE call";
+ const items = buildSqlCompletionItems(sql, sql.length, mysqlInput);
+ const column = items.find((item) => item.type === "column" && item.label === "call");
+ expect(column?.apply).toBe("`call`");
+ });
+
+ it.each(["set", "update", "condition", "read", "system", "lead"] as const)("backticks the reserved word %s used as a column", (word) => {
+ const sql = `SELECT * FROM dxcc_exceptions WHERE ${word}`;
+ const items = buildSqlCompletionItems(sql, sql.length, {
+ ...mysqlInput,
+ columnsByTable: new Map([["dxcc_exceptions", [{ name: word, table: "dxcc_exceptions" }]]]),
+ });
+ expect(items.find((item) => item.type === "column" && item.label === word)?.apply).toBe(`\`${word}\``);
+ });
+
+ it("keeps a non-reserved column suggestion bare", () => {
+ const sql = "SELECT * FROM dxcc_exceptions WHERE status";
+ const items = buildSqlCompletionItems(sql, sql.length, mysqlInput);
+ expect(items.find((item) => item.type === "column" && item.label === "status")?.apply).toBe("status");
+ });
+
+ it("backticks the column of an alias-qualified suggestion without touching the typed qualifier", () => {
+ const sql = "SELECT * FROM dxcc_exceptions d WHERE d.call";
+ const items = buildSqlCompletionItems(sql, sql.length, mysqlInput);
+ expect(items.find((item) => item.type === "column" && item.label === "call")?.apply).toBe("`call`");
+ });
+
+ it("keeps `call` bare on postgres, where CALL is not reserved", () => {
+ const sql = "SELECT * FROM dxcc_exceptions WHERE call";
+ const items = buildSqlCompletionItems(sql, sql.length, { ...mysqlInput, databaseType: "postgres" as const });
+ expect(items.find((item) => item.type === "column" && item.label === "call")?.apply).toBe("call");
+ });
+
+ it("backticks the mysql-only reserved words trigger and while", () => {
+ const columns = [
+ { name: "trigger", table: "dxcc_exceptions" },
+ { name: "while", table: "dxcc_exceptions" },
+ ];
+ const input = { ...mysqlInput, columnsByTable: new Map([["dxcc_exceptions", columns]]) };
+ for (const name of ["trigger", "while"]) {
+ const sql = `SELECT * FROM dxcc_exceptions WHERE ${name}`;
+ const items = buildSqlCompletionItems(sql, sql.length, input);
+ expect(items.find((item) => item.type === "column" && item.label === name)?.apply, name).toBe(`\`${name}\``);
+ }
+ });
+});
+
+describe("mysql reserved-word quoting beyond completion", () => {
+ it("backticks reserved columns in generated data-tab SQL and keeps plain ones bare", () => {
+ expect(quoteTableIdentifierIfNeeded("mysql", "call")).toBe("`call`");
+ expect(quoteTableIdentifierIfNeeded("mysql", "status")).toBe("status");
+ });
+
+ it("does not double-wrap a backticked apply when the user already typed the opening backtick", () => {
+ const sql = "SELECT * FROM dxcc_exceptions WHERE `ca";
+ const cursor = sql.length;
+ const prepared = prepareSqlCompletionReplacement(sql, cursor, { prefix: "ca", qualifier: undefined, replacementRange: { start: sql.indexOf("`"), end: cursor }, selectListWildcardAfterCursor: undefined }, [{ label: "call", type: "column", apply: "`call`", boost: 1 }]);
+ expect(prepared.from).toBe(sql.indexOf("`"));
+ expect(prepared.items[0]?.apply).toBe("`call`");
+ });
+});
diff --git a/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts b/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
index a28d18fa36..09c8ce1b47 100644
--- a/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
+++ b/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
@@ -178,10 +178,13 @@ describe("ddlFormatDialectFor", () => {
});
it("keeps case-sensitive quoted Oracle names quoted in the DDL view", () => {
- const ddl = 'CREATE TABLE "SYSTEM"."TEST" ("id" VARCHAR2(32), "cName" VARCHAR2(100))';
+ // MYDB is reserved in no dialect: the old "SYSTEM" fixture schema is on the
+ // MySQL reserved list, so mysql-highlighted omitDdlIdentifierQuotes kept it
+ // quoted and broke the case-sensitivity expectation this test verifies.
+ const ddl = 'CREATE TABLE "MYDB"."TEST" ("id" VARCHAR2(32), "cName" VARCHAR2(100))';
const highlightDialect = "mysql";
const resolved = ddlFormatDialectFor({ databaseType: "oracle", highlightDialect });
- expect(omitDdlIdentifierQuotes(ddl, highlightDialect)).toBe("CREATE TABLE SYSTEM.TEST (id VARCHAR2(32), cName VARCHAR2(100))");
- expect(omitDdlIdentifierQuotes(ddl, resolved)).toBe('CREATE TABLE SYSTEM.TEST ("id" VARCHAR2(32), "cName" VARCHAR2(100))');
+ expect(omitDdlIdentifierQuotes(ddl, highlightDialect)).toBe("CREATE TABLE MYDB.TEST (id VARCHAR2(32), cName VARCHAR2(100))");
+ expect(omitDdlIdentifierQuotes(ddl, resolved)).toBe('CREATE TABLE MYDB.TEST ("id" VARCHAR2(32), "cName" VARCHAR2(100))');
});
});
diff --git a/apps/desktop/src/lib/sql/sqlIdentifier.ts b/apps/desktop/src/lib/sql/sqlIdentifier.ts
index cf51ecc46f..385d7c6c5f 100644
--- a/apps/desktop/src/lib/sql/sqlIdentifier.ts
+++ b/apps/desktop/src/lib/sql/sqlIdentifier.ts
@@ -105,72 +105,198 @@ const POSTGRES_RESERVED_IDENTIFIER_KEYWORDS = new Set([
"verbose",
"when",
"where",
+ "while",
"window",
"with",
]);
// GaussDB MySQL-compatible drivers report a backtick. These additional words
-// cover MySQL-only names that cannot safely be emitted as bare identifiers.
+// cover MySQL-only names that cannot safely be emitted as bare identifiers —
+// the full MySQL reserved-word list (MySQL 8.4 manual, keywords marked (R))
+// minus the PostgreSQL set above, so the union matches MySQL exactly.
const MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS = new Set([
+ "_filename",
"accessible",
+ "add",
+ "alter",
+ "asensitive",
"auto_increment",
+ "before",
+ "between",
+ "bigint",
+ "blob",
+ "by",
+ "call",
+ "cascade",
"change",
+ "char",
+ "character",
+ "condition",
+ "continue",
+ "convert",
+ "cube",
+ "cume_dist",
+ "cursor",
"database",
"databases",
+ "day_hour",
+ "day_microsecond",
+ "day_minute",
+ "day_second",
+ "dec",
+ "decimal",
+ "declare",
"delayed",
+ "delete",
+ "dense_rank",
"describe",
+ "deterministic",
+ "distinctrow",
"div",
+ "double",
+ "drop",
"dual",
+ "each",
+ "elseif",
+ "empty",
"enclosed",
"escaped",
"explain",
+ "exists",
+ "exit",
+ "first_value",
+ "float",
+ "float4",
+ "float8",
"force",
"fulltext",
+ "function",
+ "generated",
+ "get",
+ "grouping",
+ "groups",
"high_priority",
+ "hour_microsecond",
+ "hour_minute",
+ "hour_second",
+ "if",
"ignore",
"index",
"infile",
+ "inout",
+ "insensitive",
+ "insert",
+ "int",
+ "int1",
+ "int2",
+ "int3",
+ "int4",
+ "int8",
+ "integer",
+ "interval",
+ "io_after_gtids",
+ "io_before_gtids",
+ "iterate",
+ "json_table",
"key",
"keys",
"kill",
+ "lag",
+ "last_value",
+ "lead",
+ "leave",
"linear",
"lines",
"load",
"lock",
+ "long",
+ "longblob",
+ "longtext",
+ "loop",
"low_priority",
"master_ssl_verify_server_cert",
+ "match",
"maxvalue",
+ "mediumblob",
"mediumint",
+ "mediumtext",
+ "middleint",
+ "minute_microsecond",
+ "minute_second",
"mod",
+ "modifies",
"no_write_to_binlog",
+ "nth_value",
+ "ntile",
+ "numeric",
+ "of",
"optimize",
+ "optimizer_costs",
+ "option",
"optionally",
+ "out",
"outfile",
+ "over",
"partition",
+ "percent_rank",
+ "precision",
+ "procedure",
"purge",
+ "qualify",
"range",
+ "rank",
+ "read",
"read_write",
+ "reads",
+ "real",
+ "recursive",
"regexp",
"release",
"rename",
+ "repeat",
"replace",
"require",
+ "resignal",
+ "restrict",
+ "return",
+ "revoke",
"rlike",
+ "row",
+ "row_number",
+ "rows",
"schema",
"schemas",
+ "second_microsecond",
+ "sensitive",
"separator",
+ "set",
"show",
+ "signal",
+ "smallint",
"spatial",
+ "specific",
+ "sql",
"sql_big_result",
"sql_calc_found_rows",
"sql_small_result",
+ "sqlexception",
+ "sqlstate",
+ "sqlwarning",
"ssl",
"starting",
+ "stored",
"straight_join",
+ "system",
"terminated",
+ "tinyblob",
"tinyint",
+ "tinytext",
+ "trigger",
+ "undo",
"unlock",
"unsigned",
+ "update",
+ "usage",
"use",
"utc_date",
"utc_time",
@@ -178,8 +304,12 @@ const MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS = new Set([
"values",
"varbinary",
"varchar",
+ "varcharacter",
+ "varying",
+ "virtual",
"write",
"xor",
+ "year_month",
"zerofill",
]);