Created
March 8, 2017 15:00
-
-
Save cskardon/1bceace0e4dd729764a76d7c16720a0a to your computer and use it in GitHub Desktop.
LinqPad Transactions with Neo4jClient - using REST and BOLT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 = "[email protected]", Id = 1 }; | |
var user2 = new User { Email = "[email protected]", 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