Skip to content

Commit

Permalink
disable DateOnly / TimeOnly support - see #2071 / #2072
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Apr 26, 2024
1 parent 2fa046c commit 5ccac97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static implicit operator TypeMapEntry(DbType dbType)
static SqlMapper()
{
typeMap = new Dictionary<Type, TypeMapEntry>(41
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER && DATEONLY
+ 4 // {Date|Time}Only[?]
#endif
)
Expand Down Expand Up @@ -248,7 +248,7 @@ static SqlMapper()
[typeof(SqlDecimal?)] = TypeMapEntry.DecimalFieldValue,
[typeof(SqlMoney)] = TypeMapEntry.DecimalFieldValue,
[typeof(SqlMoney?)] = TypeMapEntry.DecimalFieldValue,
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER && DATEONLY
[typeof(DateOnly)] = TypeMapEntry.DoNotSetFieldValue,
[typeof(TimeOnly)] = TypeMapEntry.DoNotSetFieldValue,
[typeof(DateOnly?)] = TypeMapEntry.DoNotSetFieldValue,
Expand Down
26 changes: 22 additions & 4 deletions tests/Dapper.Tests/DateTimeOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ namespace Dapper.Tests;
[Collection("DateTimeOnlyTests")]
public sealed class SystemSqlClientDateTimeOnlyTests : DateTimeOnlyTests<SystemSqlClientProvider> { }
*/
#if MSSQLCLIENT
#if MSSQLCLIENT && DATEONLY
[Collection("DateTimeOnlyTests")]
public sealed class MicrosoftSqlClientDateTimeOnlyTests : DateTimeOnlyTests<MicrosoftSqlClientProvider> { }
#endif
public abstract class DateTimeOnlyTests<TProvider> : TestBase<TProvider> where TProvider : DatabaseProvider
{
public class HazDateTimeOnly
{
public string Name { get; set; }
public DateOnly Date { get; set; }
public TimeOnly Time { get; set; }
public DateOnly? NDate { get; set; }
public TimeOnly? NTime { get; set; }
}

[Fact]
Expand All @@ -27,12 +30,18 @@ public void TypedInOut()
var now = DateTime.Now;
var args = new HazDateTimeOnly
{
Name = nameof(TypedInOut),
Date = DateOnly.FromDateTime(now),
Time = TimeOnly.FromDateTime(now),
NDate = DateOnly.FromDateTime(now),
NTime = TimeOnly.FromDateTime(now),
};
var row = connection.QuerySingle<HazDateTimeOnly>("select @date as [Date], @time as [Time]", args);
var row = connection.QuerySingle<HazDateTimeOnly>("select @name as [Name], @date as [Date], @time as [Time], @ndate as [NDate], @ntime as [NTime]", args);
Assert.Equal(args.Name, row.Name);
Assert.Equal(args.Date, row.Date);
Assert.Equal(args.Time, row.Time);
Assert.Equal(args.NDate, row.NDate);
Assert.Equal(args.NTime, row.NTime);
}

[Fact]
Expand All @@ -41,24 +50,33 @@ public async Task TypedInOutAsync()
var now = DateTime.Now;
var args = new HazDateTimeOnly
{
Name = nameof(TypedInOutAsync),
Date = DateOnly.FromDateTime(now),
Time = TimeOnly.FromDateTime(now),
NDate = DateOnly.FromDateTime(now),
NTime = TimeOnly.FromDateTime(now),
};
var row = await connection.QuerySingleAsync<HazDateTimeOnly>("select @date as [Date], @time as [Time]", args);
var row = await connection.QuerySingleAsync<HazDateTimeOnly>("select @name as [Name], @date as [Date], @time as [Time], @ndate as [NDate], @ntime as [NTime]", args);
Assert.Equal(args.Name, row.Name);
Assert.Equal(args.Date, row.Date);
Assert.Equal(args.Time, row.Time);
Assert.Equal(args.NDate, row.NDate);
Assert.Equal(args.NTime, row.NTime);
}

[Fact]
public void UntypedInOut()
{
var now = DateTime.Now;
var args = new DynamicParameters();
var name = nameof(UntypedInOut);
var date = DateOnly.FromDateTime(now);
var time = TimeOnly.FromDateTime(now);
args.Add("name", name);
args.Add("date", date);
args.Add("time", time);
var row = connection.QuerySingle<dynamic>("select @date as [Date], @time as [Time]", args);
var row = connection.QuerySingle<dynamic>("select @name as [Name], @date as [Date], @time as [Time]", args);
Assert.Equal(name, (string)row.Name);
// untyped, observation is that these come back as DateTime and TimeSpan
Assert.Equal(date, DateOnly.FromDateTime((DateTime)row.Date));
Assert.Equal(time, TimeOnly.FromTimeSpan((TimeSpan)row.Time));
Expand Down

0 comments on commit 5ccac97

Please sign in to comment.