Skip to content

Commit ba7ef22

Browse files
support money postgres data type as decimal
1 parent ad96fbb commit ba7ef22

File tree

26 files changed

+264
-198
lines changed

26 files changed

+264
-198
lines changed

Drivers/MySqlConnectorDriver.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public partial class MySqlConnectorDriver(Options options, Dictionary<string, Ta
5050
new Dictionary<string, DbTypeInfo>
5151
{
5252
{ "char", new DbTypeInfo() },
53-
{ "decimal", new DbTypeInfo() },
5453
{ "longtext", new DbTypeInfo() },
5554
{ "mediumtext", new DbTypeInfo() },
5655
{ "text", new DbTypeInfo() },
@@ -79,6 +78,11 @@ public partial class MySqlConnectorDriver(Options options, Dictionary<string, Ta
7978
{ "double", new DbTypeInfo() },
8079
{ "float", new DbTypeInfo() }
8180
}, ordinal => $"reader.GetDouble({ordinal})"),
81+
new("decimal",
82+
new Dictionary<string, DbTypeInfo>
83+
{
84+
{ "decimal", new DbTypeInfo() }
85+
}, ordinal => $"reader.GetDecimal({ordinal})"),
8286
new("object",
8387
new Dictionary<string, DbTypeInfo>(), ordinal => $"reader.GetValue({ordinal})")
8488
];

Drivers/NpgsqlDriver.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public NpgsqlDriver(Options options, Dictionary<string, Table> tables) : base(op
9191
new Dictionary<string, DbTypeInfo>
9292
{
9393
{ "numeric", new DbTypeInfo(NpgsqlTypeOverride: "NpgsqlDbType.Numeric") },
94-
{ "decimal", new DbTypeInfo(NpgsqlTypeOverride: "NpgsqlDbType.Numeric") }
94+
{ "decimal", new DbTypeInfo(NpgsqlTypeOverride: "NpgsqlDbType.Numeric") },
95+
{ "money", new DbTypeInfo(NpgsqlTypeOverride: "NpgsqlDbType.Money") }
9596
}, ordinal => $"reader.GetDecimal({ordinal})"),
9697
new("double",
9798
new Dictionary<string, DbTypeInfo>

docs/04_Postgres.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ we consider support for the different data types separately for batch inserts an
3939
| text |||
4040
| bytea |||
4141
| 2-dimensional arrays (e.g text[],int[]) |||
42-
| money | | |
42+
| money | | |
4343
| line |||
4444
| lseg |||
4545
| box |||

end2end/EndToEndScaffold/Templates.cs

+15-7
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ public async Task TestMultipleArrays()
483483
{
484484
Impl = $$"""
485485
[Test]
486-
[TestCase(true, 35, -23423, 4235235263, 3.83f, 4.5534, 998.432, -8403284.321435, "2000-1-30", "1983-11-3 02:01:22", "E", "It takes a nation of millions to hold us back", "Rebel Without a Pause", "Prophets of Rage", new byte[] { 0x45, 0x42 }, new string[] { "Party", "Fight" }, new int[] { 543, -4234 })]
487-
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { }, new string[] { }, new int[] { })]
488-
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
486+
[TestCase(true, 35, -23423, 4235235263, 3.83f, 4.5534, 998.432, -8403284.321435, 42332.53, "2000-1-30", "1983-11-3 02:01:22", "E", "It takes a nation of millions to hold us back", "Rebel Without a Pause", "Prophets of Rage", new byte[] { 0x45, 0x42 }, new string[] { "Party", "Fight" }, new int[] { 543, -4234 })]
487+
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { }, new string[] { }, new int[] { })]
488+
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
489489
public async Task TestPostgresTypes(
490490
bool cBoolean,
491491
short cSmallint,
@@ -495,6 +495,7 @@ public async Task TestPostgresTypes(
495495
decimal cNumeric,
496496
decimal cDecimal,
497497
double cDoublePrecision,
498+
decimal cMoney,
498499
DateTime cDate,
499500
DateTime cTimestamp,
500501
string cChar,
@@ -515,6 +516,7 @@ await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs
515516
CNumeric = cNumeric,
516517
CDecimal = cDecimal,
517518
CDoublePrecision = cDoublePrecision,
519+
CMoney = cMoney,
518520
CDate = cDate,
519521
CTimestamp = cTimestamp,
520522
CChar = cChar,
@@ -536,6 +538,7 @@ await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs
536538
CNumeric = cNumeric,
537539
CDecimal = cDecimal,
538540
CDoublePrecision = cDoublePrecision,
541+
CMoney = cMoney,
539542
CDate = cDate,
540543
CTimestamp = cTimestamp,
541544
CChar = cChar,
@@ -560,6 +563,7 @@ private static void AssertSingularEquals(QuerySql.GetPostgresTypesRow expected,
560563
Assert.That(actual.CNumeric, Is.EqualTo(expected.CNumeric));
561564
Assert.That(actual.CDecimal, Is.EqualTo(expected.CDecimal));
562565
Assert.That(actual.CDoublePrecision, Is.EqualTo(expected.CDoublePrecision));
566+
Assert.That(actual.CMoney, Is.EqualTo(expected.CMoney));
563567
Assert.That(actual.CDate, Is.EqualTo(expected.CDate));
564568
Assert.That(actual.CTimestamp, Is.EqualTo(expected.CTimestamp));
565569
Assert.That(actual.CChar, Is.EqualTo(expected.CChar));
@@ -576,10 +580,10 @@ private static void AssertSingularEquals(QuerySql.GetPostgresTypesRow expected,
576580
{
577581
Impl = $$"""
578582
[Test]
579-
[TestCase(100, true, 3, 453, -1445214231, 666.6f, 336.3431, -99.999, -1377.996, "1973-12-3", "1960-11-3 02:01:22", "z", "Sex Pistols", "Anarchy in the U.K", "Never Mind the Bollocks...", new byte[] { 0x53, 0x56 })]
580-
[TestCase(500, false, -4, 867, 8768769709, -64.8f, -324.8671, 127.4793, 423.9869, "2024-12-31", "1999-3-1 03:00:10", "1", "Fugazi", "Waiting Room", "13 Songs", new byte[] { 0x03 })]
581-
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { })]
582-
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
583+
[TestCase(100, true, 3, 453, -1445214231, 666.6f, 336.3431, -99.999, -1377.996, -43242.43, "1973-12-3", "1960-11-3 02:01:22", "z", "Sex Pistols", "Anarchy in the U.K", "Never Mind the Bollocks...", new byte[] { 0x53, 0x56 })]
584+
[TestCase(500, false, -4, 867, 8768769709, -64.8f, -324.8671, 127.4793, 423.9869, 32143.99, "2024-12-31", "1999-3-1 03:00:10", "1", "Fugazi", "Waiting Room", "13 Songs", new byte[] { 0x03 })]
585+
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { })]
586+
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
583587
public async Task TestCopyFrom(
584588
int batchSize,
585589
bool? cBoolean,
@@ -590,6 +594,7 @@ public async Task TestCopyFrom(
590594
decimal? cDecimal,
591595
decimal? cNumeric,
592596
double? cDoublePrecision,
597+
decimal? cMoney,
593598
DateTime? cDate,
594599
DateTime? cTimestamp,
595600
string{{UnknownNullableIndicatorPlaceholder}} cChar,
@@ -609,6 +614,7 @@ public async Task TestCopyFrom(
609614
CDecimal = cDecimal,
610615
CNumeric = cNumeric,
611616
CDoublePrecision = cDoublePrecision,
617+
CMoney = cMoney,
612618
CDate = cDate,
613619
CTimestamp = cTimestamp,
614620
CChar = cChar,
@@ -630,6 +636,7 @@ public async Task TestCopyFrom(
630636
CDecimal = cDecimal,
631637
CNumeric = cNumeric,
632638
CDoublePrecision = cDoublePrecision,
639+
CMoney = cMoney,
633640
CDate = cDate,
634641
CTimestamp = cTimestamp,
635642
CChar = cChar,
@@ -653,6 +660,7 @@ private static void AssertSingularEquals(QuerySql.GetPostgresTypesAggRow expecte
653660
Assert.That(actual.CDecimal, Is.EqualTo(expected.CDecimal));
654661
Assert.That(actual.CNumeric, Is.EqualTo(expected.CNumeric));
655662
Assert.That(actual.CDoublePrecision, Is.EqualTo(expected.CDoublePrecision));
663+
Assert.That(actual.CMoney, Is.EqualTo(expected.CMoney));
656664
Assert.That(actual.CDate, Is.EqualTo(expected.CDate));
657665
Assert.That(actual.CTimestamp, Is.EqualTo(expected.CTimestamp));
658666
Assert.That(actual.CChar, Is.EqualTo(expected.CChar));

end2end/EndToEndTests/NpgsqlDapperTester.generated.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ private static bool SingularEquals(QuerySql.GetAuthorByIdRow x, QuerySql.GetAuth
125125
}
126126

127127
[Test]
128-
[TestCase(100, true, 3, 453, -1445214231, 666.6f, 336.3431, -99.999, -1377.996, "1973-12-3", "1960-11-3 02:01:22", "z", "Sex Pistols", "Anarchy in the U.K", "Never Mind the Bollocks...", new byte[] { 0x53, 0x56 })]
129-
[TestCase(500, false, -4, 867, 8768769709, -64.8f, -324.8671, 127.4793, 423.9869, "2024-12-31", "1999-3-1 03:00:10", "1", "Fugazi", "Waiting Room", "13 Songs", new byte[] { 0x03 })]
130-
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { })]
131-
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
132-
public async Task TestCopyFrom(int batchSize, bool? cBoolean, short? cSmallint, int? cInteger, long? cBigint, float? cReal, decimal? cDecimal, decimal? cNumeric, double? cDoublePrecision, DateTime? cDate, DateTime? cTimestamp, string? cChar, string? cVarchar, string? cCharacterVarying, string? cText, byte[] cBytea)
128+
[TestCase(100, true, 3, 453, -1445214231, 666.6f, 336.3431, -99.999, -1377.996, -43242.43, "1973-12-3", "1960-11-3 02:01:22", "z", "Sex Pistols", "Anarchy in the U.K", "Never Mind the Bollocks...", new byte[] { 0x53, 0x56 })]
129+
[TestCase(500, false, -4, 867, 8768769709, -64.8f, -324.8671, 127.4793, 423.9869, 32143.99, "2024-12-31", "1999-3-1 03:00:10", "1", "Fugazi", "Waiting Room", "13 Songs", new byte[] { 0x03 })]
130+
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { })]
131+
[TestCase(10, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
132+
public async Task TestCopyFrom(int batchSize, bool? cBoolean, short? cSmallint, int? cInteger, long? cBigint, float? cReal, decimal? cDecimal, decimal? cNumeric, double? cDoublePrecision, decimal? cMoney, DateTime? cDate, DateTime? cTimestamp, string? cChar, string? cVarchar, string? cCharacterVarying, string? cText, byte[] cBytea)
133133
{
134-
var batchArgs = Enumerable.Range(0, batchSize).Select(_ => new QuerySql.InsertPostgresTypesBatchArgs { CBoolean = cBoolean, CSmallint = cSmallint, CInteger = cInteger, CBigint = cBigint, CReal = cReal, CDecimal = cDecimal, CNumeric = cNumeric, CDoublePrecision = cDoublePrecision, CDate = cDate, CTimestamp = cTimestamp, CChar = cChar, CVarchar = cVarchar, CCharacterVarying = cCharacterVarying, CText = cText, CBytea = cBytea }).ToList();
134+
var batchArgs = Enumerable.Range(0, batchSize).Select(_ => new QuerySql.InsertPostgresTypesBatchArgs { CBoolean = cBoolean, CSmallint = cSmallint, CInteger = cInteger, CBigint = cBigint, CReal = cReal, CDecimal = cDecimal, CNumeric = cNumeric, CDoublePrecision = cDoublePrecision, CMoney = cMoney, CDate = cDate, CTimestamp = cTimestamp, CChar = cChar, CVarchar = cVarchar, CCharacterVarying = cCharacterVarying, CText = cText, CBytea = cBytea }).ToList();
135135
await QuerySql.InsertPostgresTypesBatch(batchArgs);
136136
var expected = new QuerySql.GetPostgresTypesAggRow
137137
{
@@ -144,6 +144,7 @@ public async Task TestCopyFrom(int batchSize, bool? cBoolean, short? cSmallint,
144144
CDecimal = cDecimal,
145145
CNumeric = cNumeric,
146146
CDoublePrecision = cDoublePrecision,
147+
CMoney = cMoney,
147148
CDate = cDate,
148149
CTimestamp = cTimestamp,
149150
CChar = cChar,
@@ -167,6 +168,7 @@ private static void AssertSingularEquals(QuerySql.GetPostgresTypesAggRow expecte
167168
Assert.That(actual.CDecimal, Is.EqualTo(expected.CDecimal));
168169
Assert.That(actual.CNumeric, Is.EqualTo(expected.CNumeric));
169170
Assert.That(actual.CDoublePrecision, Is.EqualTo(expected.CDoublePrecision));
171+
Assert.That(actual.CMoney, Is.EqualTo(expected.CMoney));
170172
Assert.That(actual.CDate, Is.EqualTo(expected.CDate));
171173
Assert.That(actual.CTimestamp, Is.EqualTo(expected.CTimestamp));
172174
Assert.That(actual.CChar, Is.EqualTo(expected.CChar));
@@ -359,12 +361,12 @@ public async Task TestMultipleArrays()
359361
}
360362

361363
[Test]
362-
[TestCase(true, 35, -23423, 4235235263, 3.83f, 4.5534, 998.432, -8403284.321435, "2000-1-30", "1983-11-3 02:01:22", "E", "It takes a nation of millions to hold us back", "Rebel Without a Pause", "Prophets of Rage", new byte[] { 0x45, 0x42 }, new string[] { "Party", "Fight" }, new int[] { 543, -4234 })]
363-
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { }, new string[] { }, new int[] { })]
364-
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
365-
public async Task TestPostgresTypes(bool cBoolean, short cSmallint, int cInteger, long cBigint, float cReal, decimal cNumeric, decimal cDecimal, double cDoublePrecision, DateTime cDate, DateTime cTimestamp, string cChar, string cVarchar, string cCharacterVarying, string cText, byte[] cBytea, string[] cTextArray, int[] cIntegerArray)
364+
[TestCase(true, 35, -23423, 4235235263, 3.83f, 4.5534, 998.432, -8403284.321435, 42332.53, "2000-1-30", "1983-11-3 02:01:22", "E", "It takes a nation of millions to hold us back", "Rebel Without a Pause", "Prophets of Rage", new byte[] { 0x45, 0x42 }, new string[] { "Party", "Fight" }, new int[] { 543, -4234 })]
365+
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new byte[] { }, new string[] { }, new int[] { })]
366+
[TestCase(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)]
367+
public async Task TestPostgresTypes(bool cBoolean, short cSmallint, int cInteger, long cBigint, float cReal, decimal cNumeric, decimal cDecimal, double cDoublePrecision, decimal cMoney, DateTime cDate, DateTime cTimestamp, string cChar, string cVarchar, string cCharacterVarying, string cText, byte[] cBytea, string[] cTextArray, int[] cIntegerArray)
366368
{
367-
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CBoolean = cBoolean, CSmallint = cSmallint, CInteger = cInteger, CBigint = cBigint, CReal = cReal, CNumeric = cNumeric, CDecimal = cDecimal, CDoublePrecision = cDoublePrecision, CDate = cDate, CTimestamp = cTimestamp, CChar = cChar, CVarchar = cVarchar, CCharacterVarying = cCharacterVarying, CText = cText, CBytea = cBytea, CTextArray = cTextArray, CIntegerArray = cIntegerArray });
369+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CBoolean = cBoolean, CSmallint = cSmallint, CInteger = cInteger, CBigint = cBigint, CReal = cReal, CNumeric = cNumeric, CDecimal = cDecimal, CDoublePrecision = cDoublePrecision, CMoney = cMoney, CDate = cDate, CTimestamp = cTimestamp, CChar = cChar, CVarchar = cVarchar, CCharacterVarying = cCharacterVarying, CText = cText, CBytea = cBytea, CTextArray = cTextArray, CIntegerArray = cIntegerArray });
368370
var expected = new QuerySql.GetPostgresTypesRow
369371
{
370372
CBoolean = cBoolean,
@@ -375,6 +377,7 @@ public async Task TestPostgresTypes(bool cBoolean, short cSmallint, int cInteger
375377
CNumeric = cNumeric,
376378
CDecimal = cDecimal,
377379
CDoublePrecision = cDoublePrecision,
380+
CMoney = cMoney,
378381
CDate = cDate,
379382
CTimestamp = cTimestamp,
380383
CChar = cChar,
@@ -399,6 +402,7 @@ private static void AssertSingularEquals(QuerySql.GetPostgresTypesRow expected,
399402
Assert.That(actual.CNumeric, Is.EqualTo(expected.CNumeric));
400403
Assert.That(actual.CDecimal, Is.EqualTo(expected.CDecimal));
401404
Assert.That(actual.CDoublePrecision, Is.EqualTo(expected.CDoublePrecision));
405+
Assert.That(actual.CMoney, Is.EqualTo(expected.CMoney));
402406
Assert.That(actual.CDate, Is.EqualTo(expected.CDate));
403407
Assert.That(actual.CTimestamp, Is.EqualTo(expected.CTimestamp));
404408
Assert.That(actual.CChar, Is.EqualTo(expected.CChar));

0 commit comments

Comments
 (0)