Skip to content

Commit 0eb9518

Browse files
committed
Support char-valued parameters. Fixes #412
1 parent 3076795 commit 0eb9518

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlParameter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
191191
writer.WriteUtf8(stringValue.Replace("\\", "\\\\").Replace("'", "\\'"));
192192
writer.Write((byte) '\'');
193193
}
194+
else if (Value is char charValue)
195+
{
196+
writer.Write((byte) '\'');
197+
switch (charValue)
198+
{
199+
case '\'':
200+
case '\\':
201+
writer.Write((byte) '\\');
202+
writer.Write((byte) charValue);
203+
break;
204+
205+
default:
206+
writer.WriteUtf8(charValue.ToString());
207+
break;
208+
}
209+
writer.Write((byte) '\'');
210+
}
194211
else if (Value is byte || Value is sbyte || Value is short || Value is int || Value is long || Value is ushort || Value is uint || Value is ulong || Value is decimal)
195212
{
196213
writer.WriteUtf8("{0}".FormatInvariant(Value));

tests/SideBySide/QueryTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,62 @@ public void OutputParameter()
767767
}
768768
}
769769

770+
[Fact]
771+
public void CharParameter()
772+
{
773+
m_database.Connection.Execute(@"drop table if exists char_test;
774+
create table char_test(id integer not null primary key, char1 char(1) not null, char4 char(4) not null, varchar1 varchar(1) not null, varchar4 varchar(4) not null) collate utf8mb4_bin;
775+
insert into char_test (id, char1, char4, varchar1, varchar4) VALUES (1, '\'', 'b', 'c', 'Σ'), (2, 'e', '\\', '""', 'h');
776+
");
777+
778+
using (var command = new MySqlCommand("select id from char_test where char1 = @ch;", m_database.Connection))
779+
{
780+
command.Parameters.AddWithValue("@ch", '\'');
781+
using (var reader = command.ExecuteReader())
782+
{
783+
Assert.True(reader.Read());
784+
Assert.Equal(1, reader.GetInt32(0));
785+
Assert.False(reader.Read());
786+
}
787+
}
788+
789+
using (var command = new MySqlCommand("select id from char_test where char4 = @ch;", m_database.Connection))
790+
{
791+
command.Parameters.AddWithValue("@ch", '\\');
792+
using (var reader = command.ExecuteReader())
793+
{
794+
Assert.True(reader.Read());
795+
Assert.Equal(2, reader.GetInt32(0));
796+
Assert.False(reader.Read());
797+
}
798+
}
799+
800+
using (var command = new MySqlCommand("select id from char_test where varchar1 = @ch;", m_database.Connection))
801+
{
802+
command.Parameters.AddWithValue("@ch", '"');
803+
using (var reader = command.ExecuteReader())
804+
{
805+
Assert.True(reader.Read());
806+
Assert.Equal(2, reader.GetInt32(0));
807+
Assert.False(reader.Read());
808+
}
809+
}
810+
811+
#if !BASELINE
812+
// can't repro test failure locally, but it fails on Appveyor
813+
using (var command = new MySqlCommand("select id from char_test where varchar4 = @ch;", m_database.Connection))
814+
{
815+
command.Parameters.AddWithValue("@ch", 'Σ');
816+
using (var reader = command.ExecuteReader())
817+
{
818+
Assert.True(reader.Read());
819+
Assert.Equal(1, reader.GetInt32(0));
820+
Assert.False(reader.Read());
821+
}
822+
}
823+
#endif
824+
}
825+
770826
[Fact]
771827
public void EnumParameter()
772828
{

0 commit comments

Comments
 (0)