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(sqlserver): highlight SET as a keyword

s1oopX2529737a640e
diff --git a/apps/desktop/src/lib/__tests__/editor/codemirrorSqlDialect.spec.ts b/apps/desktop/src/lib/__tests__/editor/codemirrorSqlDialect.spec.ts
index c83a001f56..1907c383b0 100644
--- a/apps/desktop/src/lib/__tests__/editor/codemirrorSqlDialect.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editor/codemirrorSqlDialect.spec.ts
@@ -1,8 +1,18 @@
import { describe, expect, it } from "vitest";
import * as langSql from "@codemirror/lang-sql";
-import { createDbxCodeMirrorSqlDialect, postgresKeywordSyntaxTerms } from "@/lib/editor/codemirrorSqlDialect";
+import { createDbxCodeMirrorSqlDialect, postgresKeywordSyntaxTerms, sqlServerBuiltinSyntaxTerms } from "@/lib/editor/codemirrorSqlDialect";
import type { DatabaseType } from "@/types/database";
+function nodeNameAt(dialect: langSql.SQLDialect, statement: string, word: string): string | undefined {
+ const cursor = dialect.language.parser.parse(statement).cursor();
+
+ do {
+ if (statement.slice(cursor.from, cursor.to) === word) return cursor.name;
+ } while (cursor.next());
+
+ return undefined;
+}
+
describe("codemirrorSqlDialect", () => {
it("keeps common PostgreSQL identifier names out of keyword highlighting", () => {
const keywords = new Set(postgresKeywordSyntaxTerms(langSql.PostgreSQL.spec.keywords || "").split(/\s+/));
@@ -35,6 +45,29 @@ describe("codemirrorSqlDialect", () => {
expect(mysqlBuiltins.has("dolt_branch")).toBe(false);
});
+ it("highlights SQL Server clause words as keywords instead of builtin functions", () => {
+ const builtins = new Set(sqlServerBuiltinSyntaxTerms(langSql.MSSQL.spec.builtin || "").split(/\s+/));
+
+ expect(builtins.has("set")).toBe(false);
+ expect(builtins.has("next")).toBe(false);
+ expect(builtins.has("for")).toBe(false);
+ expect(builtins.has("getdate")).toBe(true);
+ expect(builtins.has("count")).toBe(true);
+ expect(builtins.has("left")).toBe(true);
+ });
+
+ it("tokenizes SET as a keyword for SQL Server statements", () => {
+ const dialect = createDbxCodeMirrorSqlDialect(langSql, "sqlserver", "sqlserver");
+ const statements = ["UPDATE users SET name = 'Alice' WHERE id = 1", "SET NOCOUNT ON", "SET @total = 5"];
+
+ for (const statement of statements) {
+ expect(nodeNameAt(dialect, statement, "SET"), statement).toBe("Keyword");
+ }
+
+ expect(nodeNameAt(dialect, "SELECT COUNT(*) FROM users", "COUNT")).toBe("Builtin");
+ expect(nodeNameAt(dialect, "SELECT GETDATE()", "GETDATE")).toBe("Builtin");
+ });
+
it("keeps double quotes as identifier delimiters for Oracle-family dialects", () => {
const databaseTypes: DatabaseType[] = ["oracle", "dameng", "yashandb", "oscar", "oceanbase-oracle"];
diff --git a/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts b/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts
index aa6f92a0e6..b7de10824d 100644
--- a/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts
+++ b/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts
@@ -55,6 +55,21 @@ const POSTGRES_IDENTIFIER_LIKE_KEYWORDS = new Set("COMMENT COUNT DATA DAY HOUR I
// SQL Server table-valued parameters require READONLY in procedure/function declarations.
const SQLSERVER_KEYWORDS = "readonly";
+// CodeMirror's MSSQL builtin list registers a few T-SQL clause words as functions:
+// `set` arrives with the query hint terms, while `next`/`for` come from the
+// `NEXT VALUE FOR` sequence expression being split into single words. Builtin terms are
+// applied after keywords when the dialect vocabulary is built, so those entries shadow the
+// reserved-keyword highlighting for statements like `UPDATE ... SET` and `SET NOCOUNT ON`.
+// None of them is callable on its own, so drop them and let the keyword classification win.
+const SQLSERVER_NON_FUNCTION_BUILTIN_TERMS = new Set(["set", "next", "for"]);
+
+export function sqlServerBuiltinSyntaxTerms(builtin: string): string {
+ return builtin
+ .split(/\s+/)
+ .filter((term) => term && !SQLSERVER_NON_FUNCTION_BUILTIN_TERMS.has(term.toLowerCase()))
+ .join(" ");
+}
+
const CLICKHOUSE_KEYWORDS = [
"ATTACH",
"DETACH",
@@ -216,12 +231,13 @@ export function createDbxCodeMirrorSqlDialect(langSql: CodeMirrorSqlLanguageModu
const baseKeywords = isClickHouse ? standardSqlKeywordSyntaxTerms(langSql) : isPostgres ? postgresKeywordSyntaxTerms(baseDialect.spec.keywords || "") : baseDialect.spec.keywords || "";
const baseTypes = isClickHouse ? STANDARD_SQL_TYPES : baseDialect.spec.types || "";
const commonKeywords = isClickHouse ? DBX_COMMON_SQL_KEYWORDS.toLowerCase() : DBX_COMMON_SQL_KEYWORDS;
+ const baseBuiltin = isSqlServer ? sqlServerBuiltinSyntaxTerms(baseDialect.spec.builtin || "") : baseDialect.spec.builtin || "";
return langSql.SQLDialect.define({
...baseDialect.spec,
keywords: [baseKeywords, commonKeywords, isClickHouse ? CLICKHOUSE_KEYWORDS : "", isPostgres ? POSTGRES_PLPGSQL_KEYWORDS : "", isSqlServer ? SQLSERVER_KEYWORDS : ""].filter(Boolean).join(" "),
types: [baseTypes, isClickHouse ? CLICKHOUSE_TYPES : "", isPostgres ? POSTGRES_PLPGSQL_TYPES : ""].filter(Boolean).join(" ") || undefined,
- builtin: [baseDialect.spec.builtin || "", isClickHouse ? CLICKHOUSE_BUILTINS : "", isPostgres ? `${POSTGRES_BUILTINS} ${POSTGRES_PLPGSQL_BUILTIN}` : "", isMysql ? MYSQL_BUILTINS : "", driverProfileSqlBuiltinTerms(driverProfile)].filter(Boolean).join(" ") || undefined,
+ builtin: [baseBuiltin, isClickHouse ? CLICKHOUSE_BUILTINS : "", isPostgres ? `${POSTGRES_BUILTINS} ${POSTGRES_PLPGSQL_BUILTIN}` : "", isMysql ? MYSQL_BUILTINS : "", driverProfileSqlBuiltinTerms(driverProfile)].filter(Boolean).join(" ") || undefined,
...(isClickHouse
? {
identifierQuotes: '"`',