Skip to content

Commit

Permalink
Merge pull request JasperFx#149 from nieve/where-modulo
Browse files Browse the repository at this point in the history
added support for modulo where queries
  • Loading branch information
jeremydmiller committed Feb 12, 2016
2 parents 9f9fb6e + 7dfe709 commit 6d2f924
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Marten.Testing/Linq/query_with_modulo_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Linq;
using Marten.Services;
using Marten.Testing.Fixtures;
using Xunit;

namespace Marten.Testing.Linq
{
public class query_with_modulo_Tests : DocumentSessionFixture<NulloIdentityMap>
{
[Fact]
public void use_modulo()
{
theSession.Store(new Target{Color = Colors.Blue, Number = 1});
theSession.Store(new Target{Color = Colors.Blue, Number = 2});
theSession.Store(new Target{Color = Colors.Blue, Number = 3});
theSession.Store(new Target{Color = Colors.Blue, Number = 4});
theSession.Store(new Target{Color = Colors.Blue, Number = 5});
theSession.Store(new Target{Color = Colors.Green, Number = 6});

theSession.SaveChanges();

theSession.Query<Target>().Where(x => x.Number % 2 == 0 && x.Color == Colors.Blue).ToArray()
.Select(x => x.Number)
.ShouldHaveTheSameElementsAs(2, 4);
}
}
}
1 change: 1 addition & 0 deletions src/Marten.Testing/Marten.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="Linq\query_against_primitive_array_Tests.cs" />
<Compile Include="Linq\query_running_through_the_IdentityMap_Tests.cs" />
<Compile Include="Linq\query_with_any_within_child_collection_Tests.cs" />
<Compile Include="Linq\query_with_modulo_Tests.cs" />
<Compile Include="Linq\query_with_enums_Tests.cs" />
<Compile Include="Linq\query_with_nullable_types_Tests.cs" />
<Compile Include="Linq\using_containment_operator_in_linq_Tests.cs" />
Expand Down
12 changes: 12 additions & 0 deletions src/Marten/Linq/MartenExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,23 @@ private IWhereFragment buildSimpleWhereClause(IDocumentMapping mapping, BinaryEx

return new WhereFragment(sql);
}
if (binary.Left.NodeType == ExpressionType.Modulo)
{
var moduloByValue = GetModuloByValue(binary);
return new WhereFragment("{0} % {1} {2} ?".ToFormat(jsonLocator, moduloByValue, op), value);
}


return new WhereFragment("{0} {1} ?".ToFormat(jsonLocator, op), value);
}

private static object GetModuloByValue(BinaryExpression binary)
{
var moduloExpression = binary.Left as BinaryExpression;
var moduloValueExpression = moduloExpression?.Right as ConstantExpression;
return moduloValueExpression != null ? Value(moduloValueExpression) : 1;
}

public static object Value(Expression expression)
{
if (expression is PartialEvaluationExceptionExpression)
Expand Down

0 comments on commit 6d2f924

Please sign in to comment.