public void EnsureEntityIsCreatedEvenFlushedWithoutTransaction()
        {
            using (Transaction.Begin())
            {
                Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");
                RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                RawRecord record = result.FirstOrDefault();

                RawNode loaded = record["n"].As <RawNode>();
                Assert.AreEqual(loaded.Properties["name"], "Address");
                Assert.AreEqual(loaded.Properties["title"], "Developer");

                Transaction.Flush();
            }

            using (Transaction.Begin())
            {
                RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                RawRecord record = result.FirstOrDefault();

                RawNode loaded = record["n"].As <RawNode>();
                Assert.AreEqual(loaded.Properties["name"], "Address");
                Assert.AreEqual(loaded.Properties["title"], "Developer");
            }
        }
Example #2
0
        protected override void ApplyFunctionalId(FunctionalId functionalId)
        {
            if (functionalId == null)
            {
                return;
            }

            if (functionalId.wasApplied || functionalId.highestSeenId == -1)
            {
                return;
            }

            lock (functionalId)
            {
                string    getFidQuery = $"CALL blueprint41.functionalid.current('{functionalId.Label}')";
                RawResult result      = Run(getFidQuery);
                long?     currentFid  = result.FirstOrDefault()?.Values["Sequence"].As <long?>();
                if (currentFid.HasValue)
                {
                    functionalId.SeenUid(currentFid.Value);
                }

                string setFidQuery = $"CALL blueprint41.functionalid.setSequenceNumber('{functionalId.Label}', {functionalId.highestSeenId}, {(functionalId.Format == IdFormat.Numeric).ToString().ToLowerInvariant()})";
                Run(setFidQuery);
                functionalId.wasApplied    = true;
                functionalId.highestSeenId = -1;
            }
        }
        public void EnsureCanCreateAnEntity()
        {
            using (Transaction.Begin(true))
            {
                // Let us try to create an entity
                Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");

                RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                RawRecord record = result.FirstOrDefault();
                RawNode   loaded = record["n"].As <RawNode>();

                Assert.AreEqual(loaded.Properties["name"], "Address");
                Assert.AreEqual(loaded.Properties["title"], "Developer");

                Transaction.Commit();
            }
        }
        public void EnsureEntityShouldNotBeRollbackedAfterCommitedAndViceVersa()
        {
            InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() =>
            {
                using (Transaction.Begin(true))
                {
                    // Let us try to create an entity
                    Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");

                    RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                    RawRecord record = result.FirstOrDefault();
                    RawNode loaded   = record["n"].As <RawNode>();

                    Assert.AreEqual(loaded.Properties["name"], "Address");
                    Assert.AreEqual(loaded.Properties["title"], "Developer");

                    Transaction.Commit();
                    Transaction.Rollback();
                }
            });

            Assert.That(exception.Message, Contains.Substring("The transaction was already committed or rolled back."));

            InvalidOperationException exception2 = Assert.Throws <InvalidOperationException>(() =>
            {
                using (Transaction.Begin(true))
                {
                    // Let us try to create an entity
                    Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");

                    RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                    RawRecord record = result.FirstOrDefault();
                    RawNode loaded   = record["n"].As <RawNode>();

                    Assert.AreEqual(loaded.Properties["name"], "Address");
                    Assert.AreEqual(loaded.Properties["title"], "Developer");

                    Transaction.Rollback();
                    Transaction.Commit();
                }
            });

            Assert.That(exception2.Message, Contains.Substring("The transaction was already committed or rolled back."));
        }
        public void EnsureEntityIsRolledbackWhenExceptionIsThrown()
        {
            Assert.Throws <Exception>(() =>
            {
                using (Transaction.Begin(true))
                {
                    // Let us try to create an entity
                    Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");
                    throw new Exception();
                }
            });

            using (Transaction.Begin())
            {
                RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                RawRecord record = result.FirstOrDefault();
                Assert.IsNull(record);
            }
        }
        public void EnsureEntityIsCreatedRegardlessAnExceptionIsThrown()
        {
            Assert.Throws <Exception>(() =>
            {
                using (Transaction.Begin())
                {
                    // Let us try to create an entity
                    Transaction.RunningTransaction.Run("CREATE (n:SampleEntity { name: 'Address', title: 'Developer' })");
                    throw new Exception();
                }
            });

            using (Transaction.Begin())
            {
                RawResult result = Transaction.RunningTransaction.Run("Match (n:SampleEntity) Return n");
                RawRecord record = result.FirstOrDefault();
                RawNode   loaded = record["n"].As <RawNode>();

                Assert.AreEqual(loaded.Properties["name"], "Address");
                Assert.AreEqual(loaded.Properties["title"], "Developer");
            }
        }
        public override void AddUnmanaged(Relationship relationship, OGM inItem, OGM outItem, DateTime?startDate, DateTime?endDate, bool fullyUnmanaged = false)
        {
            Transaction trans = Transaction.RunningTransaction;

            Checks(relationship, inItem, outItem);

            if (!fullyUnmanaged)
            {
                string find = string.Format(
                    "MATCH (in:{0})-[r:{1}]->(out:{2}) WHERE in.{3} = {{inKey}} and out.{4} = {{outKey}} and (r.{5} <= {{endDate}} OR r.{5} IS NULL) AND (r.{6} > {{startDate}} OR r.{6} IS NULL) RETURN min(COALESCE(r.{5}, {{MinDateTime}})) as MinStartDate, max(COALESCE(r.{6}, {{MaxDateTime}})) as MaxEndDate, count(r) as Count",
                    inItem.GetEntity().Label.Name,
                    relationship.Neo4JRelationshipType,
                    outItem.GetEntity().Label.Name,
                    inItem.GetEntity().Key.Name,
                    outItem.GetEntity().Key.Name,
                    relationship.StartDate,
                    relationship.EndDate);

                Dictionary <string, object?> parameters = new Dictionary <string, object?>();
                parameters.Add("inKey", inItem.GetKey());
                parameters.Add("outKey", outItem.GetKey());
                parameters.Add("startDate", Conversion <DateTime, long> .Convert(startDate ?? DateTime.MinValue));
                parameters.Add("endDate", Conversion <DateTime, long> .Convert(endDate ?? DateTime.MaxValue));
                parameters.Add("MinDateTime", Conversion <DateTime, long> .Convert(DateTime.MinValue));
                parameters.Add("MaxDateTime", Conversion <DateTime, long> .Convert(DateTime.MaxValue));

                RawResult result = trans.Run(find, parameters);
                RawRecord record = result.FirstOrDefault();
                int       count  = record["Count"].As <int>();
                if (count > 0)
                {
                    DateTime?minStartDate = Conversion <long?, DateTime?> .Convert(record["MinStartDate"].As <long?>());

                    DateTime?maxEndDate = Conversion <long?, DateTime?> .Convert(record["MaxEndDate"].As <long?>());

                    if (startDate > minStartDate)
                    {
                        startDate = minStartDate ?? DateTime.MinValue;
                    }

                    if (endDate < maxEndDate)
                    {
                        endDate = maxEndDate ?? DateTime.MaxValue;
                    }
                }

                string delete = string.Format(
                    "MATCH (in:{0})-[r:{1}]->(out:{2}) WHERE in.{3} = {{inKey}} and out.{4} = {{outKey}} and (r.{5} <= {{endDate}}) AND (r.{6} > {{startDate}}) DELETE r",
                    inItem.GetEntity().Label.Name,
                    relationship.Neo4JRelationshipType,
                    outItem.GetEntity().Label.Name,
                    inItem.GetEntity().Key.Name,
                    outItem.GetEntity().Key.Name,
                    relationship.StartDate,
                    relationship.EndDate);

                trans.Run(delete, parameters);
            }

            string match = string.Format("MATCH (in:{0}) WHERE in.{1} = {{inKey}} MATCH (out:{2}) WHERE out.{3} = {{outKey}}",
                                         inItem.GetEntity().Label.Name,
                                         inItem.GetEntity().Key.Name,
                                         outItem.GetEntity().Label.Name,
                                         outItem.GetEntity().Key.Name);
            string create = string.Format("CREATE (in)-[outr:{0} {{node}}]->(out)", relationship.Neo4JRelationshipType);

            Dictionary <string, object> node = new Dictionary <string, object>();

            node.Add(relationship.CreationDate, Conversion <DateTime, long> .Convert(trans.TransactionDate));
            if (relationship.IsTimeDependent)
            {
                node.Add(relationship.StartDate, Conversion <DateTime, long> .Convert(startDate ?? DateTime.MinValue));
                node.Add(relationship.EndDate, Conversion <DateTime, long> .Convert(endDate ?? DateTime.MaxValue));
            }

            Dictionary <string, object?> parameters2 = new Dictionary <string, object?>();

            parameters2.Add("inKey", inItem.GetKey());
            parameters2.Add("outKey", outItem.GetKey());
            parameters2.Add("node", node);

            string query = match + "\r\n" + create;

            relationship.RaiseOnRelationCreate(trans);

            trans.Run(query, parameters2);

            relationship.RaiseOnRelationCreated(trans);
        }