Beispiel #1
0
        public override IEnumerable <CollectionItem> Load(OGM parent, Core.EntityCollectionBase target)
        {
            Entity targetEntity = target.ForeignEntity;

            string[] nodeNames = target.Parent.GetEntity().GetDbNames("node");
            string[] outNames  = targetEntity.GetDbNames("out");

            if (nodeNames.Length > 1 && outNames.Length > 1)
            {
                throw new InvalidOperationException("Both ends are virtual entities, this is too expensive to query...");
            }

            List <string> fullMatch = new List <string>();

            for (int nodeIndex = 0; nodeIndex < nodeNames.Length; nodeIndex++)
            {
                for (int outIndex = 0; outIndex < outNames.Length; outIndex++)
                {
                    string pattern = string.Empty;
                    if (target.Direction == DirectionEnum.In)
                    {
                        pattern = "MATCH ({0})-[rel:{2}]->({3}) WHERE node.{1} = {{key}} RETURN out, rel";
                    }
                    else if (target.Direction == DirectionEnum.Out)
                    {
                        pattern = "MATCH ({0})<-[rel:{2}]-({3}) WHERE node.{1} = {{key}} RETURN out, rel";
                    }

                    string match = string.Format(pattern,
                                                 nodeNames[nodeIndex],
                                                 target.ParentEntity.Key.Name,
                                                 target.Relationship.Neo4JRelationshipType,
                                                 outNames[outIndex]);

                    fullMatch.Add(match);
                }
            }

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

            parameters2.Add("key", parent.GetKey());

            List <CollectionItem> items = new List <CollectionItem>();
            var result = Transaction.RunningTransaction.Run(string.Join(" UNION ", fullMatch), parameters2);

            foreach (var record in result)
            {
                RawNode node = record.Values["out"].As <RawNode>();
                if (node is null)
                {
                    continue;
                }

                OGM             item = ReadNode(parent, targetEntity, node);
                RawRelationship rel  = record.Values["rel"].As <RawRelationship>();

                DateTime?startDate = null;
                DateTime?endDate   = null;

                if (target.Relationship.IsTimeDependent)
                {
                    object?value;
                    if (rel.Properties.TryGetValue(target.Relationship.StartDate, out value))
                    {
                        startDate = Conversion <long, DateTime> .Convert((long)value);
                    }
                    if (rel.Properties.TryGetValue(target.Relationship.EndDate, out value))
                    {
                        endDate = Conversion <long, DateTime> .Convert((long)value);
                    }
                }

                items.Add(target.NewCollectionItem(parent, item, startDate, endDate));
            }

            return(items);
        }
 public override bool Equals(RawRelationship other) => other is Neo4jRawRelationship rawOther?Relationship.Equals(rawOther.Relationship) : false;