Skip to content

Commit 65c5735

Browse files
authored
Merge pull request #785 from danielgindi/feature/GetDecimal_casts
Have the same casts in GetDecimal as in GetDouble.
2 parents df7499d + 743d4ac commit 65c5735

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ public Stream GetStream(int ordinal)
350350

351351
public string GetString(int ordinal) => (string) GetValue(ordinal);
352352

353-
public decimal GetDecimal(int ordinal) => (decimal) GetValue(ordinal);
353+
public decimal GetDecimal(int ordinal)
354+
{
355+
var value = GetValue(ordinal);
356+
return value is float floatValue ? (decimal) floatValue :
357+
value is double decimalValue ? (decimal) decimalValue :
358+
(decimal) value;
359+
}
354360

355361
public double GetDouble(int ordinal)
356362
{

tests/Conformance.Tests/GetValueConversionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ public GetValueConversionTests(SelectValueFixture fixture)
159159
public override void GetDateTime_for_minimum_DateTime() => TestGetValue(DbType.Date, ValueKind.Minimum, x => x.GetDateTime(0), new DateTime(1000, 1, 1));
160160
public override void GetDateTime_for_minimum_DateTime_with_GetFieldValue() => TestGetValue(DbType.Date, ValueKind.Minimum, x => x.GetDateTime(0), new DateTime(1000, 1, 1));
161161

162+
// GetDecimal() allows conversions from float/double
163+
public override void GetDecimal_throws_for_zero_Single() => TestGetValue(DbType.Single, ValueKind.Zero, x => x.GetDecimal(0), 0m);
164+
public override void GetDecimal_throws_for_zero_Single_with_GetFieldValue() => TestGetValue(DbType.Single, ValueKind.Zero, x => x.GetDecimal(0), 0m);
165+
public override void GetDecimal_throws_for_one_Single() => TestGetValue(DbType.Single, ValueKind.One, x => x.GetDecimal(0), 1m);
166+
public override void GetDecimal_throws_for_one_Single_with_GetFieldValue() => TestGetValue(DbType.Single, ValueKind.One, x => x.GetDecimal(0), 1m);
167+
public override void GetDecimal_throws_for_minimum_Single() => TestGetValue(DbType.Single, ValueKind.Minimum, x => x.GetDecimal(0), 0m);
168+
public override void GetDecimal_throws_for_minimum_Single_with_GetFieldValue() => TestGetValue(DbType.Single, ValueKind.Minimum, x => x.GetDecimal(0), 0m);
169+
public override void GetDecimal_throws_for_maximum_Single() => TestException(DbType.Single, ValueKind.Maximum, x => x.GetDecimal(0), typeof(OverflowException));
170+
public override void GetDecimal_throws_for_maximum_Single_with_GetFieldValue() => TestException(DbType.Single, ValueKind.Maximum, x => x.GetDecimal(0), typeof(OverflowException));
171+
public override void GetDecimal_throws_for_zero_Double() => TestGetValue(DbType.Double, ValueKind.Zero, x => x.GetDecimal(0), 0m);
172+
public override void GetDecimal_throws_for_zero_Double_with_GetFieldValue() => TestGetValue(DbType.Double, ValueKind.Zero, x => x.GetDecimal(0), 0m);
173+
public override void GetDecimal_throws_for_one_Double() => TestGetValue(DbType.Double, ValueKind.One, x => x.GetDecimal(0), 1m);
174+
public override void GetDecimal_throws_for_one_Double_with_GetFieldValue() => TestGetValue(DbType.Double, ValueKind.One, x => x.GetDecimal(0), 1m);
175+
public override void GetDecimal_throws_for_minimum_Double() => TestGetValue(DbType.Double, ValueKind.Minimum, x => x.GetDecimal(0), 0m);
176+
public override void GetDecimal_throws_for_minimum_Double_with_GetFieldValue() => TestGetValue(DbType.Double, ValueKind.Minimum, x => x.GetDecimal(0), 0m);
177+
public override void GetDecimal_throws_for_maximum_Double() => TestException(DbType.Double, ValueKind.Maximum, x => x.GetDecimal(0), typeof(OverflowException));
178+
public override void GetDecimal_throws_for_maximum_Double_with_GetFieldValue() => TestException(DbType.Double, ValueKind.Maximum, x => x.GetDecimal(0), typeof(OverflowException));
179+
162180
// The GetFloat() implementation allows for conversions from double to float.
163181
// The minimum tests for float and double do not test for the smallest possible value (as the tests for integer values do),
164182
// but test for the largest value smaller than 0 (Epsilon).

0 commit comments

Comments
 (0)