Skip to content

Commit

Permalink
Feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ralmsdeveloper committed Jul 8, 2019
1 parent ffc8579 commit 8bb238d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,22 @@ FROM [Orders] AS [o]
}
}

[ConditionalFact]
public virtual void IsDate_join_fields()
{
using (var context = CreateContext())
{
var count = context.Orders.Count(c => EF.Functions.IsDate(c.CustomerID + c.OrderID));

Assert.Equal(0, count);

AssertSql(
@"SELECT COUNT(*)
FROM [Orders] AS [o]
WHERE ISDATE([o].[CustomerID] + CAST([o].[OrderID] AS nchar(5))) = CAST(1 AS bit)");
}
}

[ConditionalFact]
public void IsDate_should_throw_on_client_eval()
{
Expand Down

0 comments on commit 8bb238d

Please sign in to comment.