Skip to content

Commit

Permalink
Merge pull request #40 from evolvedlight/add-default-mappings
Browse files Browse the repository at this point in the history
Add default mappings for types. Change visibility to public for mappings to allow compiled ef contexts
  • Loading branch information
StevenRasmussen authored Apr 6, 2024
2 parents 84d0447 + 0ca8c4e commit f205e43
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.EntityFrameworkCore.SqlServer.Storage;
using NodaTime;
using System;
using Xunit;

namespace SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests
{
public class MappingDefaultsTest
{
[Fact]
public void Duration_Defaults()
{
var durationDefault = DurationTypeMapping.Default;

Assert.NotNull(durationDefault);

Assert.Equal("time", durationDefault.StoreType);
Assert.Equal("'02:30:01'", durationDefault.GenerateSqlLiteral(Duration.FromTimeSpan(new TimeSpan(2, 30, 1))));
}

[Fact]
public void Instant_Defaults()
{
var instantDefault = InstantTypeMapping.Default;

Assert.NotNull(instantDefault);

Assert.Equal("datetime2", instantDefault.StoreType);
Assert.Equal("'2024-02-14T00:14:00.0000000Z'", instantDefault.GenerateSqlLiteral(Instant.FromUtc(2024, 02, 14, 0, 14)));
}

[Fact]
public void LocalDateTime_Defaults()
{
var localDateTimeDefault = LocalDateTimeTypeMapping.Default;

Assert.NotNull(localDateTimeDefault);

Assert.Equal("datetime2", localDateTimeDefault.StoreType);
Assert.Equal("'2024-04-01T13:15:00.0000000'", localDateTimeDefault.GenerateSqlLiteral(new LocalDateTime(2024, 04, 01, 13, 15)));
}

[Fact]
public void LocalDate_Defaults()
{
var localDateDefault = LocalDateTypeMapping.Default;

Assert.NotNull(localDateDefault);

Assert.Equal("date", localDateDefault.StoreType);
Assert.Equal("'2024-03-30'", localDateDefault.GenerateSqlLiteral(new LocalDate(2024, 03, 30)));
}

[Fact]
public void LocalTime_Defaults()
{
var localTimeDefault = LocalTimeTypeMapping.Default;

Assert.NotNull(localTimeDefault);

Assert.Equal("time", localTimeDefault.StoreType);
Assert.Equal("'19:40:00'", localTimeDefault.GenerateSqlLiteral(new LocalTime(19, 40)));
}

[Fact]
public void OffsetDateTime_Defaults()
{
var offsetDateTimeDefault = OffsetDateTimeTypeMapping.Default;

Assert.NotNull(offsetDateTimeDefault);

Assert.Equal("datetimeoffset", offsetDateTimeDefault.StoreType);
var localDateTime = new LocalDateTime(2024, 04, 01, 17, 15);
Assert.Equal("'2024-04-01T17:15:00.0000000+02:00'", offsetDateTimeDefault.GenerateSqlLiteral(new OffsetDateTime(localDateTime, Offset.FromHours(2))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class DurationTypeMapping : RelationalTypeMapping
public class DurationTypeMapping : RelationalTypeMapping
{
/// <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 DurationTypeMapping Default { get; } = new();

public DurationTypeMapping()
: base(CreateRelationalTypeMappingParameters())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public DurationValueConverter()
{
}

private static TimeSpan toProvider(Duration duration)
public static TimeSpan toProvider(Duration duration)
{
return duration.ToTimeSpan();
}

private static Duration fromProvider(TimeSpan timeSpan)
public static Duration fromProvider(TimeSpan timeSpan)
{
return Duration.FromTimeSpan(timeSpan);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using Microsoft.EntityFrameworkCore.Storage;
using NodaTime;
using SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage;
using DateTimeTypeMapping = SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage.DateTimeTypeMapping;

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class InstantTypeMapping : DateTimeTypeMapping
public class InstantTypeMapping : DateTimeTypeMapping
{
/// <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 InstantTypeMapping Default { get; } = new(SqlServerDateTimeTypes.DateTime2);

public InstantTypeMapping(string storeType)
: base(storeType, typeof(Instant), new InstantValueConverter())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public InstantValueConverter()
{
}

private static DateTime toProvider(Instant instant)
public static DateTime toProvider(Instant instant)
{
return instant.ToDateTimeUtc();
}

private static Instant fromProvider(DateTime dateTime)
public static Instant fromProvider(DateTime dateTime)
{
return Instant.FromDateTimeUtc(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using Microsoft.EntityFrameworkCore.Storage;
using NodaTime;
using SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage;
using DateTimeTypeMapping = SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage.DateTimeTypeMapping;

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class LocalDateTimeTypeMapping : DateTimeTypeMapping
public class LocalDateTimeTypeMapping : DateTimeTypeMapping
{
/// <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 LocalDateTimeTypeMapping Default { get; } = new(SqlServerDateTimeTypes.DateTime2);

public LocalDateTimeTypeMapping(string storeType)
: base(storeType, typeof(LocalDateTime), new LocalDateTimeValueConverter())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public LocalDateTimeValueConverter()
{
}

private static DateTime toProvider(LocalDateTime localDateTime)
public static DateTime toProvider(LocalDateTime localDateTime)
{
return localDateTime.ToDateTimeUnspecified();
}

private static LocalDateTime fromProvider(DateTime dateTime)
public static LocalDateTime fromProvider(DateTime dateTime)
{
return LocalDateTime.FromDateTime(dateTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class LocalDateTypeMapping : RelationalTypeMapping
public class LocalDateTypeMapping : RelationalTypeMapping
{
private const string DateFormatConst = "'{0:yyyy-MM-dd}'";

/// <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 LocalDateTypeMapping Default { get; } = new();

public LocalDateTypeMapping()
: base(CreateRelationalTypeMappingParameters())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public LocalDateValueConverter()
{
}

private static DateTime toProvider(LocalDate localDate)
public static DateTime toProvider(LocalDate localDate)
{
return localDate.ToDateTimeUnspecified();
}

private static LocalDate fromProvider(DateTime dateTime)
public static LocalDate fromProvider(DateTime dateTime)
{
return LocalDate.FromDateTime(dateTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class LocalTimeTypeMapping : RelationalTypeMapping
public class LocalTimeTypeMapping : RelationalTypeMapping
{
/// <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 LocalTimeTypeMapping Default { get; } = new();

public LocalTimeTypeMapping()
: base(CreateRelationalTypeMappingParameters())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public LocalTimeValueConverter()
{
}

private static TimeSpan toProvider(LocalTime localTime)
public static TimeSpan toProvider(LocalTime localTime)
{
return new TimeSpan(localTime.TickOfDay);
}

private static LocalTime fromProvider(TimeSpan dateTime)
public static LocalTime fromProvider(TimeSpan dateTime)
{
return LocalTime.FromTicksSinceMidnight(dateTime.Ticks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@

namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
{
internal class OffsetDateTimeTypeMapping : RelationalTypeMapping
public class OffsetDateTimeTypeMapping : RelationalTypeMapping
{
private const string DateTimeOffsetFormatConst = "{0:yyyy-MM-ddTHH:mm:ss.fffffffzzz}";

/// <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 OffsetDateTimeTypeMapping Default { get; } = new();

public OffsetDateTimeTypeMapping()
: base(CreateRelationalTypeMappingParameters())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public OffsetDateTimeValueConverter()
{
}

private static DateTimeOffset toProvider(OffsetDateTime offsetDateTime)
public static DateTimeOffset toProvider(OffsetDateTime offsetDateTime)
{
return offsetDateTime.ToDateTimeOffset();
}

private static OffsetDateTime fromProvider(DateTimeOffset dateTimeOffset)
public static OffsetDateTime fromProvider(DateTimeOffset dateTimeOffset)
{
return OffsetDateTime.FromDateTimeOffset(dateTimeOffset);
}
Expand Down

0 comments on commit f205e43

Please sign in to comment.