Skip to content

Instantly share code, notes, and snippets.

@cskardon
Created March 8, 2017 15:00
Show Gist options
  • Save cskardon/1bceace0e4dd729764a76d7c16720a0a to your computer and use it in GitHub Desktop.
Save cskardon/1bceace0e4dd729764a76d7c16720a0a to your computer and use it in GitHub Desktop.
LinqPad Transactions with Neo4jClient - using REST and BOLT
/*
LinqPad available from: https://www.linqpad.net/
Settings:
- Language = 'C# Program'
- Nuget
- Neo4jClient (min: 3.0.0-Bolt-Driver00026) [Use Development MyGet: https://www.myget.org/F/cskardon/api/v3/index.json]
- Neo4j.Driver (min: 1.1.2)
- System.Net.Http (min: 4.3.1)
*/
void Main()
{
var boltGc = new BoltGraphClient("bolt://localhost");
boltGc.Connect();
var restGc = new GraphClient(new Uri("http://localhost:7474/db/data"));
restGc.Connect();
ClearDb(boltGc);
ExecuteAll(restGc, "GRAPHCLIENT");
ExecuteAll(boltGc, "BOLTGRAPHCLIENT");
}
private static void ExecuteAll(IGraphClient gc, string type)
{
Console.WriteLine($"{type} - Clear DB");
ClearDb(gc);
Console.WriteLine($"{type} - Execute - NO commit");
Execute(gc, false);
Console.WriteLine($"{type} - Get Results");
GetResults(gc);
Console.WriteLine($"{type} - Execute - Commit");
Execute(gc, true);
Console.WriteLine($"{type} - Get Results");
GetResults(gc);
}
private static void ClearDb(IGraphClient gc)
{
gc.Cypher.Match("(n)").DetachDelete("n").ExecuteWithoutResults();
}
private static void GetResults(IGraphClient gc)
{
var query = gc.Cypher.Match("(u:User)").Return(u => u.As<User>());
var results = query.Results;
results.Dump();
}
private static void Execute(IGraphClient gc, bool commit)
{
var user = new User { Email = "a@a.com", Id = 1 };
var user2 = new User { Email = "b@b.com", Id = 2 };
using (var scope = new TransactionScope())
{
gc.Cypher.Create("(u:User {userParam})").WithParam("userParam", user).ExecuteWithoutResults();
var users = gc.Cypher.Match("(u:User)").Where((User u) => u.Id == 1).Return(u => u.As<User>()).Results;
foreach (var u in users)
{
var idParam = u.Id;
gc.Cypher.Match("(u:User {Id: {idParam}})").WithParams(new { idParam }).Set("u.Id = 7687").ExecuteWithoutResults();
}
gc.Cypher.Create("(u:User {userParam})").WithParam("userParam", user2).ExecuteWithoutResults();
if(commit)
scope.Complete();
}
}
public class User
{
public string Email { get; set; }
public int Id { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment