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(sql): support brace placeholders in formatter

vrustxc80313eed2be
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts
index b68414d52a..3d01816847 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts
@@ -14,6 +14,18 @@ describe("sqlFormatter", () => {
}
});
+ it("preserves DBX brace placeholders in generic and MySQL SQL", async () => {
+ const sql = "SELECT ${x} AS shell_value, #{x} AS mybatis_value, '${date}' AS quoted_value";
+
+ for (const dialect of ["generic", "mysql"] as const) {
+ const formatted = await formatSqlText(sql, dialect);
+
+ expect(formatted).toContain("${x}");
+ expect(formatted).toContain("#{x}");
+ expect(formatted).toContain("'${date}'");
+ }
+ });
+
it("falls back to the postgres formatter when the generic dialect cannot parse SQL", async () => {
const formatted = await formatSqlText("SELECT 1::int AS id;", "generic");
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlFormatterConfig.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlFormatterConfig.spec.ts
index 92314fb7db..8739d23c14 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlFormatterConfig.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlFormatterConfig.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { DEFAULT_SQL_FORMATTER_SETTINGS, parseSqlFormatterConfig, serializeSqlFormatterConfig } from "@/lib/sql/sqlFormatterConfig";
+import { DEFAULT_SQL_FORMATTER_SETTINGS, parseSqlFormatterConfig, serializeSqlFormatterConfig, sqlFormatterOptions } from "@/lib/sql/sqlFormatterConfig";
describe("sqlFormatterConfig shortcut storage", () => {
it("does not serialize JSON editor shortcut settings", () => {
@@ -23,4 +23,20 @@ describe("sqlFormatterConfig shortcut storage", () => {
expect(result.ok).toBe(true);
});
+
+ it("merges DBX custom parameter types with user paramTypes", () => {
+ const options = sqlFormatterOptions({
+ paramTypes: {
+ positional: false,
+ named: ["@"],
+ custom: [{ regex: String.raw`\{\{[^}]+\}\}` }],
+ },
+ });
+
+ expect(options.paramTypes).toEqual({
+ positional: false,
+ named: ["@"],
+ custom: [{ regex: String.raw`\{\{[^}]+\}\}` }, { regex: String.raw`\$\{[^}]+\}` }, { regex: String.raw`#\{[^}]+\}` }],
+ });
+ });
});
diff --git a/apps/desktop/src/lib/sql/sqlFormatterConfig.ts b/apps/desktop/src/lib/sql/sqlFormatterConfig.ts
index a6ac64e1d2..5e2933712d 100644
--- a/apps/desktop/src/lib/sql/sqlFormatterConfig.ts
+++ b/apps/desktop/src/lib/sql/sqlFormatterConfig.ts
@@ -22,6 +22,11 @@ export interface SqlFormatterCustomParameter {
regex: string;
}
+// DBX substitutes `${name}`/`#{name}` placeholders client-side (see sqlVariableSyntax.ts),
+// but no sql-formatter dialect tokenizes them, so raw SQL containing them would fail to
+// format with a parse error. Always registering them as custom params keeps formatting working.
+const DBX_CUSTOM_PARAM_TYPES: SqlFormatterCustomParameter[] = [{ regex: String.raw`\$\{[^}]+\}` }, { regex: String.raw`#\{[^}]+\}` }];
+
export interface SqlFormatterParamTypes {
positional?: boolean;
numbered?: ("?" | ":" | "$")[];
@@ -239,6 +244,7 @@ export function syncSqlFormatterConfigDraft(text: string, syncSettings: (setting
export function sqlFormatterOptions(settings: unknown) {
const normalized = sqlFormatterOptionSettings(settings);
+ const paramTypes = normalized.paramTypes ?? {};
return {
keywordCase: normalized.keywordCase,
dataTypeCase: normalized.dataTypeCase,
@@ -252,6 +258,9 @@ export function sqlFormatterOptions(settings: unknown) {
linesBetweenQueries: normalized.linesBetweenQueries,
denseOperators: normalized.denseOperators,
newlineBeforeSemicolon: normalized.newlineBeforeSemicolon,
- ...(normalized.paramTypes !== null ? { paramTypes: normalized.paramTypes } : {}),
+ paramTypes: {
+ ...paramTypes,
+ custom: [...(paramTypes.custom ?? []), ...DBX_CUSTOM_PARAM_TYPES],
+ },
};
}
diff --git a/packages/app-tests/sqlFormatterConfig.test.ts b/packages/app-tests/sqlFormatterConfig.test.ts
index cbfd298538..d820a340b3 100644
--- a/packages/app-tests/sqlFormatterConfig.test.ts
+++ b/packages/app-tests/sqlFormatterConfig.test.ts
@@ -249,6 +249,9 @@ test("maps DBX formatter settings to sql-formatter options", () => {
linesBetweenQueries: 1,
denseOperators: false,
newlineBeforeSemicolon: true,
+ paramTypes: {
+ custom: [{ regex: String.raw`\$\{[^}]+\}` }, { regex: String.raw`#\{[^}]+\}` }],
+ },
},
);
@@ -270,7 +273,10 @@ test("maps DBX formatter settings to sql-formatter options", () => {
linesBetweenQueries: 1,
denseOperators: false,
newlineBeforeSemicolon: false,
- paramTypes: { positional: true },
+ paramTypes: {
+ positional: true,
+ custom: [{ regex: String.raw`\$\{[^}]+\}` }, { regex: String.raw`#\{[^}]+\}` }],
+ },
},
);
});