diff --git a/src/MySqlConnector/MySqlDecimal.cs b/src/MySqlConnector/MySqlDecimal.cs index 45b8afc18..b798578af 100644 --- a/src/MySqlConnector/MySqlDecimal.cs +++ b/src/MySqlConnector/MySqlDecimal.cs @@ -46,7 +46,7 @@ internal MySqlDecimal(string value) throw new FormatException($"Could not parse the value as a MySqlDecimal: {value}"); } - private static readonly Regex s_pattern = new(@"^-?([1-9][0-9]*|0)(\.([0-9]+))?$"); + private static readonly Regex s_pattern = new(@"^-?([0-9]+)(\.([0-9]+))?$"); private readonly string m_value; } diff --git a/tests/IntegrationTests/InsertTests.cs b/tests/IntegrationTests/InsertTests.cs index b47a1539b..573d63962 100644 --- a/tests/IntegrationTests/InsertTests.cs +++ b/tests/IntegrationTests/InsertTests.cs @@ -514,6 +514,40 @@ public void ReadMySqlDecimalUsingReader(bool prepare) #endif } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void ReadMySqlDecimalZeroFill(bool prepare) + { + using MySqlConnection connection = new MySqlConnection(AppConfig.ConnectionString); + connection.Open(); + connection.Execute(""" + drop table if exists mysql_decimal_zerofill; + create table mysql_decimal_zerofill(rowid integer not null primary key auto_increment, value decimal(20, 10) zerofill); + insert into mysql_decimal_zerofill(value) values(0),(1),(0.1); + """); + + using var cmd = connection.CreateCommand(); + cmd.CommandText = @"select value from mysql_decimal_zerofill order by rowid;"; + if (prepare) + cmd.Prepare(); + using var reader = cmd.ExecuteReader(); + + Assert.True(reader.Read()); + Assert.Equal("0000000000.0000000000", reader.GetMySqlDecimal("value").ToString()); + Assert.Equal(0m, reader.GetDecimal(0)); + + Assert.True(reader.Read()); + Assert.Equal("0000000001.0000000000", reader.GetMySqlDecimal("value").ToString()); + Assert.Equal(1m, reader.GetDecimal(0)); + + Assert.True(reader.Read()); + Assert.Equal("0000000000.1000000000", reader.GetMySqlDecimal("value").ToString()); + Assert.Equal(0.1m, reader.GetDecimal(0)); + + Assert.False(reader.Read()); + } + [Theory] [InlineData(false)] [InlineData(true)] diff --git a/tests/MySqlConnector.Tests/MySqlDecimalTests.cs b/tests/MySqlConnector.Tests/MySqlDecimalTests.cs index 8a32827c2..2df97f8a7 100644 --- a/tests/MySqlConnector.Tests/MySqlDecimalTests.cs +++ b/tests/MySqlConnector.Tests/MySqlDecimalTests.cs @@ -30,20 +30,6 @@ public void TestToDecimal() Assert.Equal(doubleVal, mySqlDecimal.Value); } - [Fact] - public void TestInvalidFormatWithDecimalPostive() - { - var invalidValue = "0323.323"; - Assert.Throws(() => new MySqlDecimal(invalidValue)); - } - - [Fact] - public void TestInvalidFormatWithDecimalNegative() - { - var invalidValue = "-0323.323"; - Assert.Throws(() => new MySqlDecimal(invalidValue)); - } - [Fact] public void TestValidFormatWithDecimalNegative68Length() { @@ -109,6 +95,10 @@ public void TestValidFormatWithDecimalNegative67Length() [InlineData("-0.1")] [InlineData("1.0")] [InlineData("1.23")] + [InlineData("00")] + [InlineData("01")] + [InlineData("0323.323")] + [InlineData("-0323.323")] [InlineData("12345678901234567890123456789012345678901234567890123456789012345")] [InlineData("-12345678901234567890123456789012345678901234567890123456789012345")] [InlineData("12345678901234567890123456789012345.012345678901234567890123456789")] @@ -120,8 +110,6 @@ public void ValidDecimalValues(string input) => [InlineData("")] [InlineData("-0")] [InlineData("-0.0")] - [InlineData("00")] - [InlineData("01")] [InlineData("123456789012345678901234567890123456789012345678901234567890123456")] [InlineData("-123456789012345678901234567890123456789012345678901234567890123456")] [InlineData("123456789012345678901234567890123456.012345678901234567890123456789")]