Exemplo n.º 1
0
        internal static CreateQueryReturn ToCreate(NeoContext context, object entity)
        {
            var meta = context.GetMetaData(entity.GetType());

            var createQuery = ToCreateNode(context, entity, NodeKey.Create(meta.Key), new Dictionary <object, string>());

            createQuery.Query.Append("RETURN ");
            foreach (var key in createQuery.FlattedEntities.Keys)
            {
                createQuery.Query.Append($"{key},");
            }

            createQuery.Query.TryRemoveLastCharacter(',');
            return(createQuery);
        }
Exemplo n.º 2
0
        private static CreateQueryReturn ToCreateNode(NeoContext context, object entity, string queryKey, IDictionary <object, string> entities)
        {
            entities.Add(entity, queryKey);
            var meta = context.GetMetaData(entity.GetType());

            meta.GetCreatedAtProperty()?.SetValue(entity, DateTime.UtcNow);
            meta.GetUpdatedAtProperty()?.SetValue(entity, DateTime.UtcNow);

            var properties = BuildPropertyClause(meta, entity);

            var r = new CreateQueryReturn();

            r.FlattedEntities.Add(queryKey, entity);
            r.Query.AppendLine($"CREATE ({queryKey}:{meta.Label} {properties})");

            var rIndex = 0;

            foreach (var p in meta.RelationshipProperties)
            {
                var rMeta   = context.GetMetaData(p.EntityType);
                var rEntity = p.GetValue(entity);
                if (rEntity == null)
                {
                    continue;
                }

                var rCollection = p.IsCollection ? (ICollection)rEntity : new[] { rEntity };
                foreach (var item in rCollection)
                {
                    if (entities.TryGetValue(item, out var existKey))
                    {
                        r.Query.AppendLine(ToRelationshipCreate(p, queryKey, existKey));
                        continue;
                    }

                    var rNodeKey = NodeKey.Create(rMeta.Key, rIndex++, queryKey);
                    r.Append(ToCreateNode(context, item, rNodeKey, entities));
                    r.Query.AppendLine(ToRelationshipCreate(p, queryKey, rNodeKey));
                }
            }

            return(r);
        }