Skip to content

Commit 9db451e

Browse files
committed
Allow integral conversions in GetByte. Fixes #695
1 parent 2b2b3c5 commit 9db451e

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,55 @@ public bool GetBoolean(int ordinal)
6464
return (bool) value;
6565
}
6666

67-
public sbyte GetSByte(int ordinal) => (sbyte) GetValue(ordinal);
67+
public sbyte GetSByte(int ordinal)
68+
{
69+
var value = GetValue(ordinal);
70+
if (value is sbyte sbyteValue)
71+
return sbyteValue;
72+
73+
if (value is byte byteValue)
74+
return checked((sbyte) byteValue);
75+
if (value is short shortValue)
76+
return checked((sbyte) shortValue);
77+
if (value is ushort ushortValue)
78+
return checked((sbyte) ushortValue);
79+
if (value is int intValue)
80+
return checked((sbyte) intValue);
81+
if (value is uint uintValue)
82+
return checked((sbyte) uintValue);
83+
if (value is long longValue)
84+
return checked((sbyte) longValue);
85+
if (value is ulong ulongValue)
86+
return checked((sbyte) ulongValue);
87+
if (value is decimal decimalValue)
88+
return (sbyte) decimalValue;
89+
return (sbyte) value;
90+
}
6891

69-
public byte GetByte(int ordinal) => (byte) GetValue(ordinal);
92+
public byte GetByte(int ordinal)
93+
{
94+
var value = GetValue(ordinal);
95+
if (value is byte byteValue)
96+
return byteValue;
97+
98+
if (value is sbyte sbyteValue)
99+
return checked((byte) sbyteValue);
100+
if (value is short shortValue)
101+
return checked((byte) shortValue);
102+
if (value is ushort ushortValue)
103+
return checked((byte) ushortValue);
104+
if (value is int intValue)
105+
return checked((byte) intValue);
106+
if (value is uint uintValue)
107+
return checked((byte) uintValue);
108+
if (value is long longValue)
109+
return checked((byte) longValue);
110+
if (value is ulong ulongValue)
111+
return checked((byte) ulongValue);
112+
if (value is decimal decimalValue)
113+
return (byte) decimalValue;
114+
return (byte) value;
115+
}
70116

71117
public long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
72118
{

tests/Conformance.Tests/GetValueConversionTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ public GetValueConversionTests(SelectValueFixture fixture)
4545
public override void GetBoolean_throws_for_zero_UInt32() => TestGetValue(DbType.UInt32, ValueKind.Zero, x => x.GetBoolean(0), false);
4646
public override void GetBoolean_throws_for_zero_UInt64() => TestGetValue(DbType.UInt64, ValueKind.Zero, x => x.GetBoolean(0), false);
4747

48+
// GetByte allows integral conversions
49+
public override void GetByte_throws_for_maximum_Int16() => TestException(DbType.Int16, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
50+
public override void GetByte_throws_for_maximum_Int32() => TestException(DbType.Int32, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
51+
public override void GetByte_throws_for_maximum_Int64() => TestException(DbType.Int64, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
52+
public override void GetByte_throws_for_maximum_SByte() => TestGetValue(DbType.SByte, ValueKind.Maximum, x => x.GetByte(0), (byte) 127);
53+
public override void GetByte_throws_for_maximum_UInt16() => TestException(DbType.UInt16, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
54+
public override void GetByte_throws_for_maximum_UInt32() => TestException(DbType.UInt32, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
55+
public override void GetByte_throws_for_maximum_UInt64() => TestException(DbType.UInt64, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
56+
public override void GetByte_throws_for_maximum_Decimal() => TestException(DbType.Decimal, ValueKind.Maximum, x => x.GetByte(0), typeof(OverflowException));
57+
public override void GetByte_throws_for_minimum_Int16() => TestException(DbType.Int16, ValueKind.Minimum, x => x.GetByte(0), typeof(OverflowException));
58+
public override void GetByte_throws_for_minimum_Int32() => TestException(DbType.Int32, ValueKind.Minimum, x => x.GetByte(0), typeof(OverflowException));
59+
public override void GetByte_throws_for_minimum_Int64() => TestException(DbType.Int64, ValueKind.Minimum, x => x.GetByte(0), typeof(OverflowException));
60+
public override void GetByte_throws_for_minimum_SByte() => TestException(DbType.SByte, ValueKind.Minimum, x => x.GetByte(0), typeof(OverflowException));
61+
public override void GetByte_throws_for_minimum_UInt16() => TestGetValue(DbType.UInt16, ValueKind.Minimum, x => x.GetByte(0), (byte) 0);
62+
public override void GetByte_throws_for_minimum_UInt32() => TestGetValue(DbType.UInt32, ValueKind.Minimum, x => x.GetByte(0), (byte) 0);
63+
public override void GetByte_throws_for_minimum_UInt64() => TestGetValue(DbType.UInt64, ValueKind.Minimum, x => x.GetByte(0), (byte) 0);
64+
public override void GetByte_throws_for_minimum_Decimal() => TestGetValue(DbType.Decimal, ValueKind.Minimum, x => x.GetByte(0), (byte) 0);
65+
public override void GetByte_throws_for_one_Int16() => TestGetValue(DbType.Int16, ValueKind.One, x => x.GetByte(0), (byte) 1);
66+
public override void GetByte_throws_for_one_Int32() => TestGetValue(DbType.Int32, ValueKind.One, x => x.GetByte(0), (byte) 1);
67+
public override void GetByte_throws_for_one_Int64() => TestGetValue(DbType.Int64, ValueKind.One, x => x.GetByte(0), (byte) 1);
68+
public override void GetByte_throws_for_one_SByte() => TestGetValue(DbType.SByte, ValueKind.One, x => x.GetByte(0), (byte) 1);
69+
public override void GetByte_throws_for_one_UInt16() => TestGetValue(DbType.UInt16, ValueKind.One, x => x.GetByte(0), (byte) 1);
70+
public override void GetByte_throws_for_one_UInt32() => TestGetValue(DbType.UInt32, ValueKind.One, x => x.GetByte(0), (byte) 1);
71+
public override void GetByte_throws_for_one_UInt64() => TestGetValue(DbType.UInt64, ValueKind.One, x => x.GetByte(0), (byte) 1);
72+
public override void GetByte_throws_for_one_Decimal() => TestGetValue(DbType.Decimal, ValueKind.One, x => x.GetByte(0), (byte) 1);
73+
public override void GetByte_throws_for_zero_Int16() => TestGetValue(DbType.Int16, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
74+
public override void GetByte_throws_for_zero_Int32() => TestGetValue(DbType.Int32, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
75+
public override void GetByte_throws_for_zero_Int64() => TestGetValue(DbType.Int64, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
76+
public override void GetByte_throws_for_zero_SByte() => TestGetValue(DbType.SByte, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
77+
public override void GetByte_throws_for_zero_UInt16() => TestGetValue(DbType.UInt16, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
78+
public override void GetByte_throws_for_zero_UInt32() => TestGetValue(DbType.UInt32, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
79+
public override void GetByte_throws_for_zero_UInt64() => TestGetValue(DbType.UInt64, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
80+
public override void GetByte_throws_for_zero_Decimal() => TestGetValue(DbType.Decimal, ValueKind.Zero, x => x.GetByte(0), (byte) 0);
81+
4882
// the minimum date permitted by MySQL is 1000-01-01; override the minimum value for DateTime tests
4983
public override void GetDateTime_for_minimum_Date() => TestGetValue(DbType.Date, ValueKind.Minimum, x => x.GetDateTime(0), new DateTime(1000, 1, 1));
5084
public override void GetDateTime_for_minimum_DateTime() => TestGetValue(DbType.Date, ValueKind.Minimum, x => x.GetDateTime(0), new DateTime(1000, 1, 1));

0 commit comments

Comments
 (0)