Skip to content

Commit

Permalink
add support filter segment
Browse files Browse the repository at this point in the history
  • Loading branch information
voronov-maxim committed Feb 3, 2019
1 parent 3d06370 commit 0834124
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public override bool Translate(EntitySetSegment segment)
{
return GetNextSegment() is EntitySetSegment entitySetSegment && entitySetSegment.EntitySet == segment.EntitySet;
}
public override bool Translate(FilterSegment segment)
{
return GetNextSegment() is FilterSegment filterSegment && filterSegment.EdmType == segment.EdmType;
}
public override bool Translate(KeySegment segment)
{
if (GetNextSegment() is KeySegment keySegment && keySegment.EdmType == segment.EdmType && keySegment.NavigationSource == segment.NavigationSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ private static String GetSegmentResourcePath(ODataPath path, String skipOrTop)
if (stringBuilder.Length > 0)
stringBuilder.Append('/');

if (pathSegment is EntitySetSegment)
stringBuilder.Append((pathSegment as EntitySetSegment).EntitySet.Name);
else if (pathSegment is NavigationPropertySegment)
stringBuilder.Append((pathSegment as NavigationPropertySegment).NavigationProperty.Name);
if (pathSegment is EntitySetSegment entitySetSegment)
stringBuilder.Append(entitySetSegment.EntitySet.Name);
else if (pathSegment is NavigationPropertySegment navigationPropertySegment)
stringBuilder.Append(navigationPropertySegment.NavigationProperty.Name);
else if (pathSegment is KeySegment)
{
stringBuilder.Append(pathSegment.Identifier);
stringBuilder.Append("()");
}
else if (pathSegment is CountSegment)
stringBuilder.Append(pathSegment.Identifier);
else if (pathSegment is FilterSegment)
stringBuilder.Append(pathSegment.Identifier);
else
throw new InvalidOperationException("unknown ODataPathSegment " + pathSegment.GetType().ToString());
}
Expand Down
2 changes: 0 additions & 2 deletions source/OdataToEntity/Parsers/OeGetParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

Expand Down
41 changes: 25 additions & 16 deletions source/OdataToEntity/Parsers/OeParseNavigationSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public static IEdmEntitySet GetEntitySet(IReadOnlyList<OeParseNavigationSegment>

return null;
}
private static NavigationPropertySegment GetNavigationSegment(ODataPathSegment previousSegment, out IEdmEntitySet entitySet)
{
if (previousSegment is EntitySetSegment entitySetSegment)
{
entitySet = entitySetSegment.EntitySet;
return null;
}

if (previousSegment is NavigationPropertySegment navigationPropertySegment)
{
entitySet = (IEdmEntitySet)navigationPropertySegment.NavigationSource;
return navigationPropertySegment;
}

throw new InvalidOperationException("Invalid segment");
}
public static IReadOnlyList<OeParseNavigationSegment> GetNavigationSegments(ODataPath path)
{
var navigationSegments = new List<OeParseNavigationSegment>();
Expand All @@ -46,24 +62,17 @@ public static IReadOnlyList<OeParseNavigationSegment> GetNavigationSegments(ODat
navigationSegments.Add(new OeParseNavigationSegment(navigationSegment, null));
else if (segment is KeySegment keySegment)
{
IEdmEntitySet previousEntitySet;
navigationSegment = null;
if (previousSegment is EntitySetSegment)
{
var previousEntitySetSegment = previousSegment as EntitySetSegment;
previousEntitySet = previousEntitySetSegment.EntitySet;
}
else if (previousSegment is NavigationPropertySegment)
{
navigationSegment = previousSegment as NavigationPropertySegment;
previousEntitySet = (IEdmEntitySet)navigationSegment.NavigationSource;
}
else
throw new InvalidOperationException("invalid segment");

FilterClause keyFilter = CreateFilterClause(previousEntitySet, keySegment.Keys);
navigationSegment = GetNavigationSegment(previousSegment, out IEdmEntitySet entitySet);
FilterClause keyFilter = CreateFilterClause(entitySet, keySegment.Keys);
navigationSegments.Add(new OeParseNavigationSegment(navigationSegment, keyFilter));
}
else if (segment is FilterSegment filterSegment)
{
navigationSegment = GetNavigationSegment(previousSegment, out _);
FilterClause filterClause = new FilterClause(filterSegment.Expression, filterSegment.RangeVariable);
navigationSegments.Add(new OeParseNavigationSegment(navigationSegment, filterClause));
}

previousSegment = segment;
}

Expand Down
3 changes: 3 additions & 0 deletions source/OdataToEntity/Parsers/Translators/OeSelectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ private static IEdmEntitySet GetEntitySet(ODataPath path)
if (path.LastSegment is OperationSegment)
return ((EntitySetSegment)path.FirstSegment).EntitySet;

if (path.LastSegment is FilterSegment)
return ((EntitySetSegment)path.FirstSegment).EntitySet;

throw new InvalidOperationException("unknown segment type " + path.LastSegment.ToString());
}
public IReadOnlyList<IEdmNavigationProperty> GetJoinPath()
Expand Down
13 changes: 13 additions & 0 deletions test/OdataToEntity.Test/Common/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,19 @@ public async Task FilterNavigation(int pageSize)
[Theory]
[InlineData(0)]
[InlineData(1)]
public async Task FilterSegment(int pageSize)
{
var parameters = new QueryParameters<Customer>()
{
RequestUri = "Customers/$filter(Sex eq OdataToEntity.Test.Model.Sex'Female')?$orderby=Id",
Expression = t => t.Where(c => c.Sex == Sex.Female).OrderBy(c => c.Id),
PageSize = pageSize
};
await Fixture.Execute(parameters).ConfigureAwait(false);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
public async Task FilterStringConcat(int pageSize)
{
var parameters = new QueryParameters<Customer>()
Expand Down
6 changes: 4 additions & 2 deletions test/OdataToEntity.Test/Common/SelectTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public async Task BoundFunctionCollection()
{
List<int> expectedResult;
using (var dbContext = Fixture.CreateContext())
expectedResult = dbContext.OrderItems.Where(i => i.Order.Name == "Order 1" || i.Order.Name == "Order 2").Select(i => i.Id).ToList();
expectedResult = dbContext.OrderItems.Where(i =>
(i.Order.Name == "Order 1" || i.Order.Name == "Order 2") && i.Order.Customer.Sex == Model.Sex.Female)
.Select(i => i.Id).ToList();

Db.OeDataAdapter dataAdapter = Fixture.EdmModel.GetDataAdapter(Fixture.EdmModel.EntityContainer);
String request = $"Customers/BoundFunctionCollection(orderNames=['Order 1','Order 2'])?$expand=Customer,Items&$select=Name";
String request = $"Customers/$filter(Sex eq 'Female')/BoundFunctionCollection(orderNames=['Order 1','Order 2'])?$expand=Customer,Items&$select=Name";

ODataUri odataUri = Fixture.ParseUri(request);
IEdmModel edmModel = Fixture.EdmModel.GetEdmModel(odataUri.Path);
Expand Down

0 comments on commit 0834124

Please sign in to comment.