Skip to content

Commit 813a61e

Browse files
committed
Support enum parameter values. Fixes #255
1 parent 168f38f commit 813a61e

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ may execute differently with MySqlConnector. To get Connector/NET-compatible beh
6363
* [#81650](https://bugs.mysql.com/bug.php?id=81650): `Server` connection string option may now contain multiple, comma separated hosts that will be tried in order until a connection succeeds
6464
* [#83229](https://bugs.mysql.com/bug.php?id=83329): "Unknown command" exception inserting large blob with UseCompression=True
6565
* [#84220](https://bugs.mysql.com/bug.php?id=84220): Cannot call a stored procedure with `.` in its name
66+
* [#84701](https://bugs.mysql.com/bug.php?id=84701): Can't create a paramter using a 64-bit enum with a value greater than int.MaxValue
6667
* [#85185](https://bugs.mysql.com/bug.php?id=85185): `ConnectionReset=True` does not preserve connection charset

src/MySqlConnector/MySqlClient/MySqlParameter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
193193
{
194194
writer.WriteUtf8("{0}".FormatInvariant((ulong) Value));
195195
}
196+
else if (Value is Enum)
197+
{
198+
writer.WriteUtf8("{0:d}".FormatInvariant(Value));
199+
}
196200
else
197201
{
198202
throw new NotSupportedException("Parameter type {0} (DbType: {1}) not currently supported. Value: {2}".FormatInvariant(Value.GetType().Name, DbType, Value));

tests/SideBySide/QueryTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,52 @@ public void OutputParameter()
662662
}
663663
}
664664

665+
[Fact]
666+
public void EnumParameter()
667+
{
668+
m_database.Connection.Execute(@"drop table if exists enum_test;
669+
create table enum_test(id integer not null primary key, value text not null);
670+
insert into enum_test (id, value) VALUES (1002, 'no'), (1003, 'yes');
671+
");
672+
673+
using (var command = new MySqlCommand("select * from enum_test where id = @ID;", m_database.Connection))
674+
{
675+
command.Parameters.AddWithValue("@ID", MySqlErrorCode.No);
676+
using (var reader = command.ExecuteReader())
677+
{
678+
Assert.True(reader.Read());
679+
Assert.Equal((int) MySqlErrorCode.No, reader.GetInt32(0));
680+
Assert.Equal("no", reader.GetString(1));
681+
Assert.False(reader.Read());
682+
}
683+
}
684+
}
685+
686+
[Fact
687+
#if BASELINE
688+
(Skip = "https://bugs.mysql.com/bug.php?id=84701")
689+
#endif
690+
]
691+
public void Int64EnumParameter()
692+
{
693+
m_database.Connection.Execute(@"drop table if exists long_enum_test;
694+
create table long_enum_test(id bigint not null primary key, value integer not null);
695+
insert into long_enum_test (id, value) VALUES (0x7FFFFFFFFFFFFFFF, 1);
696+
");
697+
698+
using (var command = new MySqlCommand("select * from long_enum_test where id = @ID;", m_database.Connection))
699+
{
700+
command.Parameters.AddWithValue("@ID", TestLongEnum.Value);
701+
using (var reader = command.ExecuteReader())
702+
{
703+
Assert.True(reader.Read());
704+
Assert.Equal(long.MaxValue, reader.GetInt64(0));
705+
Assert.Equal(1, reader.GetInt32(1));
706+
Assert.False(reader.Read());
707+
}
708+
}
709+
}
710+
665711
class BoolTest
666712
{
667713
public int Id { get; set; }
@@ -681,6 +727,11 @@ public UseReaderWithoutDisposingThreadData(List<Exception> exceptions, MySqlConn
681727
public MySqlConnectionStringBuilder ConnectionStringBuilder { get; }
682728
}
683729

730+
enum TestLongEnum : long
731+
{
732+
Value = long.MaxValue,
733+
}
734+
684735
readonly DatabaseFixture m_database;
685736
}
686737
}

0 commit comments

Comments
 (0)