Skip to content

Commit

Permalink
added TraversalEdgeDefinition extention
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0o0f committed Oct 16, 2016
1 parent 4581432 commit 808649e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/ArangoDB.Client/ArangoDB.Client.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="Data\TraversalData.cs" />
<Compile Include="Data\TraversalPathData.cs" />
<Compile Include="Data\TraversalResult.cs" />
<Compile Include="Data\TraverseEdge.cs" />
<Compile Include="Graph\ArangoGraph.cs" />
<Compile Include="Graph\ArangoGraphEdge.cs" />
<Compile Include="Graph\ArangoGraphVertex.cs" />
Expand Down
119 changes: 119 additions & 0 deletions src/ArangoDB.Client/Data/TraverseEdge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArangoDB.Client
{

public class TraversalEdgeDefinition
{
internal List<TraverseEdge.IEdge> Edges { get; set; }

public TraversalEdgeDefinition()
{
Edges = new List<TraverseEdge.IEdge>();
}

public TraversalEdgeDefinition Collection<T>(EdgeDirection direction)
{
Edges.Add(new TraverseEdge.Edge<T> { Direction = direction });

return this;
}

public TraversalEdgeDefinition Collection<T>()
{
Edges.Add(new TraverseEdge.Edge<T>());

return this;
}

public TraversalEdgeDefinition Collection(string collection, EdgeDirection direction)
{
Edges.Add(new TraverseEdge.Edge { Collection = collection, Direction = direction });

return this;
}

public TraversalEdgeDefinition Collection(string collection)
{
Edges.Add(new TraverseEdge.Edge { Collection = collection });

return this;
}
}

public class TraverseEdge
{
private TraverseEdge()
{
}

internal interface IEdge
{
object GetCollection();

EdgeDirection? Direction { get; set; }
}

internal class Edge : IEdge
{
public string Collection { get; set; }

public EdgeDirection? Direction { get; set; }

public object GetCollection()
{
return Collection;
}
}

internal class Edge<T> : IEdge
{
public EdgeDirection? Direction { get; set; }

public object GetCollection()
{
return typeof(T);
}
}

public static TraversalEdgeDefinition Collection<T>(EdgeDirection direction)
{
var edgeDefinition = new TraversalEdgeDefinition();

edgeDefinition.Edges.Add(new Edge<T> { Direction = direction });

return edgeDefinition;
}

public static TraversalEdgeDefinition Collection<T>()
{
var edgeDefinition = new TraversalEdgeDefinition();

edgeDefinition.Edges.Add(new Edge<T>());

return edgeDefinition;
}

public static TraversalEdgeDefinition Collection(string collection, EdgeDirection direction)
{
var edgeDefinition = new TraversalEdgeDefinition();

edgeDefinition.Edges.Add(new Edge { Collection = collection, Direction = direction });

return edgeDefinition;
}

public static TraversalEdgeDefinition Collection(string collection)
{
var edgeDefinition = new TraversalEdgeDefinition();

edgeDefinition.Edges.Add(new Edge { Collection = collection });

return edgeDefinition;
}
}
}
18 changes: 18 additions & 0 deletions src/ArangoDB.Client/Extentions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,24 @@ internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> InternalEdges
Expression.Constant(edgeType)
)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Edges<TVertex, TEdge>(this IQueryable source, TraversalEdgeDefinition traverseEdge)
{
return InternalEdges<TVertex, TEdge>(source, traverseEdge, typeof(TVertex), typeof(TEdge));
}

[ExtentionIdentifier("TraverseEdges")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> InternalEdges<TVertex, TEdge>(this IQueryable source, TraversalEdgeDefinition traverseEdge, Type vertexType, Type edgeType)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("TraverseEdges", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(traverseEdge),
Expression.Constant(vertexType),
Expression.Constant(edgeType)
)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

[ExtentionIdentifier("Depth")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Depth<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, int min, int max)
Expand Down
28 changes: 25 additions & 3 deletions src/ArangoDB.Client/Query/ArangoModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static ArangoModelVisitor()
{typeof(AverageResultOperator),"average"}
};
}

public override void VisitQueryModel(QueryModel queryModel)
{
if (DefaultAssociatedIdentifier != null)
Expand Down Expand Up @@ -292,7 +292,7 @@ public void VisitTraversalClause(ITraversalClause traversalClause, QueryModel qu

GetAqlExpression(traversalClause.StartVertex, queryModel);

if(traversalClause.TraversalContext.Type == typeof(string))
if (traversalClause.TraversalContext.Type == typeof(string))
{
QueryText.AppendFormat(" graph \"{0}\" ", traversalClause.TraversalContext.Value.ToString());
}
Expand All @@ -301,8 +301,30 @@ public void VisitTraversalClause(ITraversalClause traversalClause, QueryModel qu
string[] collections = traversalClause.TraversalContext.Value as string[];
QueryText.Append(string.Join(", ", collections.Select(c => LinqUtility.ResolvePropertyName(c))));
}
else if (traversalClause.TraversalContext.Type == typeof(TraversalEdgeDefinition))
{
var edgeDefinition = traversalClause.TraversalContext.Value as TraversalEdgeDefinition;
StringBuilder edges = new StringBuilder();

for (int i = 0; i < edgeDefinition.Edges.Count; i++)
{
var e = edgeDefinition.Edges[i];

if (i != 0)
edges.Append(", ");

if (e.Direction.HasValue)
edges.AppendFormat("{0} ", Utils.EdgeDirectionToString(e.Direction.Value));

if (e.GetCollection().GetType() == typeof(string))
edges.Append(LinqUtility.ResolvePropertyName(e.GetCollection() as string));
else
edges.Append(LinqUtility.ResolveCollectionName(Db, e.GetCollection() as Type));
}

QueryText.Append(edges);
}


}

public override void VisitWhereClause(WhereClause whereClause, QueryModel queryModel, int index)
Expand Down
14 changes: 8 additions & 6 deletions src/ArangoDB.Client/Query/Clause/GraphExpressionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public class TraversalExpressionNode : MethodCallExpressionNodeBase, IQuerySourc
{
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.Graph<object,object>(null, null, null, null)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.Graph<object,object>(null,null)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.InternalEdges<object,object>(null, null, null, null)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.Edges<object,object>(null,null))
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.InternalEdges<object,object>(null, new string[] { }, null, null)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.Edges<object,object>(null,string.Empty)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.InternalEdges<object,object>(null, TraverseEdge.Collection(""), null, null)),
LinqUtility.GetSupportedMethod(()=>QueryableExtensions.Edges<object,object>(null, TraverseEdge.Collection("")))
};

public ConstantExpression TraversalContext { get; private set; }
Expand All @@ -29,14 +31,14 @@ public class TraversalExpressionNode : MethodCallExpressionNodeBase, IQuerySourc

string identifier;

public TraversalExpressionNode(MethodCallExpressionParseInfo parseInfo,
public TraversalExpressionNode(MethodCallExpressionParseInfo parseInfo,
ConstantExpression traversalContext,
ConstantExpression vertexType,
ConstantExpression edgeType)
: base(parseInfo)
{
TraversalContext = traversalContext;
if(vertexType != null && edgeType != null)
if (vertexType != null && edgeType != null)
{
VertextType = vertexType;
EdgeType = edgeType;
Expand Down Expand Up @@ -84,14 +86,14 @@ public Expression GetResolvedAdaptedSelector(ClauseGenerationContext clauseGener
r =>
{
var traversalDataType = typeof(TraversalData<,>).MakeGenericType(new Type[] { VertextType.Value as Type, EdgeType.Value as Type });

var constr = ReflectionUtils.GetConstructors(traversalDataType).ToList()[0];
var newExpression =
Expression.Convert(
Expression.New(constr), traversalDataType)
;

return r.GetResolvedExpression(newExpression, Expression.Parameter(traversalDataType,"dummy"), clauseGenerationContext);
return r.GetResolvedExpression(newExpression, Expression.Parameter(traversalDataType, "dummy"), clauseGenerationContext);
});
}
}
Expand Down

0 comments on commit 808649e

Please sign in to comment.