Skip to content

Commit

Permalink
Merge pull request #116 from pelavall/feature/supporteverqueries
Browse files Browse the repository at this point in the history
Add a WasEver extension to support the Ever query verb

+semver: minor
  • Loading branch information
rjmurillo authored Mar 4, 2017
2 parents 30e9e37 + 888e3ab commit f1bc53c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Qwiq.Linq/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ private static bool AsOf<T>(this T _, DateTime __)
{
return true;
}

public static bool WasEver<T>(this T _, T __)
{
return true;
}
}
}

1 change: 1 addition & 0 deletions src/Qwiq.Linq/Qwiq.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
<Compile Include="Visitors\PartialEvaluator.cs" />
<Compile Include="Visitors\QueryRewriter.cs" />
<Compile Include="Visitors\WiqlQueryBuilder.cs" />
<Compile Include="WiqlExpressions\WasEverExpression.cs" />
<Compile Include="WiqlExpressions\AsOfExpression.cs" />
<Compile Include="WiqlExpressions\ContainsExpression.cs" />
<Compile Include="WiqlExpressions\IndexerExpression.cs" />
Expand Down
8 changes: 8 additions & 0 deletions src/Qwiq.Linq/Visitors/QueryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
return new UnderExpression(node.Type, subject, target);
}

if (node.Method.DeclaringType == typeof(QueryExtensions) && node.Method.Name == "WasEver")
{
var subject = Visit(node.Arguments[0]);
var target = Visit(node.Arguments[1]);

return new WasEverExpression(node.Type, subject, target);
}

// This is a contains used to see if a value is in a list, such as: bug => aliases.Contains(bug.AssignedTo)
if (node.Method.DeclaringType == typeof(Enumerable) && node.Method.Name == "Contains")
{
Expand Down
23 changes: 23 additions & 0 deletions src/Qwiq.Linq/WiqlExpressions/WasEverExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Linq.Expressions;

namespace Microsoft.Qwiq.Linq.WiqlExpressions
{
public class WasEverExpression : Expression
{
internal WasEverExpression(Type type, Expression subject, Expression target)
{
Type = type;
Subject = subject;
Target = target;
}

public override ExpressionType NodeType => (ExpressionType)WiqlExpressionType.WasEver;

public override Type Type { get; }

internal Expression Subject { get; private set; }
internal Expression Target { get; private set; }
}
}

3 changes: 2 additions & 1 deletion src/Qwiq.Linq/WiqlExpressions/WiqlExpressionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal enum WiqlExpressionType
AsOf,
Contains,
Select,
Indexer
Indexer,
WasEver
}
}

15 changes: 15 additions & 0 deletions src/Qwiq.Linq/WiqlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public override Expression Visit(Expression expression)
return VisitContains((ContainsExpression)expression);
case WiqlExpressionType.Indexer:
return VisitIndexer((IndexerExpression) expression);
case WiqlExpressionType.WasEver:
return VisitWasEver((WasEverExpression)expression);
default:
return base.Visit(expression);
}
Expand Down Expand Up @@ -150,6 +152,19 @@ protected virtual Expression VisitUnder(UnderExpression expression)
return expression;
}

protected virtual Expression VisitWasEver(WasEverExpression expression)
{
_expressionInProgress.Enqueue(new GroupStartFragment());
Visit(expression.Subject);

_expressionInProgress.Enqueue(new StringFragment(" EVER "));

Visit(expression.Target);
_expressionInProgress.Enqueue(new GroupEndFragment());

return expression;
}

protected virtual Expression VisitOrder(OrderExpression expression)
{
Visit(expression.Source);
Expand Down
18 changes: 18 additions & 0 deletions test/Qwiq.Linq.Tests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,24 @@ public void the_StartsWith_is_translated_to_an_under_operator()
}
}

[TestClass]
// ReSharper disable once InconsistentNaming
public class when_a_where_clause_uses_the_ever_function : QueryBuilderTests
{
public override void When()
{
base.When();
Expected = "SELECT * FROM WorkItems WHERE (([Assigned To] EVER 'alias'))";
Actual = Query.Where(item => item.AssignedTo.WasEver("alias")).ToString();
}

[TestMethod]
public void the_WasEver_is_translated_to_an_ever_operator()
{
Actual.ShouldEqual(Expected);
}
}

[TestClass]
// ReSharper disable once InconsistentNaming
public class when_a_where_clause_uses_the_Contains_string_function : QueryBuilderTests
Expand Down

0 comments on commit f1bc53c

Please sign in to comment.