-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
ffc8579
commit 8bb238d
Showing
2 changed files
with
23 additions
and
37 deletions.
There are no files selected for viewing
44 changes: 7 additions & 37 deletions
44
src/EFCore.SqlServer/Query/Pipeline/SqlServerIsDateFunctionTranslator.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 |
---|---|---|
@@ -1,57 +1,27 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline; | ||
using Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Pipeline | ||
{ | ||
public class SqlServerIsDateFunctionTranslator : IMethodCallTranslator | ||
{ | ||
private const string IsDateFunctionName = "ISDATE"; | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
private static readonly MethodInfo _isDateMethodInfo | ||
= typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod( | ||
nameof(SqlServerDbFunctionsExtensions.IsDate), | ||
new[] { typeof(DbFunctions), typeof(string) }); | ||
|
||
private static readonly IDictionary<MethodInfo, string> _functionMapping | ||
= new Dictionary<MethodInfo, string> | ||
{ | ||
{_isDateMethodInfo, IsDateFunctionName }, | ||
}; | ||
|
||
public SqlServerIsDateFunctionTranslator(ISqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
public SqlExpression Translate(SqlExpression instance, MethodInfo method, IList<SqlExpression> arguments) | ||
{ | ||
if (_functionMapping.TryGetValue(method, out var _)) | ||
{ | ||
var propertyReference = arguments[1]; | ||
if (!(propertyReference is ColumnExpression)) | ||
{ | ||
throw new InvalidOperationException(SqlServerStrings.InvalidColumnNameForIsDate); | ||
} | ||
|
||
var typeMapping = ExpressionExtensions.InferTypeMapping(propertyReference); | ||
var isDateExpression = _sqlExpressionFactory.ApplyTypeMapping(propertyReference, typeMapping); | ||
|
||
return _sqlExpressionFactory.Function( | ||
IsDateFunctionName, | ||
new[] | ||
{ | ||
isDateExpression | ||
}, | ||
typeof(bool)); | ||
} | ||
|
||
return null; | ||
} | ||
=> (method.Name == nameof(SqlServerDbFunctionsExtensions.IsDate) && arguments.Count > 1) | ||
? _sqlExpressionFactory.Function( | ||
"ISDATE", | ||
arguments.Skip(1), | ||
typeof(bool)) | ||
: null; | ||
} | ||
} |
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