-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eea7e31
commit d5b35e6
Showing
2 changed files
with
94 additions
and
14 deletions.
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
66 changes: 66 additions & 0 deletions
66
...FrameworkCore.Firebird/Query/ExpressionTranslators/Internal/FbDateOnlyMethodTranslator.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,66 @@ | ||
/* | ||
* The contents of this file are subject to the Initial | ||
* Developer's Public License Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at | ||
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt. | ||
* | ||
* Software distributed under the License is distributed on | ||
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either | ||
* express or implied. See the License for the specific | ||
* language governing rights and limitations under the License. | ||
* | ||
* All Rights Reserved. | ||
*/ | ||
|
||
//$Authors = Jiri Cincura ([email protected]) | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace FirebirdSql.EntityFrameworkCore.Firebird.Query.ExpressionTranslators.Internal; | ||
|
||
public class SqlServerDateOnlyMethodTranslator : IMethodCallTranslator | ||
{ | ||
readonly Dictionary<MethodInfo, string> _methodInfoDatePartMapping = new() | ||
{ | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddYears), [typeof(int)]), "year" }, | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddMonths), [typeof(int)]), "month" }, | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddDays), [typeof(int)]), "day" } | ||
}; | ||
|
||
readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public SqlServerDateOnlyMethodTranslator(ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
} | ||
|
||
public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
if (_methodInfoDatePartMapping.TryGetValue(method, out var datePart) && instance != null) | ||
{ | ||
instance = _sqlExpressionFactory.ApplyDefaultTypeMapping(instance); | ||
|
||
return _sqlExpressionFactory.Function( | ||
"DATEADD", | ||
new[] { _sqlExpressionFactory.Fragment(datePart), _sqlExpressionFactory.Convert(arguments[0], typeof(int)), instance }, | ||
nullable: true, | ||
argumentsPropagateNullability: new[] { false, true, true }, | ||
instance.Type, | ||
instance.TypeMapping); | ||
} | ||
|
||
if (method.DeclaringType == typeof(DateOnly) && method.Name == nameof(DateOnly.FromDateTime) && arguments.Count == 1) | ||
{ | ||
return _sqlExpressionFactory.Convert(arguments[0], typeof(DateOnly)); | ||
} | ||
|
||
return null; | ||
} | ||
} |