Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 92bc2de

Browse files
authored
Improve the implementation of FloatConverter to fix the conversion failure problem in some cases. (#657)
* add ToString format paramater support for MySql DateTime * Add tests for DateTime.ToString() format parameter in Mysql * Improve the implementation of FloatConverter to fix the conversion failure problem in some cases.
1 parent f899103 commit 92bc2de

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

src/ServiceStack.OrmLite/Converters/FloatConverters.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public override string ToQuotedString(Type fieldType, object value)
2525
switch (typeCode)
2626
{
2727
case TypeCode.Single:
28-
return ((float)value).ToString(CultureInfo.InvariantCulture);
28+
return Convert.ToSingle(value).ToString(CultureInfo.InvariantCulture);
2929
case TypeCode.Double:
30-
return ((double)value).ToString(CultureInfo.InvariantCulture);
30+
return Convert.ToDouble(value).ToString(CultureInfo.InvariantCulture);
3131
case TypeCode.Decimal:
32-
return ((decimal)value).ToString(CultureInfo.InvariantCulture);
32+
return Convert.ToDecimal(value).ToString(CultureInfo.InvariantCulture);
3333
}
3434

3535
return base.ToQuotedString(fieldType, value);

tests/ServiceStack.OrmLite.Tests/ConverterTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Globalization;
35
using System.Linq;
46
using NUnit.Framework;
57
using ServiceStack.DataAnnotations;
8+
using ServiceStack.OrmLite.Converters;
69
using ServiceStack.Text;
710

811
namespace ServiceStack.OrmLite.Tests
@@ -22,6 +25,35 @@ public void FromDbValue_does_not_throw_Exception()
2225
Assert.That(convertedValue, Is.Null);
2326
}
2427

28+
[Test]
29+
public void ToDbValue_does_not_throw_Exception()
30+
{
31+
var dialectProvider = DialectProvider;
32+
var convertedValue = dialectProvider.ToDbValue (89.123456789, typeof(decimal));
33+
Assert.AreEqual(convertedValue, 89.123456789);
34+
}
35+
36+
[Test]
37+
public void ToQuotedString_FloatConverter_not_throw_Exception()
38+
{
39+
var floatConverter = new FloatConverter();
40+
floatConverter.ToQuotedString(typeof(decimal), 89.123456789);
41+
floatConverter.ToQuotedString(typeof(float), 89.123456789);
42+
floatConverter.ToQuotedString(typeof(double), 89.123456789);
43+
}
44+
45+
[Test]
46+
public void ToQuotedString_LegacyFloatConverter_throw_Exception()
47+
{
48+
Assert.Catch(typeof(InvalidCastException), () =>
49+
{
50+
var floatConverter = new LegacyFloatConverter();
51+
floatConverter.ToQuotedString(typeof(decimal), 89.123456789);
52+
floatConverter.ToQuotedString(typeof(float), 89.123456789);
53+
floatConverter.ToQuotedString(typeof(double), 89.123456789);
54+
});
55+
}
56+
2557
[Test]
2658
public void Can_insert_update_and_select_AllTypes()
2759
{
@@ -298,4 +330,36 @@ public override int GetHashCode()
298330
}
299331
}
300332

333+
//A copy of the original implementation code
334+
internal class LegacyFloatConverter : NativeValueOrmLiteConverter
335+
{
336+
public override string ColumnDefinition => "DOUBLE";
337+
public override DbType DbType => DbType.Single;
338+
339+
public override object ToDbValue(Type fieldType, object value)
340+
{
341+
return this.ConvertNumber(fieldType, value);
342+
}
343+
344+
public override object FromDbValue(Type fieldType, object value)
345+
{
346+
return this.ConvertNumber(fieldType, value);
347+
}
348+
349+
public override string ToQuotedString(Type fieldType, object value)
350+
{
351+
var typeCode = fieldType.GetTypeCode();
352+
switch (typeCode)
353+
{
354+
case TypeCode.Single:
355+
return ((float)value).ToString(CultureInfo.InvariantCulture);
356+
case TypeCode.Double:
357+
return ((double)value).ToString(CultureInfo.InvariantCulture);
358+
case TypeCode.Decimal:
359+
return ((decimal)value).ToString(CultureInfo.InvariantCulture);
360+
}
361+
362+
return base.ToQuotedString(fieldType, value);
363+
}
364+
}
301365
}

0 commit comments

Comments
 (0)