Skip to content

Commit e946854

Browse files
committed
Fix support for converting to and from DateOnly
1 parent 91a5d3f commit e946854

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,12 @@ private static object AutomaticConvert(object value,
885885
{
886886
return AutomaticConvertGuidToString(value);
887887
}
888+
#if NET6_0_OR_GREATER
889+
else if (fromType == StaticType.DateOnly && targetType == StaticType.DateTime)
890+
{
891+
return AutomaticConvertDateOnlyToDateTime(value);
892+
}
893+
#endif
888894
else
889895
{
890896
return (value != DBNull.Value) ? Convert.ChangeType(value, targetType) : Activator.CreateInstance(targetType);
@@ -915,6 +921,10 @@ private static object AutomaticConvertStringToGuid(object value)
915921
private static object AutomaticConvertGuidToString(object value) =>
916922
value?.ToString();
917923

924+
#if NET6_0_OR_GREATER
925+
private static object AutomaticConvertDateOnlyToDateTime(object value) =>
926+
(value is DateOnly dateOnly ? dateOnly.ToDateTime(default(TimeOnly)) : null);
927+
#endif
918928
#endregion
919929
}
920930
}

RepoDb.Core/RepoDb/Extensions/ExpressionExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public static object GetValue(this NewExpression expression)
382382
if (expression.Arguments.Count > 0)
383383
{
384384
return Activator.CreateInstance(expression.Type,
385-
expression.Arguments.Select(arg => arg.GetValue()));
385+
expression.Arguments.Select(arg => arg.GetValue()).ToArray());
386386
}
387387
else
388388
{

RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
using RepoDb.Enumerations;
2+
using RepoDb.Exceptions;
3+
using RepoDb.Extensions;
4+
using RepoDb.Interfaces;
5+
using RepoDb.Resolvers;
16
using System;
27
using System.Collections.Generic;
38
using System.Data;
49
using System.Data.Common;
510
using System.Linq;
611
using System.Linq.Expressions;
712
using System.Reflection;
8-
using RepoDb.Enumerations;
9-
using RepoDb.Exceptions;
10-
using RepoDb.Extensions;
11-
using RepoDb.Interfaces;
12-
using RepoDb.Resolvers;
1313

1414
namespace RepoDb.Reflection
1515
{
@@ -510,7 +510,15 @@ internal static MethodInfo GetDateTimeTimeOfDayPropertyGetMethod() =>
510510
/// <returns></returns>
511511
internal static MethodInfo GetDateOnlyFromDateTimeStaticMethod() =>
512512
StaticType.DateOnly.GetMethod("FromDateTime");
513+
514+
/// <summary>
515+
///
516+
/// </summary>
517+
/// <returns></returns>
518+
internal static MethodInfo GetDateTimeFromDateOnlyMethod() =>
519+
StaticType.DateOnly.GetMethod("ToDateTime", new Type[] { StaticType.TimeOnly });
513520
#endif
521+
514522
/// <summary>
515523
///
516524
/// </summary>
@@ -548,6 +556,24 @@ internal static Expression ConvertExpressionToNullableGetValueOrDefaultExpressio
548556
return expression;
549557
}
550558

559+
#if NET6_0_OR_GREATER
560+
internal static Expression ConvertExpressionToNullableGetValueOrDefaultExpression(Func<Expression, Expression> converter, Expression expression)
561+
{
562+
if (Nullable.GetUnderlyingType(expression.Type) != null)
563+
{
564+
var converted = converter(ConvertExpressionToNullableGetValueOrDefaultExpression(expression));
565+
var nullableType = typeof(Nullable<>).MakeGenericType(converted.Type);
566+
return Expression.Condition(
567+
Expression.Property(expression, nameof(Nullable<int>.HasValue)),
568+
Expression.Convert(converted, nullableType),
569+
Expression.Constant(null, nullableType)
570+
);
571+
}
572+
573+
return converter(expression);
574+
}
575+
#endif
576+
551577
/// <summary>
552578
///
553579
/// </summary>
@@ -601,7 +627,15 @@ internal static Expression ConvertExpressionToDateTimeToTimeSpanExpression(Expre
601627
/// <param name="expression"></param>
602628
/// <returns></returns>
603629
internal static Expression ConvertExpressionToDateTimeToDateOnlyExpression(Expression expression) =>
604-
ConvertExpressionToNullableGetValueOrDefaultExpression(ConvertExpressionToDateOnlyFromDateTimeExpression(expression));
630+
ConvertExpressionToNullableGetValueOrDefaultExpression(ConvertExpressionToDateTimeFromDateOnlyExpression, expression);
631+
632+
/// <summary>
633+
///
634+
/// </summary>
635+
/// <param name="expression"></param>
636+
/// <returns></returns>
637+
internal static Expression ConvertExpressionToDateOnlyToDateTimeExpression(Expression expression) =>
638+
ConvertExpressionToNullableGetValueOrDefaultExpression(ConvertExpressionToDateOnlyFromDateTimeExpression, expression);
605639
#endif
606640
/// <summary>
607641
///
@@ -625,6 +659,14 @@ internal static Expression ConvertExpressionToDateTimeTimeOfDayExpression(Expres
625659
/// <param name="expression"></param>
626660
/// <returns></returns>
627661
internal static Expression ConvertExpressionToDateOnlyFromDateTimeExpression(Expression expression) =>
662+
Expression.Call(expression, GetDateTimeFromDateOnlyMethod(), Expression.Constant(default(TimeOnly)));
663+
664+
/// <summary>
665+
///
666+
/// </summary>
667+
/// <param name="expression"></param>
668+
/// <returns></returns>
669+
internal static Expression ConvertExpressionToDateTimeFromDateOnlyExpression(Expression expression) =>
628670
Expression.Call(null, GetDateOnlyFromDateTimeStaticMethod(), expression);
629671
#endif
630672
/// <summary>
@@ -907,6 +949,12 @@ internal static Expression ConvertExpressionWithAutomaticConversion(Expression e
907949
{
908950
expression = ConvertExpressionToDateTimeToDateOnlyExpression(expression);
909951
}
952+
953+
// DateOnly to DateTime
954+
else if (fromType == StaticType.DateOnly && toType == StaticType.DateTime)
955+
{
956+
expression = ConvertExpressionToDateOnlyToDateTimeExpression(expression);
957+
}
910958
#endif
911959
// Others
912960
else
@@ -1084,7 +1132,7 @@ internal static Expression ConvertExpressionToClassHandlerSetExpression(Expressi
10841132
return entityOrEntitiesExpression;
10851133
}
10861134

1087-
#endregion
1135+
#endregion
10881136

10891137
#region Common
10901138

RepoDb.Core/RepoDb/StaticType.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ internal static class StaticType
9595
/// Gets a type of the <see cref="System.DateTimeOffset"/> .NET CLR type.
9696
/// </summary>
9797
public static Type DateTimeOffset => typeof(DateTimeOffset);
98-
99-
#if NET6_0_OR_GREATER
98+
99+
#if NET6_0_OR_GREATER
100100
/// <summary>
101101
/// Gets a type of the <see cref="System.DateOnly"/> .NET CLR type.
102102
/// </summary>
@@ -105,8 +105,8 @@ internal static class StaticType
105105
/// Gets a type of the <see cref="System.TimeOnly"/> .NET CLR type.
106106
/// </summary>
107107
public static Type TimeOnly => typeof(TimeOnly);
108-
#endif
109-
108+
#endif
109+
110110
/// <summary>
111111
/// Gets a type of the <see cref="System.Data.Common.DbCommand"/> .NET CLR type.
112112
/// </summary>

0 commit comments

Comments
 (0)