示例#1
0
        /// <summary>
        /// Creates a single large query for the creation of a node and it's direct relationships.
        /// </summary>
        /// <param name="model">The for which to create the creation query</param>
        /// <returns>The query string for the creation of the node and its direct relationships</returns>
        private string BuildNodeCreationQuery(BasicNodeModel model)
        {
            StringBuilder builder = new StringBuilder();

            // Append the node creation statement
            builder.AppendFormat(kNodeCreationStatement, model.GetNodeLabels());
            builder.AppendLine();
            // Append the relationship statements
            BuildRelationshipMergeStatement(builder, model.Relationships, model.ContentType);
            // Append the return statement
            builder.Append("RETURN n");

            return(builder.ToString());
        }
示例#2
0
        /// <summary>
        /// Adds the given node to the graph.
        /// PRECONDITION: The node must be valid
        /// </summary>
        /// <param name="node">The BasicNodeModel of the node to add to the graph</param>
        /// <returns>Returns true if the node was added to the graph successfully</returns>
        public bool AddNode(BasicNodeModel node)
        {
            // The total number of nodes that should be created
            int expectedNodeCount         = node.Relationships.Where(x => x.IsNewAddition).Count() + 1;
            int expectedRelationshipCount = node.Relationships.Count();
            int actualNodeCount           = 0;
            int actualRelationshipCount   = 0;
            // Get the statements that need to be run
            List <Statement> creationStatements = GenerateStatements(node);

            // Create the session
            using (ISession session = driver.Session())
            {
                // Add the source node
                session.WriteTransaction(action =>
                {
                    IStatementResult nodeResult = action.Run(new Statement($"CREATE (s:{node.GetNodeLabels()} $props)",
                                                                           new Dictionary <string, object> {
                        { "props", node.GetPropertyMap() }
                    }));
                    actualNodeCount += nodeResult.Summary.Counters.NodesCreated;
                    // Only continue if the node was added
                    if (actualNodeCount == 1)
                    {
                        // For each of the relationship statements,
                        foreach (Statement relStatement in creationStatements)
                        {
                            // Run the statement
                            IStatementResult relResult = action.Run(relStatement);
                            // Update the counts of the actual node and relationship values
                            actualNodeCount         += relResult.Summary.Counters.NodesCreated;
                            actualRelationshipCount += relResult.Summary.Counters.RelationshipsCreated;
                        }
                    }

                    // If the node and relationship counts do not equal the expected values,
                    if (actualNodeCount != expectedNodeCount || actualRelationshipCount != expectedRelationshipCount)
                    {
                        // This transaction has failed
                        action.Failure();
                    }
                    else
                    {
                        // This transaction has succeeded
                        action.Success();
                    }
                });
            }

            return(actualNodeCount == expectedNodeCount && actualRelationshipCount == expectedRelationshipCount);
        }