4

Is it possible to execute CYPHER queries as just plain old strings using the Neo4j .NET Client or any other module?

For instance, if I want to add some nodes to my graph database and already had the statements assembled, is there a means to execute a string:

CREATE (n:Edit {name:"L-1154LX"});

I'm looking to batch process a list of CREATE CYPHER queries that have already be created.

smk081
  • 783
  • 1
  • 10
  • 36

2 Answers2

5

Officially documented at https://github.com/Readify/Neo4jClient/wiki/cypher#manual-queries-highly-discouraged

However, this will be bad for performance, and risky for security.

It's bad for performance, because it will have to re-parse every query. You should use parameters, like in the example at https://github.com/Readify/Neo4jClient/wiki/cypher-examples#create-a-user This way, the query text remains consistent, and just the parameter varies, so you don't incur the query compilation cost on every call.

It's risky for security, because you can easily get the encoding wrong, and expose yourself to injection risks.

So, please don't run manual queries unless you really understand what you're doing. They're hidden on purpose.

Tatham Oddie
  • 4,290
  • 19
  • 31
  • Thank you! I am having trouble implementing this in code. I am assuming the "query" is the raw string Cypher query but is this just a method of the standard Client object? I am using the client for .NET. – smk081 Nov 16 '14 at 00:04
  • I really really caution you against doing this. There are better, safer, faster ways to do what you want. The documentation I linked to gives three lines of code that you could literally copy and paste. Note that the methods are explicit interface implementations, and thus require that you cast to IRawGraphClient first. But, I still discourage you from doing this. Really. Really really. – Tatham Oddie Nov 16 '14 at 03:21
  • Follow this code instead: https://github.com/Readify/Neo4jClient/wiki/cypher-examples#create-a-user – Tatham Oddie Nov 16 '14 at 03:22
1

I am writing a .NET application that enables the extraction of nodes and relationships from Excel spreadsheets so that they can be dynamically generated and loaded into neo4j (see translation / management object model below).

When I load them into neo4j via the neo4jclient I will not know what my nodes look like at run time; they can have any number of attributes and these could have any name and value. In the examples at https://github.com/Readify/Neo4jClient/wiki/cypher-examples it looks like I should have local classes to refer to for attribute names and values; but this will not work. Am I missing a trick here? parameterised queries? (even these examples expected a local, hard coded class).

 public class Node
        {
            public string NodeType = "";
            public List<Attribute> Attributes;
            public List<Relationship> ParentRelationships;
            public List<Relationship> ChildRelationships;
            public Node(string nodeType)
            {
                NodeType = nodeType;
                Attributes = new List<Attribute>();
                ParentRelationships = new List<Relationship>();
                ChildRelationships = new List<Relationship>();
            }

            public void AddAttribute(Attribute attribute)
            {
                //We are not allowing empty attributes
                if(attribute.GetValue() != "")
                    this.Attributes.Add(attribute);
            }

            public string GetIdentifier()
            {
                foreach (Attribute a in Attributes)
                {
                    if (a.IsIdentifier)
                        return a.GetValue();
                }
                return null;
            }

            public void AddParentRelationship(Relationship pr)
            {
                ParentRelationships.Add(pr);
            }

            public void AddChildRelationship(Relationship cr)
            {
                ChildRelationships.Add(cr);
            }

  public class Attribute
        {
            private string Name;
            private string Value;
            public bool IsIdentifier;

            public Attribute(string name, string value, bool isIdentifier)
            {
                SetName(name);
                SetValue(value);
                IsIdentifier = isIdentifier;
            }

            public void SetName(string name)
            {
                Name = name.Trim();
            }
            public void SetValue(string value)
            {
                Value = value.Trim();
            }
            public string GetName()
            {
                return Name;
            }
            public string GetValue()
            {
                return Value;
            }

        }