Skip to content

Commit

Permalink
fix returning all traversal query result
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0o0f committed Oct 18, 2016
1 parent 2349fad commit 7eb7b62
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Documents\DocumentCommand.cs" />
<Compile Include="Graphs\GraphCommand.cs" />
<Compile Include="Linq\ModificationQuery.cs" />
<Compile Include="Linq\TraversalQuery.cs" />
<Compile Include="Models\Follow.cs" />
<Compile Include="Models\Host.cs" />
<Compile Include="Models\Person.cs" />
Expand Down
114 changes: 114 additions & 0 deletions src/ArangoDB.Client.Examples/Linq/TraversalQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using ArangoDB.Client.Data;
using ArangoDB.Client.Examples.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ArangoDB.Client.Examples.Linq
{
public class TraversalQuery : TestDatabaseSetup
{
Person alice = new Person
{
Name = "Alice",
Age = 21
};

Person bob = new Person
{
Name = "Bob",
Age = 22
};

Person charlie = new Person
{
Name = "Charlie",
Age = 23
};

Person dave = new Person
{
Name = "Dave",
Age = 24
};

Person eve = new Person
{
Name = "Eve",
Age = 24
};

Follow aliceBob;
Follow bobCharlie;
Follow bobDave;
Follow charlieEve;

string graphName = "SocialGraph";

private void InitiateGraph()
{
db.InsertMultiple<Person>(new Person[]
{
alice, bob, charlie, dave, eve
});

aliceBob = new Follow
{
Follower = alice.Id,
Followee = bob.Id,
Label = nameof(aliceBob)
};
bobCharlie = new Follow
{
Follower = bob.Id,
Followee = charlie.Id,
Label = nameof(bobCharlie)
};
bobDave = new Follow
{
Follower = bob.Id,
Followee = dave.Id,
Label = nameof(bobDave)
};
charlieEve = new Follow
{
Follower = charlie.Id,
Followee = eve.Id,
Label = nameof(charlieEve)
};

db.InsertMultiple<Follow>(new Follow[]
{
aliceBob, bobCharlie, bobDave, charlieEve
});

db.Graph(graphName).Create(new EdgeDefinitionTypedData[]
{
new EdgeDefinitionTypedData
{
Collection = typeof(Follow),
From = new Type[] { typeof(Person) },
To = new Type[] { typeof(Person) }
}
});
}

[Fact]
public void Traversal()
{
InitiateGraph();

var result = db.Query()
.Traversal<Person, Follow>(alice.Id)
.Depth(1, 4)
.OutBound()
.Graph(graphName)
.Select(g => g)
.ToList();

}
}
}
2 changes: 2 additions & 0 deletions src/ArangoDB.Client.Examples/Models/Follow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class Follow
public string Followee { get; set; }

public DateTime CreatedDate { get; set; }

public string Label { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Database\DatabaseGenerator.cs" />
<Compile Include="Linq\CustomQuery.cs" />
<Compile Include="Linq\SimpleQuery.cs" />
<Compile Include="Linq\TraversalQuery.cs" />
<Compile Include="Mock\ArangoDatabaseMock.cs" />
<Compile Include="Model\Car.cs" />
<Compile Include="Model\Friend.cs" />
Expand Down
15 changes: 15 additions & 0 deletions src/ArangoDB.Client.Test/Linq/TraversalQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ArangoDB.Client.Test.Database;
using ArangoDB.Client.Test.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ArangoDB.Client.Test.Linq
{
public class TraversalQuery
{
}
}
32 changes: 31 additions & 1 deletion src/ArangoDB.Client/Query/ArangoExpressionTreeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,37 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)

protected override Expression VisitParameter(ParameterExpression expression)
{
ModelVisitor.QueryText.AppendFormat(" {0} ", LinqUtility.ResolvePropertyName(expression.Name));
string name = expression.Name;

if(expression.Type.Name == "TraversalData`2")
{
string prefix = LinqUtility.MemberNameFromMap(name, "graph", ModelVisitor);

ModelVisitor.QueryText.AppendFormat(" {{ {0} : {1}, {2} : {3}, {4} : {5} }} ",
LinqUtility.ResolvePropertyName($"Vertex"),
LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
LinqUtility.ResolvePropertyName($"Edge"),
LinqUtility.ResolvePropertyName($"{prefix}_Edge"),
LinqUtility.ResolvePropertyName($"Path"),
LinqUtility.ResolvePropertyName($"{prefix}_Path"));

return expression;
}

if(expression.Type.Name == "ShortestPathData`2")
{
string prefix = LinqUtility.MemberNameFromMap(name, "graph", ModelVisitor);

ModelVisitor.QueryText.AppendFormat(" {{ {0} : {1}, {2} : {3} }} ",
LinqUtility.ResolvePropertyName($"Vertex"),
LinqUtility.ResolvePropertyName($"{prefix}_Vertex"),
LinqUtility.ResolvePropertyName($"Edge"),
LinqUtility.ResolvePropertyName($"{prefix}_Edge"));

return expression;
}

ModelVisitor.QueryText.AppendFormat(" {0} ", LinqUtility.ResolvePropertyName(name));

return expression;
}
Expand Down

0 comments on commit 7eb7b62

Please sign in to comment.