Skip to content

Commit

Permalink
Addressing code review comments #2 (2018-05-15).
Browse files Browse the repository at this point in the history
  • Loading branch information
austindrenski committed May 15, 2018
1 parent ed0b754 commit fc9ff22
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#endregion

using System;
using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
Expand All @@ -41,70 +40,46 @@ public class NpgsqlRangeTranslator : IMethodCallTranslator
{
/// <inheritdoc />
[CanBeNull]
public Expression Translate(MethodCallExpression methodCallExpression) =>
TryTranslateOperator(methodCallExpression);

/// <summary>
/// Attempts to translate the <see cref="MethodCallExpression"/> as a PostgreSQL range operator.
/// </summary>
/// <param name="expression">The <see cref="MethodCallExpression"/> to be translated.</param>
/// <returns>
/// The expression if successful; otherwise, null.
/// </returns>
[CanBeNull]
static Expression TryTranslateOperator([NotNull] MethodCallExpression expression)
public Expression Translate(MethodCallExpression expression)
{
switch (expression.Method.Name)
{
case nameof(NpgsqlRangeExtensions.Contains):
return MakeBinaryExpression(expression, "@>", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "@>", typeof(bool));

case nameof(NpgsqlRangeExtensions.ContainedBy):
return MakeBinaryExpression(expression, "<@", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "<@", typeof(bool));

case nameof(NpgsqlRangeExtensions.Overlaps):
return MakeBinaryExpression(expression, "&&", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "&&", typeof(bool));

case nameof(NpgsqlRangeExtensions.IsStrictlyLeftOf):
return MakeBinaryExpression(expression, "<<", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "<<", typeof(bool));

case nameof(NpgsqlRangeExtensions.IsStrictlyRightOf):
return MakeBinaryExpression(expression, ">>", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], ">>", typeof(bool));

case nameof(NpgsqlRangeExtensions.DoesNotExtendRightOf):
return MakeBinaryExpression(expression, "&<", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "&<", typeof(bool));

case nameof(NpgsqlRangeExtensions.DoesNotExtendLeftOf):
return MakeBinaryExpression(expression, "&>", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "&>", typeof(bool));

case nameof(NpgsqlRangeExtensions.IsAdjacentTo):
return MakeBinaryExpression(expression, "-|-", typeof(bool));
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "-|-", typeof(bool));

case nameof(NpgsqlRangeExtensions.Union):
return MakeBinaryExpression(expression, "+", expression.Arguments[0].Type);
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "+", expression.Arguments[0].Type);

case nameof(NpgsqlRangeExtensions.Intersect):
return MakeBinaryExpression(expression, "*", expression.Arguments[0].Type);
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "*", expression.Arguments[0].Type);

case nameof(NpgsqlRangeExtensions.Except):
return MakeBinaryExpression(expression, "-", expression.Arguments[0].Type);
return new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], "-", expression.Arguments[0].Type);

default:
return null;
}
}

/// <summary>
/// Constructs a <see cref="CustomBinaryExpression"/>.
/// </summary>
/// <param name="expression">The <see cref="MethodCallExpression"/> containing two parameters.</param>
/// <param name="symbol">The symbolic operator for PostgreSQL.</param>
/// <param name="returnType">The return type of the operator.</param>
/// <returns>
/// A <see cref="CustomBinaryExpression"/>.
/// </returns>
[NotNull]
static Expression MakeBinaryExpression([NotNull] MethodCallExpression expression, [NotNull] string symbol, [NotNull] Type returnType) =>
new CustomBinaryExpression(expression.Arguments[0], expression.Arguments[1], symbol, returnType);
}
}
158 changes: 0 additions & 158 deletions test/EFCore.PG.FunctionalTests/Query/RangeQueryNpgsqlFixture.cs

This file was deleted.

Loading

0 comments on commit fc9ff22

Please sign in to comment.