public static void Main() { const string stmt0 = "RETURN \"Hello World\""; const string stmt1 = "CREATE path=(acme:Company {name:\"Acme Corporation\"})" + " -[:owns]-> " + "(tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN path, acme, tesla, 7890, 1.2, ['a', 'b', 'c'] AS abc "; const string stmt3 = "MATCH path=(co:Company)-[r:owns]->(car:Car) RETURN path, co, r, car, " + "5678, 1.4, timestamp() AS unixepoctime, ['a', 'b', 'c'] AS abc LIMIT 5"; const string stmt4 = "MATCH (n:Car) RETURN n.make LIMIT 5"; const string stmt5 = "MATCH (n:Car) RETURN 1234 LIMIT 5"; const string stmt6 = "MATCH (n) RETURN DISTINCT labels(n)"; const string stmt7 = "MATCH (n) RETURN DISTINCT labels(n), count(*) AS NumofNodes, " + "avg(size(keys(n))) AS AvgNumOfPropPerNode, min(size(keys(n))) AS MinNumPropPerNode, max(size(keys(n))) AS MaxNumPropPerNode, " + "avg(size((n)-[]-())) AS AvgNumOfRelationships, min(size((n)-[]-())) AS MinNumOfRelationships, max(size((n)-[]-())) AS MaxNumOfRelationships "; const string stmt8 = "CALL db.labels() YIELD label RETURN label, timestamp() AS unixepoctime"; const string stmt9 = "RETURN ['a', 'b', 'c'] AS abc, ['d', 123, 456.5] AS def"; const string stmt10 = "RETURN 1 <> 0"; const string stmta = "CREATE (co:Company {name:\"Able Corporation\"}) RETURN co"; const string stmtb = "CREATE (co:Company {name:\"Baker Corporation\"}) RETURN co"; const string stmtc = "CREATE (co:Company {name:\"Charlie Corporation\"}) RETURN co"; const string stmtd = "CREATE (co:Company {name:\"Delta Corporation\"}) RETURN co"; const string stmtd0 = "MATCH (n) DETACH DELETE n RETURN Count(*)"; const string stmtd1 = "MATCH (n:Car) DETACH DELETE n RETURN Count(*)"; const string stmtd2 = "MATCH (n:Company) DETACH DELETE n RETURN Count(*)"; //_driver = GraphDatabase.Driver("bolt://52.207.74.86:33912", AuthTokens.Basic("neo4j", "gun-november-trips")); _driver = GraphDatabase.Driver("bolt://localhost:7687"); using (var session = _driver.Session()) { IStatementResult resultRecords0 = session.Run(stmtd0); IStatementResult resultRecords = session.Run(stmt1); List <string> entityKeys = (List <string>)resultRecords.Keys; // variable names & constants in RETURN clause var recordList = resultRecords.ToList <IRecord>(); foreach (IRecord record in recordList) // for each match in the Cypher statement { ProcessRecord(record, entityKeys); } using (var tx = session.BeginTransaction()) { resultRecords = tx.Run(stmta); resultRecords = tx.Run(stmtb); resultRecords = tx.Run(stmtc); resultRecords = tx.Run(stmtd); tx.Success(); } } _driver.Close(); Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); }
public DriverDecorator(IDriver driver, N4pperManager manager) : base(driver) { Manager = manager; using (ISession session = base.Session().WithGraphManager(Manager, this)) { IStatementResult res = session.Run("CALL dbms.procedures() YIELD name WITH name WHERE name STARTS WITH 'apoc.' RETURN count(*)"); IsApocAvailable = res != null && ((long)res.ToList().First().Values.First().Value) > 0; } }
public static void Main() { _driver = GraphDatabase.Driver("bolt://52.207.74.86:33912", AuthTokens.Basic("neo4j", "gun-november-trips")); using (var session = _driver.Session()) { string stmt0 = "RETURN \"Hello World\""; IStatementResult resultRecords = session.Run(stmt0); var recordList = resultRecords.ToList <IRecord>(); IRecord record = recordList[0]; IReadOnlyDictionary <string, object> entities = record.Values; KeyValuePair <string, object> entity = entities.First(); string key = entity.Key; string value = entity.Value.ToString(); Console.WriteLine("key: " + key + "\nvalue: " + value); } _driver.Close(); Console.ReadLine(); }
internal static object Execute <TResult>(IStatementRunner runner, Statement statement, Func <IRecord, Type, object> mapper, Expression expression) { bool IsEnumerable = typeof(TResult).IsEnumerable(); Type typeResult; QueryTranslator tranaslator = new QueryTranslator(); expression = Evaluator.PartialEval(expression); string statementText = Regex.Replace(statement.Text, "RETURN", "WITH", RegexOptions.IgnoreCase); PipeVariableRewriter rewriter = new PipeVariableRewriter(); statementText = rewriter.Tokenize(statementText).Rebuild(); (string firstVar, IEnumerable <string> otherVars) = GetFirstCypherVariableName(statementText); string queryText = tranaslator.Translate(expression, out MethodCallExpression terminal, out int?countFromBegin, out typeResult, firstVar, otherVars); queryText = Regex.Replace($"{statementText} {queryText}", "RETURN", "WITH", RegexOptions.IgnoreCase); queryText = rewriter.Tokenize(queryText).Rebuild(); queryText = Regex.Replace(queryText, "WITH(.+?)$", "RETURN$1", RegexOptions.IgnoreCase | RegexOptions.RightToLeft); IStatementResult result = runner.Run(queryText, statement.Parameters); IQueryable <IRecord> records = result.ToList().AsQueryable(); if (terminal != null) { IRecord r = records.FirstOrDefault(); if (terminal.Method.Name.StartsWith("First")) { if (r == null) { if (terminal.Method.Name.EndsWith("Default")) { return(typeResult.GetDefault()); } else { throw new ArgumentOutOfRangeException(nameof(records), "The collection is empty"); } } return(mapper(r, typeResult)); } else { return(Convert.ChangeType(r.Values[r.Keys[0]], terminal.Type));//can only be an aggregate numeric value } } else { System.Collections.IList lst = (System.Collections.IList) typeof(List <>).MakeGenericType(typeResult).GetInstanceOf(null); foreach (object item in records.Select(p => mapper(p, typeResult))) { lst.Add(item); } IQueryable lstQ = lst.AsQueryable(); if (countFromBegin == null) { return(lstQ); } else { ExpressionInitialModifier treeCopier = new ExpressionInitialModifier(lstQ, countFromBegin.Value); Expression newExpressionTree = treeCopier.Visit(expression); // This step creates an IQueryable that executes by replacing Queryable methods with Enumerable methods. if (IsEnumerable) { return(lstQ.Provider.CreateQuery(newExpressionTree)); } else { return(lstQ.Provider.Execute(newExpressionTree)); } } } }