Skip to content

Commit c881f76

Browse files
committed
Support ZEROFILL MySqlDecimal columns. Fixes #1354
1 parent 59bf391 commit c881f76

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/MySqlConnector/MySqlDecimal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal MySqlDecimal(string value)
4646
throw new FormatException($"Could not parse the value as a MySqlDecimal: {value}");
4747
}
4848

49-
private static readonly Regex s_pattern = new(@"^-?([1-9][0-9]*|0)(\.([0-9]+))?$");
49+
private static readonly Regex s_pattern = new(@"^-?([0-9]+)(\.([0-9]+))?$");
5050

5151
private readonly string m_value;
5252
}

tests/IntegrationTests/InsertTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,40 @@ public void ReadMySqlDecimalUsingReader(bool prepare)
514514
#endif
515515
}
516516

517+
[Theory]
518+
[InlineData(false)]
519+
[InlineData(true)]
520+
public void ReadMySqlDecimalZeroFill(bool prepare)
521+
{
522+
using MySqlConnection connection = new MySqlConnection(AppConfig.ConnectionString);
523+
connection.Open();
524+
connection.Execute("""
525+
drop table if exists mysql_decimal_zerofill;
526+
create table mysql_decimal_zerofill(rowid integer not null primary key auto_increment, value decimal(20, 10) zerofill);
527+
insert into mysql_decimal_zerofill(value) values(0),(1),(0.1);
528+
""");
529+
530+
using var cmd = connection.CreateCommand();
531+
cmd.CommandText = @"select value from mysql_decimal_zerofill order by rowid;";
532+
if (prepare)
533+
cmd.Prepare();
534+
using var reader = cmd.ExecuteReader();
535+
536+
Assert.True(reader.Read());
537+
Assert.Equal("0000000000.0000000000", reader.GetMySqlDecimal("value").ToString());
538+
Assert.Equal(0m, reader.GetDecimal(0));
539+
540+
Assert.True(reader.Read());
541+
Assert.Equal("0000000001.0000000000", reader.GetMySqlDecimal("value").ToString());
542+
Assert.Equal(1m, reader.GetDecimal(0));
543+
544+
Assert.True(reader.Read());
545+
Assert.Equal("0000000000.1000000000", reader.GetMySqlDecimal("value").ToString());
546+
Assert.Equal(0.1m, reader.GetDecimal(0));
547+
548+
Assert.False(reader.Read());
549+
}
550+
517551
[Theory]
518552
[InlineData(false)]
519553
[InlineData(true)]

tests/MySqlConnector.Tests/MySqlDecimalTests.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ public void TestToDecimal()
3030
Assert.Equal(doubleVal, mySqlDecimal.Value);
3131
}
3232

33-
[Fact]
34-
public void TestInvalidFormatWithDecimalPostive()
35-
{
36-
var invalidValue = "0323.323";
37-
Assert.Throws<FormatException>(() => new MySqlDecimal(invalidValue));
38-
}
39-
40-
[Fact]
41-
public void TestInvalidFormatWithDecimalNegative()
42-
{
43-
var invalidValue = "-0323.323";
44-
Assert.Throws<FormatException>(() => new MySqlDecimal(invalidValue));
45-
}
46-
4733
[Fact]
4834
public void TestValidFormatWithDecimalNegative68Length()
4935
{
@@ -109,6 +95,10 @@ public void TestValidFormatWithDecimalNegative67Length()
10995
[InlineData("-0.1")]
11096
[InlineData("1.0")]
11197
[InlineData("1.23")]
98+
[InlineData("00")]
99+
[InlineData("01")]
100+
[InlineData("0323.323")]
101+
[InlineData("-0323.323")]
112102
[InlineData("12345678901234567890123456789012345678901234567890123456789012345")]
113103
[InlineData("-12345678901234567890123456789012345678901234567890123456789012345")]
114104
[InlineData("12345678901234567890123456789012345.012345678901234567890123456789")]
@@ -120,8 +110,6 @@ public void ValidDecimalValues(string input) =>
120110
[InlineData("")]
121111
[InlineData("-0")]
122112
[InlineData("-0.0")]
123-
[InlineData("00")]
124-
[InlineData("01")]
125113
[InlineData("123456789012345678901234567890123456789012345678901234567890123456")]
126114
[InlineData("-123456789012345678901234567890123456789012345678901234567890123456")]
127115
[InlineData("123456789012345678901234567890123456.012345678901234567890123456789")]

0 commit comments

Comments
 (0)