-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
returned DateOnlyTypeHandler and TimeOnlyTypeHandler
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Byndyusoft.Data.Relational/TypeHandlers/DateOnlyTypeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using System.Data; | ||
using Dapper; | ||
|
||
namespace Byndyusoft.Data.Relational.TypeHandlers | ||
{ | ||
public class DateOnlyTypeHandler : SqlMapper.TypeHandler<DateOnly> | ||
{ | ||
public override DateOnly Parse(object value) | ||
{ | ||
return value switch | ||
{ | ||
DateOnly dateOnly => dateOnly, | ||
DateTime dateTime => DateOnly.FromDateTime(dateTime), | ||
_ => default | ||
}; | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, DateOnly value) | ||
{ | ||
parameter.DbType = DbType.Date; | ||
parameter.Value = value.ToDateTime(TimeOnly.MinValue); | ||
} | ||
} | ||
} | ||
|
||
#endif |
29 changes: 29 additions & 0 deletions
29
src/Byndyusoft.Data.Relational/TypeHandlers/TimeOnlyTypeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using System.Data; | ||
using Dapper; | ||
|
||
namespace Byndyusoft.Data.Relational.TypeHandlers | ||
{ | ||
public class TimeOnlyTypeHandler : SqlMapper.TypeHandler<TimeOnly> | ||
{ | ||
public override TimeOnly Parse(object value) | ||
{ | ||
return value switch | ||
{ | ||
TimeOnly time => time, | ||
DateTime date => TimeOnly.FromDateTime(date), | ||
TimeSpan span => TimeOnly.FromTimeSpan(span), | ||
_ => default | ||
}; | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, TimeOnly value) | ||
{ | ||
parameter.DbType = DbType.Time; | ||
parameter.Value = value; | ||
} | ||
} | ||
} | ||
#endif |