Skip to content

Commit

Permalink
Support ZEROFILL MySqlDecimal columns. Fixes #1354
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Sep 4, 2023
1 parent 59bf391 commit c881f76
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/MySqlConnector/MySqlDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
34 changes: 34 additions & 0 deletions tests/IntegrationTests/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
20 changes: 4 additions & 16 deletions tests/MySqlConnector.Tests/MySqlDecimalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ public void TestToDecimal()
Assert.Equal(doubleVal, mySqlDecimal.Value);
}

[Fact]
public void TestInvalidFormatWithDecimalPostive()
{
var invalidValue = "0323.323";
Assert.Throws<FormatException>(() => new MySqlDecimal(invalidValue));
}

[Fact]
public void TestInvalidFormatWithDecimalNegative()
{
var invalidValue = "-0323.323";
Assert.Throws<FormatException>(() => new MySqlDecimal(invalidValue));
}

[Fact]
public void TestValidFormatWithDecimalNegative68Length()
{
Expand Down Expand Up @@ -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")]
Expand All @@ -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")]
Expand Down

0 comments on commit c881f76

Please sign in to comment.