Skip to content

Commit

Permalink
refactor(Translators): extract type mapping for ToString convert base
Browse files Browse the repository at this point in the history
test(Translators): add separate test for each type mapping
  • Loading branch information
laskoviymishka authored and anpete committed Dec 12, 2016
1 parent c4b2fb1 commit b6efc1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6400,18 +6400,40 @@ public virtual void Query_expression_with_to_string_and_contains()
os => os.Where(o => o.OrderDate != null && o.EmployeeID.Value.ToString().Contains("10"))
.Select(o => new Order
{
CustomerID = o.CustomerID
CustomerID = o.CustomerID
}));
}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Expressions;

Expand All @@ -13,6 +13,13 @@ namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal
/// <seealso cref="Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.IMethodCallTranslator" />
public class SqlServerObjectToStringTranslator : IMethodCallTranslator
{
private static readonly Dictionary<string, string> _typeMapping = new Dictionary<string, string>
{
[nameof(Int64)] = "VARCHAR(20)",
[nameof(Int32)] = "VARCHAR(10)",
[nameof(Int16)] = "VARCHAR(5)"
};

/// <summary>
/// Translates the given method call expression.
/// </summary>
Expand All @@ -29,26 +36,14 @@ public virtual Expression Translate(MethodCallExpression methodCallExpression)
returnType: methodCallExpression.Type,
arguments: new[]
{
new SqlFragmentExpression(GetConvertBase(methodCallExpression.Object?.Type.Name)),
new SqlFragmentExpression(GetConvertBase(methodCallExpression.Object?.Type.UnwrapEnumType().Name)),
methodCallExpression.Object
});
}
return null;
}

private string GetConvertBase(string typeName)
{
switch (typeName)
{
case nameof(Int64):
return "VARCHAR(20)";
case nameof(Int32):
return "VARCHAR(10)";
case nameof(Int16):
return "VARCHAR(5)";
default:
return "VARCHAR(MAX)";
}
}
=> _typeMapping.ContainsKey(typeName) ? _typeMapping[typeName] : "VARCHAR(MAX)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6333,13 +6333,33 @@ WHERE [o].[OrderDate] IS NOT NULL AND (CHARINDEX(N'10', CONVERT(VARCHAR(10), [o]
Sql);
}

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

Assert.Equal(
@"SELECT CONVERT(VARCHAR(20), [o].[OrderID])
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL",
Sql);
}
public override void Select_expression_int_to_string()
{
base.Select_expression_int_to_string();

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

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

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

0 comments on commit b6efc1f

Please sign in to comment.