Skip to content

Commit 34bcbda

Browse files
committed
Remove duplicated method.
This was added independently in 1f17b21 and 16d2c99. Add tests to verify that parameters are found case-insensitively.
1 parent fea6c56 commit 34bcbda

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

src/MySqlConnector/MySqlClient/MySqlParameterCollection.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ public override int IndexOf(string parameterName)
8585
return string.Equals(parameterName, m_parameters[index].ParameterName, StringComparison.OrdinalIgnoreCase) ? index : -1;
8686
}
8787

88+
// Finds the index of a parameter by name, regardless of whether 'parameterName' or the matching
89+
// MySqlParameter.ParameterName has a leading '?' or '@'.
8890
internal int NormalizedIndexOf(string parameterName)
8991
{
90-
if (parameterName == null)
91-
throw new ArgumentNullException(nameof(parameterName));
92-
return m_nameToIndex.TryGetValue(MySqlParameter.NormalizeParameterName(parameterName), out var index) ? index : -1;
92+
var normalizedName = MySqlParameter.NormalizeParameterName(parameterName ?? throw new ArgumentNullException(nameof(parameterName)));
93+
return m_nameToIndex.TryGetValue(normalizedName, out var index) ? index : -1;
9394
}
9495

9596
public override void Insert(int index, object value) => m_parameters.Insert(index, (MySqlParameter) value);
@@ -141,15 +142,6 @@ protected override void SetParameter(int index, DbParameter value)
141142
set => SetParameter(index, value);
142143
}
143144

144-
// Finds the index of a parameter by name, regardless of whether 'parameterName' or the matching
145-
// MySqlParameter.ParameterName has a leading '?' or '@'.
146-
internal int FlexibleIndexOf(string parameterName)
147-
{
148-
if (parameterName == null)
149-
throw new ArgumentNullException(nameof(parameterName));
150-
return m_nameToIndex.TryGetValue(MySqlParameter.NormalizeParameterName(parameterName), out var index) ? index : -1;
151-
}
152-
153145
private void AddParameter(MySqlParameter parameter)
154146
{
155147
m_parameters.Add(parameter);

src/MySqlConnector/MySqlClient/MySqlStatementPreparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected override void OnBeforeParse(string sql)
5151
protected override void OnNamedParameter(int index, int length)
5252
{
5353
var parameterName = m_preparer.m_commandText.Substring(index, length);
54-
var parameterIndex = m_preparer.m_parameters.FlexibleIndexOf(parameterName);
54+
var parameterIndex = m_preparer.m_parameters.NormalizedIndexOf(parameterName);
5555
if (parameterIndex != -1)
5656
DoAppendParameter(parameterIndex, index, length);
5757
else if ((m_preparer.m_options & StatementPreparerOptions.AllowUserVariables) == 0)

tests/SideBySide/ParameterCollection.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66
namespace SideBySide
77
{
8-
public class ParameterCollection : IDisposable
9-
{
10-
public ParameterCollection()
11-
{
12-
m_command = new MySqlCommand();
8+
public class ParameterCollection : IDisposable
9+
{
10+
public ParameterCollection()
11+
{
12+
m_command = new MySqlCommand();
1313
m_parameterCollection = m_command.Parameters;
1414
}
1515

16-
public void Dispose()
17-
{
18-
m_command.Dispose();
19-
}
16+
public void Dispose()
17+
{
18+
m_command.Dispose();
19+
}
2020

2121
[Theory]
2222
[InlineData("Baz", 0)]
@@ -33,12 +33,12 @@ public void Dispose()
3333
[InlineData("?Bar", -1)]
3434
[InlineData("", -1)]
3535
public void FindByName(string parameterName, int position)
36-
{
37-
m_parameterCollection.Add(new MySqlParameter { ParameterName = "Baz", Value = 0 });
36+
{
37+
m_parameterCollection.Add(new MySqlParameter { ParameterName = "Baz", Value = 0 });
3838
m_parameterCollection.Add(new MySqlParameter { ParameterName = "@Test", Value = 1 });
3939
m_parameterCollection.Add(new MySqlParameter { ParameterName = "?Foo", Value = 2 });
40-
int index = m_parameterCollection.IndexOf(parameterName);
41-
Assert.Equal(position, index);
40+
int index = m_parameterCollection.IndexOf(parameterName);
41+
Assert.Equal(position, index);
4242
Assert.Equal(position != -1, m_parameterCollection.Contains(parameterName));
4343

4444
string expectedParameterName = parameterName;
@@ -60,7 +60,31 @@ public void FindByName(string parameterName, int position)
6060
Assert.NotNull(parameter);
6161
Assert.Equal(expectedParameterName, parameter.ParameterName);
6262
}
63-
}
63+
}
64+
65+
[Theory]
66+
[InlineData("@test")]
67+
[InlineData("@Test")]
68+
[InlineData("@tEsT")]
69+
[InlineData("@TEST")]
70+
public void FindByNameIgnoringCase(string parameterName)
71+
{
72+
m_parameterCollection.AddWithValue("@Test", 1);
73+
Assert.Equal(1, m_parameterCollection.Count);
74+
75+
Assert.True(m_parameterCollection.Contains(parameterName));
76+
Assert.Equal(0, m_parameterCollection.IndexOf(parameterName));
77+
78+
var parameter = m_parameterCollection[parameterName];
79+
Assert.Equal("@Test", parameter.ParameterName);
80+
Assert.Equal(1, parameter.Value);
81+
82+
parameter = m_parameterCollection[0];
83+
Assert.Equal("@Test", parameter.ParameterName);
84+
85+
m_parameterCollection.RemoveAt(parameterName);
86+
Assert.Equal(0, m_parameterCollection.Count);
87+
}
6488

6589
[Fact]
6690
public void IndexOfNull()
@@ -82,17 +106,17 @@ public void Clear()
82106
}
83107

84108
[Fact]
85-
public void RemoveAtIndex()
86-
{
109+
public void RemoveAtIndex()
110+
{
87111
m_parameterCollection.Add(new MySqlParameter { ParameterName = "@Test1", Value = 0 });
88112
m_parameterCollection.Add(new MySqlParameter { ParameterName = "@Test2", Value = 1 });
89113
Assert.Equal(0, m_parameterCollection.IndexOf("@Test1"));
90114
Assert.Equal(1, m_parameterCollection.IndexOf("@Test2"));
91-
m_parameterCollection.RemoveAt(0);
92-
Assert.Equal(1, m_parameterCollection.Count);
115+
m_parameterCollection.RemoveAt(0);
116+
Assert.Equal(1, m_parameterCollection.Count);
93117
Assert.Equal(-1, m_parameterCollection.IndexOf("@Test1"));
94118
Assert.Equal(0, m_parameterCollection.IndexOf("@Test2"));
95-
}
119+
}
96120

97121
[Fact]
98122
public void RemoveAtString()
@@ -122,8 +146,8 @@ public void SetParameterIndex()
122146
}
123147

124148
[Fact]
125-
public void SetParameterString()
126-
{
149+
public void SetParameterString()
150+
{
127151
m_parameterCollection.Add(new MySqlParameter { ParameterName = "@Test1", Value = 0 });
128152
m_parameterCollection.Add(new MySqlParameter { ParameterName = "@Test2", Value = 1 });
129153
Assert.Equal(0, m_parameterCollection.IndexOf("@Test1"));

0 commit comments

Comments
 (0)