Skip to content

Commit 0c3e3f8

Browse files
[BUG5][BUG6] Escape DELETE sqlLine + quote UPDATE idents + 0.9.2-beta
BUG5: GenerateDeleteMethod built sqlLine with literal " characters (BUG3 fix added them) and embedded the result inside a C# verbatim string @"...". A literal " inside @"..." closes the literal, so the generated .g.cs failed to compile (CS1022). Fix: pre-compute sqlLineVerbatim = sqlLine.Replace("\"", "\"\"") and interpolate that into both NpgsqlConnection + Transaction overloads. BUG6: GenerateUpdateMethod was missed by the BUG3 sweep entirely. The setClauses, whereClauses, and the `UPDATE {schema}.{name}` template were emitted bare. PostgreSQL folded mixed-case names (fhir_Patient -> fhir_patient) and the statement failed at runtime. Fix: quote every identifier in the UPDATE codegen using the `""` verbatim escape form (matches the INSERT path), in both NpgsqlConnection + Transaction overloads. Verified locally: - repro: GenerateUpdate=true, GenerateDelete=true on fhir_Patient - generated UPDATE: UPDATE "public"."fhir_Patient" SET "Active" = @Active WHERE "Id" = @id - generated DELETE: DELETE FROM "public"."fhir_Patient" WHERE "Id" = @id - compile-tested .g.cs as a class library: 0 errors, 0 warnings. Bump Directory.Build.props to 0.9.2-beta.
1 parent a2bb191 commit 0c3e3f8

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

DataProvider/DataProvider/PostgresCli.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,19 @@ string pascalName
10121012
var allParams = pkCols.Concat(updateable).ToList();
10131013
var parameters = string.Join(", ", allParams.Select(c => $"{c.CSharpType} {c.Name}"));
10141014

1015-
var setClauses = string.Join(", ", updateable.Select(c => $"{c.Name} = @{c.Name}"));
1016-
var whereClauses = string.Join(" AND ", pkCols.Select(c => $"{c.Name} = @{c.Name}"));
1015+
// BUG6 fix: quote SET + WHERE column idents in UPDATE codegen so
1016+
// mixed-case columns (Active, ChapterNumber) survive PG case-folding.
1017+
// Same reasoning as BUG3 INSERT/DELETE quoting. The strings are
1018+
// embedded in a C# verbatim string `@"..."` so use the `""` escape
1019+
// form (two double-quotes inside @"..." represent a single literal ").
1020+
var setClauses = string.Join(
1021+
", ",
1022+
updateable.Select(c => $"\"\"{c.Name}\"\" = @{c.Name}")
1023+
);
1024+
var whereClauses = string.Join(
1025+
" AND ",
1026+
pkCols.Select(c => $"\"\"{c.Name}\"\" = @{c.Name}")
1027+
);
10171028

10181029
// NpgsqlConnection overload
10191030
_ = sb.AppendLine();
@@ -1031,9 +1042,11 @@ string pascalName
10311042
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" {parameters})");
10321043
_ = sb.AppendLine(" {");
10331044
_ = sb.AppendLine(" const string sql = @\"");
1045+
// BUG6 fix: quote schema + table in UPDATE so mixed-case fhir_Patient
1046+
// round-trips through PG. Verbatim string -> use `""` escape form.
10341047
_ = sb.AppendLine(
10351048
CultureInfo.InvariantCulture,
1036-
$" UPDATE {table.Schema}.{table.Name}"
1049+
$" UPDATE \"\"{table.Schema}\"\".\"\"{table.Name}\"\""
10371050
);
10381051
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" SET {setClauses}");
10391052
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" WHERE {whereClauses}\";");
@@ -1072,9 +1085,10 @@ string pascalName
10721085
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" {parameters})");
10731086
_ = sb.AppendLine(" {");
10741087
_ = sb.AppendLine(" const string sql = @\"");
1088+
// BUG6 fix: same UPDATE quoting in transaction overload.
10751089
_ = sb.AppendLine(
10761090
CultureInfo.InvariantCulture,
1077-
$" UPDATE {table.Schema}.{table.Name}"
1091+
$" UPDATE \"\"{table.Schema}\"\".\"\"{table.Name}\"\""
10781092
);
10791093
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" SET {setClauses}");
10801094
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" WHERE {whereClauses}\";");
@@ -1146,11 +1160,16 @@ string pascalName
11461160
return;
11471161

11481162
var parameters = string.Join(", ", pkCols.Select(c => $"{c.CSharpType} {c.Name}"));
1149-
// BUG3 fix: quote WHERE-clause column idents + table name. sqlLine
1150-
// here is the raw SQL sent to PG (not embedded in a C# verbatim
1151-
// string), so write literal `"` characters directly.
1163+
// BUG3 fix: quote WHERE-clause column idents + table name so mixed-case
1164+
// tables/columns survive PG case-folding.
11521165
var whereClauses = string.Join(" AND ", pkCols.Select(c => $"\"{c.Name}\" = @{c.Name}"));
11531166
var sqlLine = $"DELETE FROM \"{table.Schema}\".\"{table.Name}\" WHERE {whereClauses}";
1167+
// BUG5 fix: sqlLine now contains literal `"` characters from BUG3 fix
1168+
// and gets embedded in a C# verbatim string `@"..."`. Inside @"...",
1169+
// a single `"` closes the literal. Escape every `"` to `""` for the
1170+
// verbatim form, otherwise the generated .g.cs is uncompilable
1171+
// (CS1022 unexpected token / CS1003 syntax error / CS1010 newline).
1172+
var sqlLineVerbatim = sqlLine.Replace("\"", "\"\"", StringComparison.Ordinal);
11541173

11551174
// NpgsqlConnection overload
11561175
_ = sb.AppendLine();
@@ -1169,7 +1188,7 @@ string pascalName
11691188
_ = sb.AppendLine(" {");
11701189
_ = sb.AppendLine(
11711190
CultureInfo.InvariantCulture,
1172-
$" const string sql = @\"{sqlLine}\";"
1191+
$" const string sql = @\"{sqlLineVerbatim}\";"
11731192
);
11741193
_ = sb.AppendLine();
11751194
_ = sb.AppendLine(" try");
@@ -1207,7 +1226,7 @@ string pascalName
12071226
_ = sb.AppendLine(" {");
12081227
_ = sb.AppendLine(
12091228
CultureInfo.InvariantCulture,
1210-
$" const string sql = @\"{sqlLine}\";"
1229+
$" const string sql = @\"{sqlLineVerbatim}\";"
12111230
);
12121231
_ = sb.AppendLine();
12131232
_ = sb.AppendLine(" if (transaction.Connection is not NpgsqlConnection conn)");

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<NuGetAudit>false</NuGetAudit>
55
<NuGetAuditMode>disabled</NuGetAuditMode>
66
<RestoreAuditProperties>false</RestoreAuditProperties>
7-
<Version>0.9.1-beta</Version>
7+
<Version>0.9.2-beta</Version>
88
<Authors>ChristianFindlay</Authors>
99
<Company>MelbourneDeveloper</Company>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>

0 commit comments

Comments
 (0)