fix(sql): preserve quoted parameter null semantics
This commit is contained in:
2 files changed
+33
-7
No files matched your search
+24
-2
@@ -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", () => {
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in new issue
Block a user