Skip to content

Commit 10597b7

Browse files
committed
Handle null parameter value. Fixes #126
1 parent d8855a8 commit 10597b7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/MySqlConnector/MySqlClient/MySqlParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override void ResetDbType()
5959

6060
internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions options)
6161
{
62-
if (Value == DBNull.Value)
62+
if (Value == null || Value == DBNull.Value)
6363
{
6464
writer.WriteUtf8("NULL");
6565
}

tests/SideBySide.New/QueryTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,48 @@ public async Task GetName()
219219
}
220220
}
221221

222+
[Fact]
223+
public async Task ParameterIsNull()
224+
{
225+
using (var cmd = m_database.Connection.CreateCommand())
226+
{
227+
cmd.CommandText = @"drop table if exists query_null_parameter;
228+
create table query_null_parameter(id integer not null primary key, value text);
229+
insert into query_null_parameter (id, value) VALUES (1, 'one'), (2, 'two'), (3, null);
230+
";
231+
cmd.ExecuteNonQuery();
232+
}
233+
234+
using (var cmd = m_database.Connection.CreateCommand())
235+
{
236+
cmd.CommandText = "select id, value FROM query_null_parameter where @parameter is null or value = @parameter order by id;";
237+
cmd.Parameters.Add(new MySqlParameter { ParameterName = "@parameter", Value = "one" });
238+
using (var reader = cmd.ExecuteReader())
239+
{
240+
Assert.True(await reader.ReadAsync());
241+
Assert.Equal(1L, reader.GetInt64(0));
242+
Assert.False(await reader.ReadAsync());
243+
Assert.False(await reader.NextResultAsync());
244+
}
245+
}
246+
247+
using (var cmd = m_database.Connection.CreateCommand())
248+
{
249+
cmd.CommandText = "select id, value FROM query_null_parameter where @parameter is null or value = @parameter order by id;";
250+
cmd.Parameters.Add(new MySqlParameter { ParameterName = "@parameter", Value = null });
251+
using (var reader = cmd.ExecuteReader())
252+
{
253+
Assert.True(await reader.ReadAsync());
254+
Assert.Equal(1L, reader.GetInt64(0));
255+
Assert.True(await reader.ReadAsync());
256+
Assert.Equal(2L, reader.GetInt64(0));
257+
Assert.True(await reader.ReadAsync());
258+
Assert.Equal(3L, reader.GetInt64(0));
259+
Assert.False(await reader.ReadAsync());
260+
Assert.False(await reader.NextResultAsync());
261+
}
262+
}
263+
}
222264

223265
[Fact]
224266
public async Task DoubleDispose()

0 commit comments

Comments
 (0)