Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable DateOnly / TimeOnly support #2080

Merged
merged 1 commit into from
Apr 26, 2024
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
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
Loading