Skip to content

Commit

Permalink
Merge branch 'rel/1.1.1' into merge111
Browse files Browse the repository at this point in the history
  • Loading branch information
maumar committed Nov 22, 2016
2 parents 398dec4 + a7e0117 commit 1a47fe6
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query.Expressions;
using Remotion.Linq.Parsing;

namespace Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,9 @@ public static bool IsSearchCondition(Expression expression)

protected override Expression VisitBinary(BinaryExpression expression)
{
Expression newLeft;
Expression newRight;

if (_isSearchCondition)
{
if (expression.IsComparisonOperation()
Expand All @@ -1662,19 +1665,16 @@ protected override Expression VisitBinary(BinaryExpression expression)

_isSearchCondition = false;

var left = Visit(expression.Left);
var right = Visit(expression.Right);
newLeft = AdjustExpressionType(Visit(expression.Left), expression.Left.Type);
newRight = AdjustExpressionType(Visit(expression.Right), expression.Right.Type);

_isSearchCondition = parentIsSearchCondition;

return Expression.MakeBinary(expression.NodeType, left, right);
return Expression.MakeBinary(expression.NodeType, newLeft, newRight);
}
}
else
{
Expression newLeft;
Expression newRight;

if (expression.IsLogicalOperation()
|| expression.NodeType == ExpressionType.Or
|| expression.NodeType == ExpressionType.And)
Expand All @@ -1693,6 +1693,9 @@ protected override Expression VisitBinary(BinaryExpression expression)
newRight = Visit(expression.Right);
}

newLeft = AdjustExpressionType(newLeft, expression.Left.Type);
newRight = AdjustExpressionType(newRight, expression.Right.Type);

var newExpression
= expression.Update(newLeft, expression.Conversion, newRight);

Expand All @@ -1707,9 +1710,17 @@ var newExpression
}
}

return base.VisitBinary(expression);
newLeft = AdjustExpressionType(Visit(expression.Left), expression.Left.Type);
newRight = AdjustExpressionType(Visit(expression.Right), expression.Right.Type);

return expression.Update(newLeft, expression.Conversion, newRight);
}

private static Expression AdjustExpressionType(Expression expression, Type expectedType)
=> expression.Type != expectedType
? Expression.Convert(expression, expectedType)
: expression;

protected override Expression VisitConditional(ConditionalExpression expression)
{
var parentIsSearchCondition = _isSearchCondition;
Expand Down Expand Up @@ -1767,7 +1778,7 @@ protected override Expression VisitUnary(UnaryExpression expression)
{
return Expression.Equal(
expression.Operand,
Expression.Constant(false, typeof(bool)));
Expression.Constant(false, expression.Operand.Type));
}

if (expression.NodeType == ExpressionType.Convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public virtual void Include_multiple_one_to_one_and_one_to_many()
}
}

[ConditionalFact]
public virtual void ToString_guid_property_projection()
{
using (var context = CreateContext())
{
var query = context.Tags.Select(ct => new { A = ct.GearNickName, B = ct.Id.ToString() });
var result = query.ToList();

Assert.Equal(6, result.Count);
}
}

[ConditionalFact]
public virtual void Include_multiple_one_to_one_and_one_to_many_self_reference()
{
Expand Down Expand Up @@ -2099,6 +2111,21 @@ private static IEnumerable<TElement> ClientDefaultIfEmpty<TElement>(IEnumerable<
return source?.Count() == 0 ? new[] { default(TElement) } : source;
}

[ConditionalFact]
public virtual void Complex_predicate_with_AndAlso_and_nullable_bool_property()
{
using (var context = CreateContext())
{
var query = from w in context.Weapons
where w.Id != 50 && !w.Owner.HasSoulPatch
select w;

var result = query.ToList();

Assert.Equal(5, result.Count);
}
}

protected GearsOfWarContext CreateContext() => Fixture.CreateContext(TestStore);

protected GearsOfWarQueryTestBase(TFixture fixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,39 @@ private static readonly MethodInfo _readValue
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual Expression CreateReadValueExpression(
Expression valueBuffer,
Type type,
Expression valueBuffer,
Type type,
int index,
IProperty property = null)
=> Expression.Call(
_tryReadValueMethod.MakeGenericMethod(type),
valueBuffer,
Expression.Constant(index),
Expression.Constant(property, typeof(IPropertyBase)));

private static readonly MethodInfo _tryReadValueMethod
= typeof(EntityMaterializerSource).GetTypeInfo()
.GetDeclaredMethod(nameof(TryReadValue));

private static TValue TryReadValue<TValue>(
ValueBuffer valueBuffer,
int index,
IProperty property = null)
IPropertyBase property = null)
{
var readValueCallExpression
= CreateReadValueCallExpression(valueBuffer, index);
object untypedValue = null;

var convertedReadValueExpression
= Expression.Convert(readValueCallExpression, type);

var exceptionParameter
= Expression.Parameter(typeof(Exception), "e");
try
{
untypedValue = valueBuffer[index];

var catchBlock
= Expression
.Catch(exceptionParameter,
Expression.Call(
ThrowReadValueExceptionMethod.MakeGenericMethod(convertedReadValueExpression.Type),
exceptionParameter,
readValueCallExpression,
Expression.Constant(property, typeof(IPropertyBase))));
return (TValue)untypedValue;
}
catch (Exception e)
{
ThrowReadValueException<TValue>(e, untypedValue, property);
}

return Expression.TryCatch(convertedReadValueExpression, catchBlock);
return default(TValue);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,18 @@ FROM [CogTag] AS [t]
Sql);
}

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

Assert.Equal(
@"SELECT [w].[Id], [w].[AmmunitionType], [w].[IsAutomatic], [w].[Name], [w].[OwnerFullName], [w].[SynergyWithId], [w.Owner].[Nickname], [w.Owner].[SquadId], [w.Owner].[AssignedCityName], [w.Owner].[CityOrBirthName], [w.Owner].[Discriminator], [w.Owner].[FullName], [w.Owner].[HasSoulPatch], [w.Owner].[LeaderNickname], [w.Owner].[LeaderSquadId], [w.Owner].[Rank]
FROM [Weapon] AS [w]
LEFT JOIN [Gear] AS [w.Owner] ON [w].[OwnerFullName] = [w.Owner].[FullName]
WHERE ([w].[Id] <> 50) AND ([w.Owner].[HasSoulPatch] = 0)",
Sql);
}

protected override void ClearLog() => TestSqlLoggerFactory.Reset();

private const string FileLineEnding = @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,26 @@ ORDER BY [o].[CustomerID]
materializer: (ValueBuffer valueBuffer) =>
{
var3 = new Order()
var3.<OrderID>k__BackingField = try { (int) object valueBuffer.get_Item(0) } catch (Exception) { ... }
var3.<CustomerID>k__BackingField = try { (string) object valueBuffer.get_Item(1) } catch (Exception) { ... }
var3.<EmployeeID>k__BackingField = try { (Nullable<int>) object valueBuffer.get_Item(2) } catch (Exception) { ... }
var3.<OrderDate>k__BackingField = try { (Nullable<DateTime>) object valueBuffer.get_Item(3) } catch (Exception) { ... }
var3.<OrderID>k__BackingField = int TryReadValue(
valueBuffer: valueBuffer,
index: 0,
property: OrderID
)
var3.<CustomerID>k__BackingField = string TryReadValue(
valueBuffer: valueBuffer,
index: 1,
property: CustomerID
)
var3.<EmployeeID>k__BackingField = Nullable<int> TryReadValue(
valueBuffer: valueBuffer,
index: 2,
property: EmployeeID
)
var3.<OrderDate>k__BackingField = Nullable<DateTime> TryReadValue(
valueBuffer: valueBuffer,
index: 3,
property: OrderDate
)
return instance
}
)
Expand Down

0 comments on commit 1a47fe6

Please sign in to comment.