Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public IntervalMultirangeMapping(Type clrType, IntervalRangeMapping intervalRang
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected IntervalMultirangeMapping(RelationalTypeMappingParameters parameters, IntervalRangeMapping intervalRangeMapping)
: base(parameters, NpgsqlDbType.DateMultirange)
: base(parameters, NpgsqlDbType.TimestampTzMultirange)
{
_intervalRangeMapping = intervalRangeMapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected TimestampLocalDateTimeMapping(RelationalTypeMappingParameters paramete
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new TimestampLocalDateTimeMapping(Parameters);
=> new TimestampLocalDateTimeMapping(parameters);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private sealed class JsonZonedDateTimeReaderWriter : JsonValueReaderWriter<Zoned
public static JsonZonedDateTimeReaderWriter Instance { get; } = new();

public override ZonedDateTime FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> Pattern.Parse(manager.CurrentReader.GetString()!).GetValueOrThrow();
=> OffsetDateTimePattern.ExtendedIso.Parse(manager.CurrentReader.GetString()!).GetValueOrThrow().InFixedZone();

public override void ToJsonTyped(Utf8JsonWriter writer, ZonedDateTime value)
=> writer.WriteStringValue(Pattern.Format(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;

Expand Down Expand Up @@ -66,7 +67,7 @@ protected override string ProcessStoreType(RelationalTypeMappingParameters param
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
=> FormattableString.Invariant($"TIMETZ '{(DateTimeOffset)value:HH:mm:ss.FFFFFFz}'");
=> $"TIMETZ '{Format((DateTimeOffset)value)}'";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -75,7 +76,12 @@ protected override string GenerateNonNullSqlLiteral(object value)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
=> FormattableString.Invariant(@$"{(DateTimeOffset)value:HH:mm:ss.FFFFFFz}");
=> Format((DateTimeOffset)value);

private static string Format(DateTimeOffset value)
=> value.ToString(
value.Offset.Minutes == 0 ? "HH:mm:ss.FFFFFFz" : "HH:mm:ss.FFFFFFzzz",
CultureInfo.InvariantCulture);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -115,7 +121,7 @@ public override DateTimeOffset FromJsonTyped(ref Utf8JsonReaderManager manager,
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, DateTimeOffset value)
=> writer.WriteStringValue(value.ToString("HH:mm:ss.FFFFFFz"));
=> writer.WriteStringValue(Format(value));

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.PG.FunctionalTests/JsonTypesNpgsqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ public virtual Task Can_read_write_collection_of_nullable_DateTimeOffset_JSON_va

[ConditionalTheory]
[InlineData("12:34:56.123456+05:00", """{"Prop":"12:34:56.123456\u002B5"}""")]
[InlineData("12:34:56.123456+05:30", """{"Prop":"12:34:56.123456\u002B05:30"}""")]
[InlineData("12:34:56.123456-03:30", """{"Prop":"12:34:56.123456-03:30"}""")]
[InlineData("12:34:56.123456-00:30", """{"Prop":"12:34:56.123456-00:30"}""")]
public virtual void Can_read_write_timetz_JSON_values(string value, string json)
=> Can_read_and_write_JSON_property_value<DateTimeOffsetType, DateTimeOffset>(
b => b.HasColumnType("timetz"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using NodaTime;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;

namespace Microsoft.EntityFrameworkCore.Query;

public class ZonedDateTimeJsonQueryNpgsqlTest(NonSharedFixture fixture)
: NonSharedModelTestBase(fixture), IClassFixture<NonSharedFixture>
{
protected override string StoreName
=> "ZonedDateTimeJsonQueryTest";

protected override ITestStoreFactory TestStoreFactory
=> new NpgsqlTestStoreFactory(useConnectionString: true);

[ConditionalFact]
public async Task Scalar_and_whole_document()
{
var contextFactory = await Initialize();
await using var context = contextFactory.CreateContext();
var value = Earlier.InUtc();

Assert.Equal(
value,
await context.Set<Container>().Where(e => e.Details.Value == value).Select(e => e.Details.Value).SingleAsync());

var entity = await context.Set<Container>().AsNoTracking().SingleAsync();
var expected = Earlier.InZone(DateTimeZoneProviders.Tzdb["Europe/Berlin"]).ToOffsetDateTime().InFixedZone();
Assert.Equal(expected, entity.Details.Value);
Assert.Null(entity.Details.NullableValue);

AssertSql(
"""
@value='2018-10-28T00:30:00 UTC (+00)' (DbType = DateTime)

SELECT CAST(c."Details" ->> 'Value' AS timestamp with time zone)
FROM "Container" AS c
WHERE (CAST(c."Details" ->> 'Value' AS timestamp with time zone)) = @value
LIMIT 2
""",
//
"""
SELECT c."Id", c."Details", c."Items"
FROM "Container" AS c
LIMIT 2
""");
}

[ConditionalFact]
public async Task Recordset_and_primitive_collection()
{
var contextFactory = await Initialize();
await using var context = contextFactory.CreateContext();
var value = Later.InUtc();

Assert.Equal(
value,
await context.Set<Container>().SelectMany(e => e.Items)
.Where(e => e.Value == value).Select(e => e.Value).SingleAsync());
Assert.Equal(
value,
await context.Set<Container>().SelectMany(e => e.Details.Values).Where(e => e == value).SingleAsync());
Assert.Equal(value, await context.Set<Container>().Select(e => e.Details.Values[1]).SingleAsync());

AssertSql(
"""
@value='2018-10-28T01:30:00 UTC (+00)' (DbType = DateTime)

SELECT i."Value"
FROM "Container" AS c
JOIN LATERAL ROWS FROM (jsonb_to_recordset(c."Items") AS ("Value" timestamp with time zone)) WITH ORDINALITY AS i ON TRUE
WHERE i."Value" = @value
LIMIT 2
""",
//
"""
@value='2018-10-28T01:30:00 UTC (+00)' (DbType = DateTime)

SELECT v.element::timestamptz
FROM "Container" AS c
JOIN LATERAL jsonb_array_elements_text(c."Details" -> 'Values') WITH ORDINALITY AS v(element) ON TRUE
WHERE v.element::timestamptz = @value
LIMIT 2
""",
//
"""
SELECT CAST(c."Details" #>> '{Values,1}' AS timestamp with time zone)
FROM "Container" AS c
LIMIT 2
""");
}

[ConditionalTheory]
[InlineData("2018-10-28T00:30:00Z")]
[InlineData("2018-10-28T00:30:00.123456Z")]
public async Task Existing_UTC_document(string text)
{
var contextFactory = await Initialize();
await using var context = contextFactory.CreateContext();
var json = $$"""{"Value":"{{text}}","NullableValue":null,"Values":[]}""";
await context.Database.ExecuteSqlAsync($"""UPDATE "Container" SET "Details" = {json}::jsonb""");
ListLoggerFactory.Clear();

var expected = NodaTime.Text.InstantPattern.ExtendedIso.Parse(text).Value.InUtc();
Assert.Equal(expected, (await context.Set<Container>().AsNoTracking().SingleAsync()).Details.Value);
Assert.Equal(expected, await context.Set<Container>().Select(e => e.Details.Value).SingleAsync());

AssertSql(
"""
SELECT c."Id", c."Details", c."Items"
FROM "Container" AS c
LIMIT 2
""",
//
"""
SELECT CAST(c."Details" ->> 'Value' AS timestamp with time zone)
FROM "Container" AS c
LIMIT 2
""");
}

private async Task<ContextFactory<DbContext>> Initialize()
{
var contextFactory = await InitializeAsync<DbContext>(
onModelCreating: modelBuilder => modelBuilder.Entity<Container>(b =>
{
b.ComplexProperty(e => e.Details).ToJson();
b.ComplexCollection(e => e.Items).ToJson();
}),
onConfiguring: b => new NpgsqlDbContextOptionsBuilder(b).UseNodaTime(),
addServices: s => s.AddEntityFrameworkNpgsqlNodaTime(),
seed: async context =>
{
var zone = DateTimeZoneProviders.Tzdb["Europe/Berlin"];
context.Add(
new Container
{
Details = new Details
{
Value = Earlier.InZone(zone),
Values = [Earlier.InZone(zone), Later.InZone(zone)]
},
Items = [new Details { Value = Earlier.InZone(zone) }, new Details { Value = Later.InZone(zone) }]
});
await context.SaveChangesAsync();
});

ListLoggerFactory.Clear();
return contextFactory;
}

private void AssertSql(params string[] expected)
=> ((TestSqlLoggerFactory)ListLoggerFactory).AssertBaseline(expected);

private static readonly Instant Earlier = Instant.FromUtc(2018, 10, 28, 0, 30);
private static readonly Instant Later = Instant.FromUtc(2018, 10, 28, 1, 30);

public class Container
{
public int Id { get; set; }
public Details Details { get; set; } = null!;
public List<Details> Items { get; set; } = [];
}

public class Details
{
public ZonedDateTime Value { get; set; }
public ZonedDateTime? NullableValue { get; set; }
public List<ZonedDateTime> Values { get; set; } = [];
}
}
94 changes: 90 additions & 4 deletions test/EFCore.PG.Tests/Storage/NpgsqlNodaTimeTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ public void Timestamp_maps_to_LocalDateTime_by_default()
Assert.Same(typeof(LocalDateTime), GetMapping("timestamp without time zone").ClrType);
}

[Theory]
[InlineData("timestamp")]
[InlineData("timestamp without time zone")]
public void LocalDateTime_with_precision(string storeType)
{
var mapping = Mapper.FindMapping(typeof(LocalDateTime), storeType, precision: 3)!;

Assert.Equal(3, mapping.Precision);
Assert.Equal("timestamp(3) without time zone", mapping.StoreType);
}

// Mapping Instant to timestamp should only be possible in legacy mode.
// However, when upgrading to 6.0 with existing migrations, model snapshots still contain old mappings (Instant mapped to timestamp),
// and EF Core's model differ expects type mappings to be found for these. See https://github.com/dotnet/efcore/issues/26168.
Expand Down Expand Up @@ -247,15 +258,18 @@ public void GenerateSqlLiteral_returns_timestamptz_Instant_infinity_literal()
Assert.Equal("TIMESTAMPTZ 'infinity'", mapping.GenerateSqlLiteral(Instant.MaxValue));
}

[Fact]
public void GenerateSqlLiteral_returns_ZonedDateTime_literal()
[Theory]
[InlineData("UTC+02")]
[InlineData("Europe/Berlin")]
public void GenerateSqlLiteral_returns_ZonedDateTime_literal(string zoneId)
{
var mapping = GetMapping(typeof(ZonedDateTime));
var mapping = (NpgsqlTypeMapping)GetMapping(typeof(ZonedDateTime));
Assert.Equal("timestamp with time zone", mapping.StoreType);

var zonedDateTime = (new LocalDateTime(2018, 4, 20, 10, 31, 33, 666) + Period.FromTicks(6660))
.InZone(DateTimeZone.ForOffset(Offset.FromHours(2)), Resolvers.LenientResolver);
.InZone(DateTimeZoneProviders.Tzdb[zoneId], Resolvers.LenientResolver);
Assert.Equal("TIMESTAMPTZ '2018-04-20T10:31:33.666666+02'", mapping.GenerateSqlLiteral(zonedDateTime));
Assert.Equal("\"2018-04-20T10:31:33.666666+02\"", mapping.GenerateEmbeddedSqlLiteral(zonedDateTime));
}

[Fact]
Expand Down Expand Up @@ -340,6 +354,63 @@ public void Instant_json_infinity()
Assert.Equal(Instant.MinValue, readerWriter.FromJson(ref readerManager, existingObject: null));
}

[Theory]
[InlineData("Europe/Berlin", "2018-04-20T08:31:33Z", "2018-04-20T10:31:33+02")]
[InlineData("Europe/Berlin", "2018-01-20T09:31:33Z", "2018-01-20T10:31:33+01")]
[InlineData("Europe/Berlin", "2018-10-28T00:30:00Z", "2018-10-28T02:30:00+02")]
[InlineData("Europe/Berlin", "2018-10-28T01:30:00Z", "2018-10-28T02:30:00+01")]
[InlineData("America/New_York", "2018-04-20T14:31:33Z", "2018-04-20T10:31:33-04")]
[InlineData("UTC", "2018-04-20T10:31:33.123456Z", "2018-04-20T10:31:33.123456Z")]
[InlineData("UTC+05:30", "2018-04-20T05:01:33Z", "2018-04-20T10:31:33+05:30")]
[InlineData("UTC-00:30", "2018-04-20T11:01:33Z", "2018-04-20T10:31:33-00:30")]
public void ZonedDateTime_json(string zoneId, string instantString, string expected)
{
var readerWriter = GetMapping(typeof(ZonedDateTime)).JsonValueReaderWriter!;
var value = InstantPattern.ExtendedIso.Parse(instantString).GetValueOrThrow().InZone(DateTimeZoneProviders.Tzdb[zoneId]);

var json = readerWriter.ToJsonString(value);
Assert.Equal(expected, JsonSerializer.Deserialize<string>(json));

var readerManager = new Utf8JsonReaderManager(new JsonReaderData(Encoding.UTF8.GetBytes(json)), null);
readerManager.MoveNext();
Assert.Equal(value.ToOffsetDateTime().InFixedZone(), readerWriter.FromJson(ref readerManager, existingObject: null));
}

[Theory]
[InlineData("UTC")]
[InlineData("Europe/Berlin")]
public void ZonedDateTime_json_preserves_existing_format_and_precision(string zoneId)
{
var value = InstantPattern.ExtendedIso.Parse("2018-04-20T10:31:33.123456789Z").Value
.InZone(DateTimeZoneProviders.Tzdb[zoneId]);
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFo<G>", DateTimeZoneProviders.Tzdb);
var readerWriter = GetMapping(typeof(ZonedDateTime)).JsonValueReaderWriter!;
var expected = pattern.Format(value);

Assert.Equal(expected, JsonSerializer.Deserialize<string>(readerWriter.ToJsonString(value)));

if (zoneId == "UTC")
{
Assert.Equal(
value.PlusNanoseconds(-789),
pattern.Parse(JsonSerializer.Deserialize<string>(readerWriter.ToJsonString(value))!).Value);
}
}

[Theory]
[InlineData("2018-04-20T10:31:33Z")]
[InlineData("2018-04-20T10:31:33.123456Z")]
public void ZonedDateTime_json_reads_legacy_UTC_values(string value)
{
var readerWriter = GetMapping(typeof(ZonedDateTime)).JsonValueReaderWriter!;
var expected = InstantPattern.ExtendedIso.Parse(value).GetValueOrThrow().InUtc();
var readerManager = new Utf8JsonReaderManager(new JsonReaderData(Encoding.UTF8.GetBytes($"\"{value}\"")), null);
readerManager.MoveNext();

Assert.Equal(expected, readerWriter.FromJson(ref readerManager, existingObject: null));
}

[Fact]
public void Interval_is_properly_mapped()
{
Expand Down Expand Up @@ -384,6 +455,21 @@ public void Interval_array_is_properly_mapped()
public void Interval_list_is_properly_mapped()
=> Assert.Equal("tstzmultirange", GetMapping(typeof(List<Interval>)).StoreType);

[Theory]
[InlineData(typeof(Interval[]))]
[InlineData(typeof(List<Interval>))]
public void Interval_multirange_clone_preserves_NpgsqlDbType(Type clrType)
{
var mapping = GetMapping(clrType).WithStoreTypeAndSize("tstzmultirange", null);

Assert.Equal("tstzmultirange", mapping.StoreType);
Assert.Equal(NpgsqlDbType.TimestampTzMultirange, ((NpgsqlTypeMapping)mapping).NpgsqlDbType);

using var command = new NpgsqlCommand();
var parameter = (NpgsqlParameter)mapping.CreateParameter(command, "p", null);
Assert.Equal(NpgsqlDbType.TimestampTzMultirange, parameter.NpgsqlDbType);
}

[Fact]
public void GenerateSqlLiteral_returns_Interval_array_literal()
{
Expand Down
Loading
Loading