Skip to content

Commit

Permalink
Minor code cleanup in SqlTranslatingEV
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jul 30, 2019
1 parent 60f8d99 commit f7fb615
Showing 1 changed file with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ protected override Expression VisitMember(MemberExpression memberExpression)
return result;
}

return TranslationFailed(memberExpression.Expression, innerExpression)
return TranslationFailed(memberExpression.Expression, innerExpression, out var sqlInnerExpression)
? null
: Dependencies.MemberTranslatorProvider.Translate((SqlExpression)innerExpression, memberExpression.Member, memberExpression.Type);
: Dependencies.MemberTranslatorProvider.Translate(sqlInnerExpression, memberExpression.Member, memberExpression.Type);
}

private bool TryBindMember(Expression source, MemberIdentity member, out Expression expression)
Expand Down Expand Up @@ -329,24 +329,23 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}

// MethodCall translators
var @object = Visit(methodCallExpression.Object);
if (TranslationFailed(methodCallExpression.Object, @object))
if (TranslationFailed(methodCallExpression.Object, Visit(methodCallExpression.Object), out var sqlObject))
{
return null;
}

var arguments = new SqlExpression[methodCallExpression.Arguments.Count];
for (var i = 0; i < arguments.Length; i++)
{
var argument = Visit(methodCallExpression.Arguments[i]);
if (TranslationFailed(methodCallExpression.Arguments[i], argument))
var argument = methodCallExpression.Arguments[i];
if (TranslationFailed(argument, Visit(argument), out var sqlArgument))
{
return null;
}
arguments[i] = (SqlExpression)argument;
arguments[i] = sqlArgument;
}

return Dependencies.MethodCallTranslatorProvider.Translate(_model, (SqlExpression)@object, methodCallExpression.Method, arguments);
return Dependencies.MethodCallTranslatorProvider.Translate(_model, sqlObject, methodCallExpression.Method, arguments);
}

private static Expression TryRemoveImplicitConvert(Expression expression)
Expand Down Expand Up @@ -427,19 +426,16 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
var left = TryRemoveImplicitConvert(binaryExpression.Left);
var right = TryRemoveImplicitConvert(binaryExpression.Right);

left = Visit(left);
right = Visit(right);

if (TranslationFailed(binaryExpression.Left, left)
|| TranslationFailed(binaryExpression.Right, right))
if (TranslationFailed(binaryExpression.Left, Visit(left), out var sqlLeft)
|| TranslationFailed(binaryExpression.Right, Visit(right), out var sqlRight))
{
return null;
}

return _sqlExpressionFactory.MakeBinary(
binaryExpression.NodeType,
(SqlExpression)left,
(SqlExpression)right,
sqlLeft,
sqlRight,
null);
}

Expand Down Expand Up @@ -493,19 +489,14 @@ protected override Expression VisitConditional(ConditionalExpression conditional
var ifTrue = Visit(conditionalExpression.IfTrue);
var ifFalse = Visit(conditionalExpression.IfFalse);

if (TranslationFailed(conditionalExpression.Test, test)
|| TranslationFailed(conditionalExpression.IfTrue, ifTrue)
|| TranslationFailed(conditionalExpression.IfFalse, ifFalse))
if (TranslationFailed(conditionalExpression.Test, test, out var sqlTest)
|| TranslationFailed(conditionalExpression.IfTrue, ifTrue, out var sqlIfTrue)
|| TranslationFailed(conditionalExpression.IfFalse, ifFalse, out var sqlIfFalse))
{
return null;
}

return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause((SqlExpression)test,(SqlExpression) ifTrue)
},
(SqlExpression)ifFalse);
return _sqlExpressionFactory.Case(new[] { new CaseWhenClause(sqlTest, sqlIfTrue) }, sqlIfFalse);
}

protected override Expression VisitUnary(UnaryExpression unaryExpression)
Expand All @@ -517,12 +508,11 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
return unaryExpression.Update(operand);
}

if (TranslationFailed(unaryExpression.Operand, operand))
if (TranslationFailed(unaryExpression.Operand, operand, out var sqlOperand))
{
return null;
}

var sqlOperand = (SqlExpression)operand;
switch (unaryExpression.NodeType)
{
case ExpressionType.Not:
Expand Down Expand Up @@ -557,7 +547,16 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
}

[DebuggerStepThrough]
private bool TranslationFailed(Expression original, Expression translation)
=> original != null && !(translation is SqlExpression);
private bool TranslationFailed(Expression original, Expression translation, out SqlExpression castTranslation)
{
if (original != null && !(translation is SqlExpression))
{
castTranslation = null;
return true;
}

castTranslation = translation as SqlExpression;
return false;
}
}
}

0 comments on commit f7fb615

Please sign in to comment.