Skip to content

Commit d119175

Browse files
committed
Fix adding to hash lookup while renaming an unnamed parameter (#6073)
Fixes #6067 (cherry picked from commit ef219b7)
1 parent 8fd4b43 commit d119175

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Npgsql/NpgsqlParameterCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ internal void ChangeParameterName(NpgsqlParameter parameter, string? value)
143143
var oldTrimmedName = parameter.TrimmedName;
144144
parameter.ChangeParameterName(value);
145145

146-
if (_caseInsensitiveLookup is null || _caseInsensitiveLookup.Count == 0)
146+
if (_caseInsensitiveLookup is null)
147147
return;
148148

149149
var index = IndexOf(parameter);

test/Npgsql.Tests/NpgsqlParameterCollectionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,34 @@ public void Hash_lookup_parameter_rename_bug()
7171
Assert.That(command.Parameters.IndexOf("a_new_name"), Is.GreaterThanOrEqualTo(0));
7272
}
7373

74+
[Test]
75+
[IssueLink("https://github.com/npgsql/npgsql/issues/6067")]
76+
public void Hash_lookup_unnamed_parameter_rename_bug()
77+
{
78+
if (_compatMode == CompatMode.TwoPass)
79+
return;
80+
81+
using var command = new NpgsqlCommand();
82+
83+
for (var i = 0; i < 3; i++)
84+
{
85+
// Put plenty of parameters in the collection to turn on hash lookup functionality.
86+
for (var j = 0; j < LookupThreshold; j++)
87+
{
88+
// Create and add an unnamed parameter before renaming it
89+
var parameter = command.CreateParameter();
90+
command.Parameters.Add(parameter);
91+
parameter.ParameterName = $"{j}";
92+
}
93+
94+
// Make sure hash lookup is generated.
95+
Assert.AreEqual(command.Parameters["3"].ParameterName, "3");
96+
97+
// Remove all parameters to clear hash lookup
98+
command.Parameters.Clear();
99+
}
100+
}
101+
74102
[Test]
75103
public void Remove_duplicate_parameter([Values(LookupThreshold, LookupThreshold - 2)] int count)
76104
{

0 commit comments

Comments
 (0)