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): preserve quoted parameter null semantics

zipg9b8721e0e83a
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts
index f1807201e9..265ad7a98e 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlParameters.spec.ts
@@ -877,7 +877,7 @@ describe("substituteSqlParameters", () => {
).toBe("select * from t where dt >= '2026-06-26' and amount > 100.50 and enabled = TRUE");
});
- it("replaces exact quoted braced placeholders as whole tokens without double-quoting", () => {
+ it("preserves single-quoted string contexts for exact braced placeholders", () => {
const sql = "select * from t where dt = '${date}' and name = \"${name}\" and flag = '#{enabled}' and id = ${id}";
expect(
substituteSqlParameters(sql, {
@@ -886,7 +886,29 @@ describe("substituteSqlParameters", () => {
enabled: { kind: "boolean", value: "true" },
id: { kind: "number", value: "7" },
}),
- ).toBe("select * from t where dt = '2026-06-26' and name = 'O''Reilly' and flag = TRUE and id = 7");
+ ).toBe("select * from t where dt = '2026-06-26' and name = 'O''Reilly' and flag = 'true' and id = 7");
+ });
+
+ it("keeps explicit quotes around raw and numeric parameter values", () => {
+ const sql = "select ${raw_value}, '${raw_value}', ${number_value}, '${number_value}'";
+ expect(
+ substituteSqlParameters(sql, {
+ raw_value: { kind: "raw", value: "current_date" },
+ number_value: { kind: "number", value: "42" },
+ }),
+ ).toBe("select current_date, 'current_date', 42, '42'");
+ });
+
+ it("replaces exact quoted null and empty typed values with SQL NULL", () => {
+ const sql = "select '${null_value}', '${empty_number}', '${empty_raw}', '${empty_string}'";
+ expect(
+ substituteSqlParameters(sql, {
+ null_value: { kind: "null", value: "NULL" },
+ empty_number: { kind: "number", value: "" },
+ empty_raw: { kind: "raw", value: " " },
+ empty_string: { kind: "string", value: "" },
+ }),
+ ).toBe("select NULL, NULL, NULL, ''");
});
it("replaces placeholders embedded in ordinary SQL string values", () => {
diff --git a/apps/desktop/src/lib/sql/sqlParameters.ts b/apps/desktop/src/lib/sql/sqlParameters.ts
index aeb6297451..1ca1e782f8 100644
--- a/apps/desktop/src/lib/sql/sqlParameters.ts
+++ b/apps/desktop/src/lib/sql/sqlParameters.ts
@@ -25,7 +25,7 @@ export interface SqlBracedParameter extends SqlParameterDescriptor {
interface ParameterOccurrence extends SqlParameterDescriptor {
start: number;
end: number;
- replacement?: "string-fragment" | "mybatis-foreach" | "mybatis-where";
+ replacement?: "string-fragment" | "quoted-string" | "mybatis-foreach" | "mybatis-where";
foreach?: MyBatisForeach;
where?: MyBatisWhere;
}
@@ -223,7 +223,7 @@ export function substituteSqlParameters(sql: string, values: Record<string, SqlP
}
// Embedded placeholders stay inside the surrounding SQL string, so their value
// must be escaped as text instead of being wrapped in a second SQL literal.
- result += occurrence.replacement === "string-fragment" ? sqlParameterStringFragment(input) : sqlParameterLiteral(input);
+ result += occurrence.replacement === "string-fragment" ? sqlParameterStringFragment(input) : occurrence.replacement === "quoted-string" ? sqlParameterQuotedString(input) : sqlParameterLiteral(input);
cursor = occurrence.end;
}
result += sql.slice(cursor);
@@ -428,9 +428,7 @@ function findSqlParameterOccurrences(sql: string, options?: SqlParameterOptions)
}
if (ch === "'" || ch === '"') {
- // Exact quoted placeholders use SQL-literal replacement; embedded placeholders
- // in ordinary single-quoted values use escaped text replacement below.
- const quoted = tryReadQuotedBracedPlaceholder(sql, i, ch as "'" | '"', isSyntaxEnabled);
+ const quoted = tryReadQuotedBracedPlaceholder(sql, i, ch, isSyntaxEnabled);
if (quoted) {
occurrences.push(quoted);
i = quoted.end;
@@ -1583,6 +1581,7 @@ function tryReadQuotedBracedPlaceholder(sql: string, start: number, quote: "'" |
name,
syntax,
token: sql.slice(start, end),
+ ...(quote === "\'" ? { replacement: "quoted-string" as const } : {}),
start,
end,
};
@@ -1745,6 +1744,11 @@ function sqlParameterStringFragment(input: SqlParameterInput): string {
return input.value.replace(/'/g, "''");
}
+function sqlParameterQuotedString(input: SqlParameterInput): string {
+ if (input.kind === "null" || ((input.kind === "number" || input.kind === "raw") && !input.value.trim())) return "NULL";
+ return quoteSqlString(input.value);
+}
+
function normalizeBooleanLiteral(value: string): string {
const normalized = value.trim().toLowerCase();
if (normalized === "true" || normalized === "t" || normalized === "yes" || normalized === "y" || normalized === "1") return "TRUE";