Skip to content

Commit

Permalink
nhibernateGH-3530: The MySql driver doesn't properly convert strings …
Browse files Browse the repository at this point in the history
…from the database to decimal types. Add a MySqlDbDataReader to correct this deficiency.
  • Loading branch information
David Ellingsworth authored and David Ellingsworth committed Jun 10, 2024
1 parent f8c5c9d commit 350f5c6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/NHibernate/AdoNet/MySqlDbDataReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernate.AdoNet
{
public class MySqlDbDataReader : DbDataReaderWrapper
{
public MySqlDbDataReader(DbDataReader reader) : base(reader) { }

// MySql driver has a bug that incorrectly uses the CurrentCulture to parse strings.
public override float GetFloat(int ordinal)
{
var value = DataReader[ordinal];

return value switch
{
string => Convert.ToSingle(value, CultureInfo.InvariantCulture),
_ => (float) value
};
}

public override double GetDouble(int ordinal)
{
var value = DataReader[ordinal];

return value switch
{
string => Convert.ToDouble(value, CultureInfo.InvariantCulture),
_ => (double) value
};
}

public override decimal GetDecimal(int ordinal)
{
var value = DataReader[ordinal];

return value switch
{
string => Convert.ToDecimal(value, CultureInfo.InvariantCulture),
_ => (decimal) value
};
}
}
}
30 changes: 30 additions & 0 deletions src/NHibernate/Async/Driver/MySqlDataDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Data.Common;
using NHibernate.AdoNet;

namespace NHibernate.Driver
{
using System.Threading.Tasks;
using System.Threading;
public partial class MySqlDataDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider
{

public override async Task<DbDataReader> ExecuteReaderAsync(DbCommand command, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var reader = await (command.ExecuteReaderAsync(cancellationToken)).ConfigureAwait(false);

return new MySqlDbDataReader(reader);
}
}
}
10 changes: 9 additions & 1 deletion src/NHibernate/Driver/MySqlDataDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data.Common;
using NHibernate.AdoNet;

namespace NHibernate.Driver
Expand All @@ -17,7 +18,7 @@ namespace NHibernate.Driver
/// for any updates and/or documentation regarding MySQL.
/// </para>
/// </remarks>
public class MySqlDataDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider
public partial class MySqlDataDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="MySqlDataDriver"/> class.
Expand Down Expand Up @@ -79,6 +80,13 @@ public override IResultSetsCommand GetResultSetsCommand(Engine.ISessionImplement
/// <inheritdoc />
public override DateTime MinDate => new DateTime(1000, 1, 1);

public override DbDataReader ExecuteReader(DbCommand command)
{
var reader = command.ExecuteReader();

return new MySqlDbDataReader(reader);
}

System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(MySqlClientBatchingBatcherFactory);
}
}

0 comments on commit 350f5c6

Please sign in to comment.