Skip to content

Commit

Permalink
make traversal query to start with Traversal extention
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0o0f committed Oct 17, 2016
1 parent 808649e commit 7739407
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 285 deletions.
11 changes: 6 additions & 5 deletions src/ArangoDB.Client/ArangoDB.Client.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
<Compile Include="Data\EnsureIndexData.cs" />
<Compile Include="Data\EnsureIndexResult.cs" />
<Compile Include="Data\TraversalData.cs" />
<Compile Include="Query\Clause\TraversalEdgeExpressionNode.cs" />
<Compile Include="Data\TraversalPathData.cs" />
<Compile Include="Data\TraversalResult.cs" />
<Compile Include="Data\TraverseEdge.cs" />
<Compile Include="Data\TraversalEdgeDefinition.cs" />
<Compile Include="Graph\ArangoGraph.cs" />
<Compile Include="Graph\ArangoGraphEdge.cs" />
<Compile Include="Graph\ArangoGraphVertex.cs" />
Expand Down Expand Up @@ -152,10 +153,9 @@
<Compile Include="Query\Clause\FilterClause.cs" />
<Compile Include="Query\Clause\FilterExpressionNode.cs" />
<Compile Include="Query\Clause\TraversalClause.cs" />
<Compile Include="Query\Clause\GraphDepthExpressionNode.cs" />
<Compile Include="Query\Clause\GraphDirectionExpressionNode.cs" />
<Compile Include="Query\Clause\GraphExpressionNode.cs" />
<Compile Include="Query\Clause\GraphStartVertexExpressionNode.cs" />
<Compile Include="Query\Clause\TraversalDepthExpressionNode.cs" />
<Compile Include="Query\Clause\TraversalDirectionExpressionNode.cs" />
<Compile Include="Query\Clause\TraversalGraphNameExpressionNode.cs" />
<Compile Include="Query\Clause\GroupByClause.cs" />
<Compile Include="Query\Clause\GroupByExpressionNode.cs" />
<Compile Include="Query\Clause\IgnoreModificationSelectExpressionNode.cs" />
Expand All @@ -174,6 +174,7 @@
<Compile Include="Query\Clause\SkipExpressionNode.cs" />
<Compile Include="Query\Clause\SkipTakeClause.cs" />
<Compile Include="Query\Clause\TakeExpressionNode.cs" />
<Compile Include="Query\Clause\TraversalExpressionNode.cs" />
<Compile Include="Query\Clause\UpdateReplaceClause.cs" />
<Compile Include="Query\Clause\UpdateReplaceExpressionNode.cs" />
<Compile Include="Query\Clause\UpsertClause.cs" />
Expand Down
15 changes: 15 additions & 0 deletions src/ArangoDB.Client/Data/TraversalEdgeDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArangoDB.Client.Data
{
public class TraversalEdgeDefinition
{
public string CollectionName { get; set; }

public EdgeDirection? Direction { get; set; }
}
}
119 changes: 0 additions & 119 deletions src/ArangoDB.Client/Data/TraverseEdge.cs

This file was deleted.

104 changes: 30 additions & 74 deletions src/ArangoDB.Client/Extentions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,58 +392,52 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
source.Expression,
Expression.Quote(predicate)));
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Graph<TVertex, TEdge>(this IQueryable source, string graphName)
{
return Graph<TVertex, TEdge>(source, graphName, typeof(TVertex), typeof(TEdge));
}

[ExtentionIdentifier("Graph")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> Graph<TVertex, TEdge>(this IQueryable source, string graphName, Type vertexType, Type edgeType)

[ExtentionIdentifier("Traversal_Selector")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Traversal<TVertex, TEdge>(this IQueryable source, Expression<Func<string>> startVertex)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("Graph", typeof(TVertex), typeof(TEdge)),
FindExtention("Traversal_Selector", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(graphName),
Expression.Constant(vertexType),
Expression.Constant(edgeType)
)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
Expression.Quote(startVertex))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Edges<TVertex, TEdge>(this IQueryable source, params string[] collectionNames)
[ExtentionIdentifier("Traversal_Constant")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Traversal<TVertex, TEdge>(this IQueryable source, string startVertex)
{
return InternalEdges<TVertex, TEdge>(source, collectionNames, typeof(TVertex), typeof(TEdge));
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("Traversal_Constant", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(startVertex))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

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

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Edge<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string collectionName)
{
return InternalEdges<TVertex, TEdge>(source, traverseEdge, typeof(TVertex), typeof(TEdge));
return Edge(source, collectionName, null);
}

[ExtentionIdentifier("TraverseEdges")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> InternalEdges<TVertex, TEdge>(this IQueryable source, TraversalEdgeDefinition traverseEdge, Type vertexType, Type edgeType)
[ExtentionIdentifier("Edge")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Edge<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string collectionName, EdgeDirection? direction)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("TraverseEdges", typeof(TVertex), typeof(TEdge)),
FindExtention("Edge", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(traverseEdge),
Expression.Constant(vertexType),
Expression.Constant(edgeType)
Expression.Constant(collectionName),
direction.HasValue ? Expression.Constant(direction.Value) : null
)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

Expand All @@ -458,69 +452,31 @@ public static ITraversalQueryable<TraversalData<TVertex, TEdge>> Depth<TVertex,
Expression.Constant(max))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> InBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return InBound(source, Utils.EdgeDirectionToString(EdgeDirection.Inbound));
}

[ExtentionIdentifier("InBound")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> InBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string direction)
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> InBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("InBound", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(direction))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> OutBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return OutBound(source, Utils.EdgeDirectionToString(EdgeDirection.Outbound));
source.Expression)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

[ExtentionIdentifier("OutBound")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> OutBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string direction)
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> OutBound<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("OutBound", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(direction))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

public static ITraversalQueryable<TraversalData<TVertex, TEdge>> AnyDirection<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return AnyDirection(source, Utils.EdgeDirectionToString(EdgeDirection.Any));
source.Expression)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

[ExtentionIdentifier("AnyDirection")]
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> AnyDirection<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string direction)
internal static ITraversalQueryable<TraversalData<TVertex, TEdge>> AnyDirection<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("AnyDirection", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(direction))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

[ExtentionIdentifier("StartVertex_Selector")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> StartVertex<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, Expression<Func<string>> selector)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("StartVertex_Selector", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Quote(selector))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}

[ExtentionIdentifier("StartVertex_Constant")]
public static ITraversalQueryable<TraversalData<TVertex, TEdge>> StartVertex<TVertex, TEdge>(this ITraversalQueryable<TraversalData<TVertex, TEdge>> source, string vertex)
{
return source.Provider.CreateQuery<TraversalData<TVertex, TEdge>>(
Expression.Call(
FindExtention("StartVertex_Constant", typeof(TVertex), typeof(TEdge)),
source.Expression,
Expression.Constant(vertex))) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
source.Expression)) as ITraversalQueryable<TraversalData<TVertex, TEdge>>;
}
}
}
7 changes: 4 additions & 3 deletions src/ArangoDB.Client/Query/AQLParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ private IQueryParser CreateQueryParser()
customNodeTypeRegistry.Register(InModificationExpressionNode.SupportedMethods, typeof(InModificationExpressionNode));
customNodeTypeRegistry.Register(IgnoreModificationSelectExpressionNode.SupportedMethods, typeof(IgnoreModificationSelectExpressionNode));
customNodeTypeRegistry.Register(TraversalExpressionNode.SupportedMethods, typeof(TraversalExpressionNode));
customNodeTypeRegistry.Register(GraphDepthExpressionNode.SupportedMethods, typeof(GraphDepthExpressionNode));
customNodeTypeRegistry.Register(GraphDirectionExpressionNode.SupportedMethods, typeof(GraphDirectionExpressionNode));
customNodeTypeRegistry.Register(GraphStartVertexExpressionNode.SupportedMethods, typeof(GraphStartVertexExpressionNode));
customNodeTypeRegistry.Register(TraversalDepthExpressionNode.SupportedMethods, typeof(TraversalDepthExpressionNode));
customNodeTypeRegistry.Register(TraversalDirectionExpressionNode.SupportedMethods, typeof(TraversalDirectionExpressionNode));
customNodeTypeRegistry.Register(TraversalGraphNameExpressionNode.SupportedMethods, typeof(TraversalGraphNameExpressionNode));
customNodeTypeRegistry.Register(TraversalEdgeExpressionNode.SupportedMethods, typeof(TraversalEdgeExpressionNode));

var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
nodeTypeProvider.InnerProviders.Insert(0, customNodeTypeRegistry);
Expand Down
25 changes: 8 additions & 17 deletions src/ArangoDB.Client/Query/ArangoModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,35 +291,26 @@ public void VisitTraversalClause(ITraversalClause traversalClause, QueryModel qu
: Utils.EdgeDirectionToString(EdgeDirection.Any));

GetAqlExpression(traversalClause.StartVertex, queryModel);

if (traversalClause.TraversalContext.Type == typeof(string))
{
QueryText.AppendFormat(" graph \"{0}\" ", traversalClause.TraversalContext.Value.ToString());
}
else if (traversalClause.TraversalContext.Type == typeof(string[]))

if (string.IsNullOrEmpty(traversalClause.GraphName) == false)
{
string[] collections = traversalClause.TraversalContext.Value as string[];
QueryText.Append(string.Join(", ", collections.Select(c => LinqUtility.ResolvePropertyName(c))));
QueryText.AppendFormat(" graph \"{0}\" ", traversalClause.GraphName);
}
else if (traversalClause.TraversalContext.Type == typeof(TraversalEdgeDefinition))
else
{
var edgeDefinition = traversalClause.TraversalContext.Value as TraversalEdgeDefinition;
StringBuilder edges = new StringBuilder();

for (int i = 0; i < edgeDefinition.Edges.Count; i++)
for (int i = 0; i < traversalClause.EdgeCollections.Count; i++)
{
var e = edgeDefinition.Edges[i];
var e = traversalClause.EdgeCollections[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));

edges.Append(LinqUtility.ResolvePropertyName(e.CollectionName));
}

QueryText.Append(edges);
Expand Down
Loading

0 comments on commit 7739407

Please sign in to comment.