forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestampTzInstantMapping.cs
More file actions
156 lines (134 loc) · 8.15 KB
/
TimestampTzInstantMapping.cs
File metadata and controls
156 lines (134 loc) · 8.15 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
// ReSharper disable once CheckNamespace
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class TimestampTzInstantMapping : NpgsqlTypeMapping
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static TimestampTzInstantMapping Default { get; } = new();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public TimestampTzInstantMapping()
: base("timestamp with time zone", typeof(Instant), NpgsqlDbType.TimestampTz, JsonInstantReaderWriter.Instance)
{
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected TimestampTzInstantMapping(RelationalTypeMappingParameters parameters)
: base(parameters, NpgsqlDbType.TimestampTz)
{
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new TimestampTzInstantMapping(parameters);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override RelationalTypeMapping WithStoreTypeAndSize(string storeType, int? size)
=> new TimestampTzInstantMapping(Parameters.WithStoreTypeAndSize(storeType, size));
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string ProcessStoreType(RelationalTypeMappingParameters parameters, string storeType, string _)
=> parameters.Precision is null ? storeType : $"timestamp({parameters.Precision}) with time zone";
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
=> $"TIMESTAMPTZ '{Format((Instant)value)}'";
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
=> $@"""{Format((Instant)value)}""";
private static string Format(Instant instant)
{
if (!NpgsqlNodaTimeTypeMappingSourcePlugin.DisableDateTimeInfinityConversions)
{
if (instant == Instant.MinValue)
{
return "-infinity";
}
if (instant == Instant.MaxValue)
{
return "infinity";
}
}
return InstantPattern.ExtendedIso.Format(instant);
}
internal static Expression GenerateCodeLiteral(Instant instant)
=> Expression.Call(_fromUnixTimeTicks, Expression.Constant(instant.ToUnixTimeTicks()));
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression GenerateCodeLiteral(object value)
=> GenerateCodeLiteral((Instant)value);
private static readonly MethodInfo _fromUnixTimeTicks
= typeof(Instant).GetRuntimeMethod(nameof(Instant.FromUnixTimeTicks), [typeof(long)])!;
private sealed class JsonInstantReaderWriter : JsonValueReaderWriter<Instant>
{
private static readonly PropertyInfo InstanceProperty = typeof(JsonInstantReaderWriter).GetProperty(nameof(Instance))!;
public static JsonInstantReaderWriter Instance { get; } = new();
public override Instant FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
{
var s = manager.CurrentReader.GetString()!;
if (!NpgsqlNodaTimeTypeMappingSourcePlugin.DisableDateTimeInfinityConversions)
{
switch (s)
{
case "-infinity":
return Instant.MinValue;
case "infinity":
return Instant.MaxValue;
}
}
return InstantPattern.ExtendedIso.Parse(s).GetValueOrThrow();
}
public override void ToJsonTyped(Utf8JsonWriter writer, Instant value)
=> writer.WriteStringValue(Format(value));
/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
}
}