Skip to content

Commit 58880ae

Browse files
feat: support uuid Postgres data type
1 parent 11afe81 commit 58880ae

27 files changed

Lines changed: 666 additions & 319 deletions

File tree

Drivers/DbDriver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public abstract class DbDriver
4141
"decimal",
4242
"DateTime",
4343
"TimeSpan",
44+
"Guid",
4445
"NpgsqlPoint",
4546
"NpgsqlLine",
4647
"NpgsqlLSeg",

Drivers/NpgsqlDriver.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public NpgsqlDriver(
7373
readerFn: ordinal => $"reader.GetString({ordinal})",
7474
readerArrayFn: ordinal => $"reader.GetFieldValue<string[]>({ordinal})"
7575
),
76+
["Guid"] = new(
77+
new()
78+
{
79+
{ "uuid", new(NpgsqlTypeOverride: "NpgsqlDbType.Uuid") }
80+
},
81+
readerFn: ordinal => $"reader.GetFieldValue<Guid>({ordinal})",
82+
readerArrayFn: ordinal => $"reader.GetFieldValue<Guid[]>({ordinal})"
83+
),
7684
["TimeSpan"] = new(
7785
new()
7886
{

docs/04_Postgres.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ we consider support for the different data types separately for batch inserts an
5353
| macaddr8 |||
5454
| tsvector |||
5555
| tsquery |||
56-
| uuid | ||
56+
| uuid | ||
5757
| json |||
5858
| jsonb |||
5959
| jsonpath |||
6060

61-
*** Time with time zone is not useful and not recommended to use by Postgres themselves - see [here](https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME).
61+
*** Time with time zone is not useful and not recommended to use by Postgres themselves -
62+
see [here](https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME) -
63+
so we decided not to implement support for it.
6264

6365
</details>
6466

end2end/EndToEndScaffold/Config.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public enum KnownTestType
4040
PostgresDateTimeDataTypes,
4141
PostgresArrayDataTypes,
4242
PostgresDataTypesOverride,
43+
PostgresGuidDataTypes,
4344

4445
PostgresStringCopyFrom,
4546
PostgresTransaction,
@@ -190,6 +191,7 @@ internal static class Config
190191
KnownTestType.PostgresIntegerDataTypes,
191192
KnownTestType.PostgresFloatingPointDataTypes,
192193
KnownTestType.PostgresDateTimeDataTypes,
194+
KnownTestType.PostgresGuidDataTypes,
193195
KnownTestType.PostgresArrayDataTypes,
194196
KnownTestType.PostgresGeoDataTypes,
195197
KnownTestType.PostgresGeoCopyFrom,
@@ -229,6 +231,7 @@ internal static class Config
229231
KnownTestType.PostgresIntegerDataTypes,
230232
KnownTestType.PostgresFloatingPointDataTypes,
231233
KnownTestType.PostgresDateTimeDataTypes,
234+
KnownTestType.PostgresGuidDataTypes,
232235
KnownTestType.PostgresArrayDataTypes,
233236
KnownTestType.PostgresGeoDataTypes,
234237
KnownTestType.PostgresGeoCopyFrom,

end2end/EndToEndScaffold/Templates/PostgresTests.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs
170170
[KnownTestType.PostgresArrayDataTypes] = new TestImpl
171171
{
172172
Impl = $$"""
173-
public static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
173+
private static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
174174
{
175175
get
176176
{
@@ -570,7 +570,7 @@ public async Task TestMultipleArrays()
570570
[KnownTestType.PostgresGeoDataTypes] = new TestImpl
571571
{
572572
Impl = $$"""
573-
public static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
573+
private static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
574574
{
575575
get
576576
{
@@ -630,7 +630,7 @@ void AssertSingularEquals(QuerySql.GetPostgresGeoTypesRow x, QuerySql.GetPostgre
630630
[KnownTestType.PostgresGeoCopyFrom] = new TestImpl
631631
{
632632
Impl = $$"""
633-
public static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
633+
private static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
634634
{
635635
get
636636
{
@@ -795,6 +795,41 @@ private static void AssertSingularEquals(QuerySql.GetPostgresFunctionsRow expect
795795
Assert.That(actual.MaxTimestamp, Is.EqualTo(expected.MaxTimestamp));
796796
}
797797
"""
798+
},
799+
[KnownTestType.PostgresGuidDataTypes] = new TestImpl
800+
{
801+
Impl = $$"""
802+
private static IEnumerable<TestCaseData> PostgresGuidDataTypesTestCases
803+
{
804+
get
805+
{
806+
yield return new TestCaseData(Guid.NewGuid()).SetName("Valid Guid");
807+
yield return new TestCaseData(null).SetName("Null Guid");
808+
}
809+
}
810+
811+
[Test]
812+
[TestCaseSource(nameof(PostgresGuidDataTypesTestCases))]
813+
public async Task TestPostgresGuidDataTypes(Guid? cUuid)
814+
{
815+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs
816+
{
817+
CUuid = cUuid
818+
});
819+
820+
var expected = new QuerySql.GetPostgresTypesRow
821+
{
822+
CUuid = cUuid
823+
};
824+
var actual = await QuerySql.GetPostgresTypes();
825+
AssertSingularEquals(expected, actual{{Consts.UnknownRecordValuePlaceholder}});
826+
827+
void AssertSingularEquals(QuerySql.GetPostgresTypesRow x, QuerySql.GetPostgresTypesRow y)
828+
{
829+
Assert.That(x.CUuid, Is.EqualTo(y.CUuid));
830+
}
831+
}
832+
"""
798833
}
799834
};
800835
}

end2end/EndToEndTests/NpgsqlDapperTester.generated.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public async Task TestPostgresDateTimeTypes(DateTime? cDate, TimeSpan? cTime, Da
381381
Assert.That(actual.CTimestampWithTz, Is.EqualTo(expected.CTimestampWithTz));
382382
}
383383

384-
public static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
384+
private static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
385385
{
386386
get
387387
{
@@ -443,6 +443,32 @@ private static void AssertSingularEquals(QuerySql.GetPostgresFunctionsRow expect
443443
Assert.That(actual.MaxTimestamp, Is.EqualTo(expected.MaxTimestamp));
444444
}
445445

446+
private static IEnumerable<TestCaseData> PostgresGuidDataTypesTestCases
447+
{
448+
get
449+
{
450+
yield return new TestCaseData(Guid.NewGuid()).SetName("Valid Guid");
451+
yield return new TestCaseData(null).SetName("Null Guid");
452+
}
453+
}
454+
455+
[Test]
456+
[TestCaseSource(nameof(PostgresGuidDataTypesTestCases))]
457+
public async Task TestPostgresGuidDataTypes(Guid? cUuid)
458+
{
459+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CUuid = cUuid });
460+
var expected = new QuerySql.GetPostgresTypesRow
461+
{
462+
CUuid = cUuid
463+
};
464+
var actual = await QuerySql.GetPostgresTypes();
465+
AssertSingularEquals(expected, actual);
466+
void AssertSingularEquals(QuerySql.GetPostgresTypesRow x, QuerySql.GetPostgresTypesRow y)
467+
{
468+
Assert.That(x.CUuid, Is.EqualTo(y.CUuid));
469+
}
470+
}
471+
446472
[Test]
447473
[TestCase(100, "z", "Sex Pistols", "Anarchy in the U.K", "Yoshimi Battles the Pink Robots", "Never Mind the Bollocks...")]
448474
[TestCase(10, null, null, null, null, null)]
@@ -611,7 +637,7 @@ public async Task TestArrayCopyFrom(int batchSize, byte[] cBytea)
611637
Assert.That(actual.CBytea, Is.EqualTo(expected.CBytea));
612638
}
613639

614-
public static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
640+
private static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
615641
{
616642
get
617643
{
@@ -649,7 +675,7 @@ void AssertSingularEquals(QuerySql.GetPostgresGeoTypesRow x, QuerySql.GetPostgre
649675
}
650676
}
651677

652-
public static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
678+
private static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
653679
{
654680
get
655681
{

end2end/EndToEndTests/NpgsqlTester.generated.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public async Task TestPostgresDateTimeTypes(DateTime? cDate, TimeSpan? cTime, Da
381381
Assert.That(actual.Value.CTimestampWithTz, Is.EqualTo(expected.CTimestampWithTz));
382382
}
383383

384-
public static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
384+
private static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
385385
{
386386
get
387387
{
@@ -443,6 +443,32 @@ private static void AssertSingularEquals(QuerySql.GetPostgresFunctionsRow expect
443443
Assert.That(actual.MaxTimestamp, Is.EqualTo(expected.MaxTimestamp));
444444
}
445445

446+
private static IEnumerable<TestCaseData> PostgresGuidDataTypesTestCases
447+
{
448+
get
449+
{
450+
yield return new TestCaseData(Guid.NewGuid()).SetName("Valid Guid");
451+
yield return new TestCaseData(null).SetName("Null Guid");
452+
}
453+
}
454+
455+
[Test]
456+
[TestCaseSource(nameof(PostgresGuidDataTypesTestCases))]
457+
public async Task TestPostgresGuidDataTypes(Guid? cUuid)
458+
{
459+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CUuid = cUuid });
460+
var expected = new QuerySql.GetPostgresTypesRow
461+
{
462+
CUuid = cUuid
463+
};
464+
var actual = await QuerySql.GetPostgresTypes();
465+
AssertSingularEquals(expected, actual.Value);
466+
void AssertSingularEquals(QuerySql.GetPostgresTypesRow x, QuerySql.GetPostgresTypesRow y)
467+
{
468+
Assert.That(x.CUuid, Is.EqualTo(y.CUuid));
469+
}
470+
}
471+
446472
[Test]
447473
[TestCase(100, "z", "Sex Pistols", "Anarchy in the U.K", "Yoshimi Battles the Pink Robots", "Never Mind the Bollocks...")]
448474
[TestCase(10, null, null, null, null, null)]
@@ -611,7 +637,7 @@ public async Task TestArrayCopyFrom(int batchSize, byte[] cBytea)
611637
Assert.That(actual.Value.CBytea, Is.EqualTo(expected.CBytea));
612638
}
613639

614-
public static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
640+
private static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
615641
{
616642
get
617643
{
@@ -649,7 +675,7 @@ void AssertSingularEquals(QuerySql.GetPostgresGeoTypesRow x, QuerySql.GetPostgre
649675
}
650676
}
651677

652-
public static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
678+
private static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
653679
{
654680
get
655681
{

end2end/EndToEndTestsLegacy/NpgsqlDapperTester.generated.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public async Task TestPostgresDateTimeTypes(DateTime? cDate, TimeSpan? cTime, Da
381381
Assert.That(actual.CTimestampWithTz, Is.EqualTo(expected.CTimestampWithTz));
382382
}
383383

384-
public static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
384+
private static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
385385
{
386386
get
387387
{
@@ -443,6 +443,32 @@ private static void AssertSingularEquals(QuerySql.GetPostgresFunctionsRow expect
443443
Assert.That(actual.MaxTimestamp, Is.EqualTo(expected.MaxTimestamp));
444444
}
445445

446+
private static IEnumerable<TestCaseData> PostgresGuidDataTypesTestCases
447+
{
448+
get
449+
{
450+
yield return new TestCaseData(Guid.NewGuid()).SetName("Valid Guid");
451+
yield return new TestCaseData(null).SetName("Null Guid");
452+
}
453+
}
454+
455+
[Test]
456+
[TestCaseSource(nameof(PostgresGuidDataTypesTestCases))]
457+
public async Task TestPostgresGuidDataTypes(Guid? cUuid)
458+
{
459+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CUuid = cUuid });
460+
var expected = new QuerySql.GetPostgresTypesRow
461+
{
462+
CUuid = cUuid
463+
};
464+
var actual = await QuerySql.GetPostgresTypes();
465+
AssertSingularEquals(expected, actual);
466+
void AssertSingularEquals(QuerySql.GetPostgresTypesRow x, QuerySql.GetPostgresTypesRow y)
467+
{
468+
Assert.That(x.CUuid, Is.EqualTo(y.CUuid));
469+
}
470+
}
471+
446472
[Test]
447473
[TestCase(100, "z", "Sex Pistols", "Anarchy in the U.K", "Yoshimi Battles the Pink Robots", "Never Mind the Bollocks...")]
448474
[TestCase(10, null, null, null, null, null)]
@@ -611,7 +637,7 @@ public async Task TestArrayCopyFrom(int batchSize, byte[] cBytea)
611637
Assert.That(actual.CBytea, Is.EqualTo(expected.CBytea));
612638
}
613639

614-
public static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
640+
private static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
615641
{
616642
get
617643
{
@@ -649,7 +675,7 @@ void AssertSingularEquals(QuerySql.GetPostgresGeoTypesRow x, QuerySql.GetPostgre
649675
}
650676
}
651677

652-
public static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
678+
private static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
653679
{
654680
get
655681
{

end2end/EndToEndTestsLegacy/NpgsqlTester.generated.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public async Task TestPostgresDateTimeTypes(DateTime? cDate, TimeSpan? cTime, Da
381381
Assert.That(actual.CTimestampWithTz, Is.EqualTo(expected.CTimestampWithTz));
382382
}
383383

384-
public static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
384+
private static IEnumerable<TestCaseData> PostgresArrayTypesTestCases
385385
{
386386
get
387387
{
@@ -443,6 +443,32 @@ private static void AssertSingularEquals(QuerySql.GetPostgresFunctionsRow expect
443443
Assert.That(actual.MaxTimestamp, Is.EqualTo(expected.MaxTimestamp));
444444
}
445445

446+
private static IEnumerable<TestCaseData> PostgresGuidDataTypesTestCases
447+
{
448+
get
449+
{
450+
yield return new TestCaseData(Guid.NewGuid()).SetName("Valid Guid");
451+
yield return new TestCaseData(null).SetName("Null Guid");
452+
}
453+
}
454+
455+
[Test]
456+
[TestCaseSource(nameof(PostgresGuidDataTypesTestCases))]
457+
public async Task TestPostgresGuidDataTypes(Guid? cUuid)
458+
{
459+
await QuerySql.InsertPostgresTypes(new QuerySql.InsertPostgresTypesArgs { CUuid = cUuid });
460+
var expected = new QuerySql.GetPostgresTypesRow
461+
{
462+
CUuid = cUuid
463+
};
464+
var actual = await QuerySql.GetPostgresTypes();
465+
AssertSingularEquals(expected, actual);
466+
void AssertSingularEquals(QuerySql.GetPostgresTypesRow x, QuerySql.GetPostgresTypesRow y)
467+
{
468+
Assert.That(x.CUuid, Is.EqualTo(y.CUuid));
469+
}
470+
}
471+
446472
[Test]
447473
[TestCase(100, "z", "Sex Pistols", "Anarchy in the U.K", "Yoshimi Battles the Pink Robots", "Never Mind the Bollocks...")]
448474
[TestCase(10, null, null, null, null, null)]
@@ -611,7 +637,7 @@ public async Task TestArrayCopyFrom(int batchSize, byte[] cBytea)
611637
Assert.That(actual.CBytea, Is.EqualTo(expected.CBytea));
612638
}
613639

614-
public static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
640+
private static IEnumerable<TestCaseData> PostgresGeoTypesTestCases
615641
{
616642
get
617643
{
@@ -649,7 +675,7 @@ void AssertSingularEquals(QuerySql.GetPostgresGeoTypesRow x, QuerySql.GetPostgre
649675
}
650676
}
651677

652-
public static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
678+
private static IEnumerable<TestCaseData> PostgresGeoCopyFromTestCases
653679
{
654680
get
655681
{

examples/NpgsqlDapperExample/Models.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ public class PostgresType
3737
public string? CVarchar { get; init; }
3838
public string? CCharacterVarying { get; init; }
3939
public string? CBpchar { get; init; }
40-
public byte[]? CBytea { get; init; }
4140
public string? CText { get; init; }
4241
public JsonElement? CJson { get; init; }
4342
public JsonElement? CJsonStringOverride { get; init; }
4443
public JsonElement? CJsonb { get; init; }
44+
public Guid? CUuid { get; init; }
45+
public byte[]? CBytea { get; init; }
4546
public bool[]? CBooleanArray { get; init; }
4647
public string[]? CTextArray { get; init; }
4748
public int[]? CIntegerArray { get; init; }

0 commit comments

Comments
 (0)