Skip to content

Commit

Permalink
feat(Translators): support for translating ToString() to sql server
Browse files Browse the repository at this point in the history
  • Loading branch information
laskoviymishka authored and anpete committed Dec 12, 2016
1 parent 4b60abb commit ea3f5e3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6393,6 +6393,28 @@ public virtual void Subquery_member_pushdown_does_not_change_original_subquery_m
.Take(10));
}

[ConditionalFact]
public virtual void Query_expression_with_to_string_and_contains()
{
AssertQuery<Order>(
os => os.Where(o => o.OrderDate != null && o.EmployeeID.Value.ToString().Contains("10"))
.Select(o => new Order
{
CustomerID = o.CustomerID
}));
}

[ConditionalFact]
public virtual void Select_expression_to_string()
{
AssertQuery<Order>(
os => os.Where(o => o.OrderDate != null)
.Select(o => new Order
{
ShipName = o.EmployeeID.Value.ToString()
}));
}

[ConditionalFact]
public virtual void Select_expression_date_add_year()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Query\ExpressionTranslators\Internal\SqlServerStringToLowerTranslator.cs" />
<Compile Include="Query\ExpressionTranslators\Internal\SqlServerStringToUpperTranslator.cs" />
<Compile Include="Query\ExpressionTranslators\Internal\SqlServerDateTimeDatePartComponentTranslator.cs" />
<Compile Include="Query\ExpressionTranslators\Internal\SqlServerObjectToStringTranslator.cs" />
<Compile Include="Query\Internal\SqlServerCompiledQueryCacheKeyGenerator.cs" />
<Compile Include="Query\Internal\SqlServerQueryCompilationContext.cs" />
<Compile Include="Query\Internal\SqlServerQueryCompilationContextFactory.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SqlServerCompositeMethodCallTranslator : RelationalCompositeMethodC
new SqlServerMathRoundTranslator(),
new SqlServerMathTruncateTranslator(),
new SqlServerNewGuidTranslator(),
new SqlServerObjectToStringTranslator(),
new SqlServerStartsWithOptimizedTranslator(),
new SqlServerStringIsNullOrWhiteSpaceTranslator(),
new SqlServerStringReplaceTranslator(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal
{
using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Expressions;

/// <summary>
///
/// </summary>
/// <seealso cref="Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.IMethodCallTranslator" />
public class SqlServerObjectToStringTranslator : IMethodCallTranslator
{
/// <summary>
/// Translates the given method call expression.
/// </summary>
/// <param name="methodCallExpression">The method call expression.</param>
/// <returns>
/// A SQL expression representing the translated MethodCallExpression.
/// </returns>
public virtual Expression Translate(MethodCallExpression methodCallExpression)
{
if (methodCallExpression.Method.Name == nameof(ToString))
{
return new SqlFunctionExpression(
functionName: "CONVERT",
returnType: methodCallExpression.Type,
arguments: new[]
{
new SqlFragmentExpression("VARCHAR(MAX)"),
methodCallExpression.Object
});
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6322,6 +6322,29 @@ FROM [Customers] AS [c0]
Sql);
}

public override void Query_expression_with_to_string_and_contains()
{
base.Query_expression_with_to_string_and_contains();

Assert.Equal(
@"SELECT [o].[CustomerID]
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL AND (CHARINDEX(N'10', CONVERT(VARCHAR(MAX), [o].[EmployeeID])) > 0)",
Sql);
}


public override void Select_expression_to_string()
{
base.Select_expression_to_string();

Assert.Equal(
@"SELECT CONVERT(VARCHAR(MAX), [o].[EmployeeID])
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL",
Sql);
}

public override void Select_expression_date_add_year()
{
base.Select_expression_date_add_year();
Expand Down

0 comments on commit ea3f5e3

Please sign in to comment.