forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNpgsqlNodaTimeExtensions.cs
More file actions
69 lines (68 loc) · 3.07 KB
/
NpgsqlNodaTimeExtensions.cs
File metadata and controls
69 lines (68 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Data;
using NodaTime;
using Npgsql.NodaTime.Internal;
using Npgsql.TypeMapping;
using NpgsqlTypes;
// ReSharper disable once CheckNamespace
namespace Npgsql
{
/// <summary>
/// Extension adding the NodaTime plugin to an Npgsql type mapper.
/// </summary>
public static class NpgsqlNodaTimeExtensions
{
/// <summary>
/// Sets up NodaTime mappings for the PostgreSQL date/time types.
/// </summary>
/// <param name="mapper">The type mapper to set up (global or connection-specific)</param>
public static INpgsqlTypeMapper UseNodaTime(this INpgsqlTypeMapper mapper)
=> mapper
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "timestamp without time zone",
NpgsqlDbType = NpgsqlDbType.Timestamp,
DbTypes = new[] { DbType.DateTime, DbType.DateTime2 },
ClrTypes = new[] { typeof(Instant), typeof(LocalDateTime), typeof(DateTime) },
InferredDbType = DbType.DateTime,
TypeHandlerFactory = new TimestampHandlerFactory()
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "timestamp with time zone",
NpgsqlDbType = NpgsqlDbType.TimestampTz,
ClrTypes = new[] { typeof(ZonedDateTime), typeof(OffsetDateTime), typeof(DateTimeOffset) },
TypeHandlerFactory = new TimestampTzHandlerFactory()
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "date",
NpgsqlDbType = NpgsqlDbType.Date,
DbTypes = new[] { DbType.Date },
ClrTypes = new[] { typeof(LocalDate), typeof(NpgsqlDate) },
TypeHandlerFactory = new DateHandlerFactory()
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "time without time zone",
NpgsqlDbType = NpgsqlDbType.Time,
DbTypes = new[] { DbType.Time },
ClrTypes = new[] { typeof(LocalTime) },
TypeHandlerFactory = new TimeHandlerFactory()
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "time with time zone",
NpgsqlDbType = NpgsqlDbType.TimeTz,
ClrTypes = new[] { typeof(OffsetTime) },
TypeHandlerFactory = new TimeTzHandlerFactory()
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "interval",
NpgsqlDbType = NpgsqlDbType.Interval,
ClrTypes = new[] { typeof(Period), typeof(Duration), typeof(TimeSpan), typeof(NpgsqlTimeSpan) },
TypeHandlerFactory = new IntervalHandlerFactory()
}.Build());
}
}