Exemple #1
0
        private MetadataNode AddDynamicNode(List <MetadataNode> itemNodes, MetadataIdentity identity, IBindingMetadata metadata, ColumnIdentity value)
        {
            this.AddStaticNode(itemNodes, metadata, null);

            MetadataNode thisNode = this.FindNode(itemNodes, identity);

            MetadataIdentity parentIdentity = identity.Pop();

            if (thisNode != null)
            {
                thisNode.Column ??= value;
            }
            else if (parentIdentity != null)
            {
                MetadataNode parentNode = this.FindNode(itemNodes, parentIdentity) ?? this.AddDynamicNode(itemNodes, parentIdentity, metadata, null);

                if (parentNode != null)
                {
                    thisNode = new MetadataNode(identity)
                    {
                        Column   = value,
                        Metadata = metadata,
                        Flags    = NodeFlags.Dynamic,
                    };

                    parentNode.Properties.Add(thisNode);
                }
            }

            return(thisNode);
        }
Exemple #2
0
        private IJsonMetadata GetMetadata(IMetadataBuilderContext context, MetadataIdentity identity)
        {
            MetadataIdentity  parentIdentity = identity.Parent();
            IJsonMetadata     parent         = context.GetMetadata <IJsonMetadata>(parentIdentity.Name) ?? this.GetMetadata(context, parentIdentity);
            IRelationMetadata relation       = identity.GetMetadata <IRelationMetadata>();

            if (parent == null || relation == null)
            {
                return(null);
            }

            JsonMetadata metadata = new JsonMetadata(relation);

            if (this.HasJsonAttribute(relation) || relation.HasFlag(RelationMetadataFlags.Item))
            {
                metadata.MemberOf = metadata;
                metadata.IsRoot   = true;
                metadata.Path     = "$";
            }
            else
            {
                metadata.MemberOf = parent.MemberOf;
                metadata.IsRoot   = false;
                metadata.Path     = $"{parent.Path}.{this.GetPropertyNameFromContract(metadata)}";
            }

            context.AddMetadata <IJsonMetadata>(metadata);

            return(metadata);
        }
Exemple #3
0
        private static void AddDataNode(NodeTree tree, DataAttribute attribute)
        {
            MetadataIdentity identity = new MetadataIdentity(tree.Schema, attribute.Name);
            IBindingMetadata metadata = identity.Lookup <IBindingMetadata>() ?? FindDynamicMetadata(identity);

            if (metadata != null)
            {
                Node node;

                if (metadata.HasFlag(BindingMetadataFlags.Dynamic))
                {
                    node = AddDynamicNode(tree, identity, metadata);
                }
                else
                {
                    node = AddStaticNode(tree, metadata);
                }

                if (node != null)
                {
                    node.Data = attribute;

                    tree.Data.Add(node);
                }
            }
        }
Exemple #4
0
        public FuncDescriptor Build()
        {
            Delegate[]         binders    = this.relationNode.Fields.Select(this.CreateBinder).ToArray();
            MetadataIdentity[] attributes = this.relationNode.Attributes;
            IMetadataNotation  notation   = this.relationNode.Identity.Schema.Notation;

            Action <IField, IEnumerator[], IField[]>[] funcs = new Action <IField, IEnumerator[], IField[]> [this.relationNode.Items.Count];
            MetadataIdentity[] listIds = new MetadataIdentity[this.relationNode.Items.Count];

            for (int i = 0; i < funcs.Length; i++)
            {
                ReadFields itemReader = this.GetItemReader(this.relationNode.Items[i]);

                funcs[i]   = (source, enumerators, fields) => itemReader(source.Model, source, attributes, binders, enumerators, fields, notation);
                listIds[i] = this.relationNode.Items[i].List?.Metadata.Identity ?? this.relationNode.Items[i].Metadata.Identity;
            }

            return(new FuncDescriptor()
            {
                Factories = funcs,
                Degree = this.relationNode.Degree,
                VisibleDegree = this.relationNode.VisibleDegree,
                Identity = this.relationNode.Identity,
                Lists = listIds,
            });
        }
Exemple #5
0
        public RelationHeader <TTarget> Join <TTarget>(Expression <Func <TSource, IEnumerable <TTarget> > > expression)
        {
            MetadataIdentity  newIdentity = this.Source.Identity.Push(this.Schema.Notation.Lambda(expression));
            IRelationMetadata metadata    = newIdentity.Lookup <IRelationMetadata>();

            return(new RelationHeader <TTarget>(metadata.Item, this.Attributes));
        }
Exemple #6
0
        public RelationHeader <TSource> SelectAll <TTarget>(Expression <Func <TSource, TTarget> > expression, Func <IRelationMetadata, bool> selector)
        {
            MetadataIdentity sourceIdentity            = this.Source.Identity.Push(this.Schema.Notation.Lambda(expression));
            IReadOnlyList <IRelationMetadata> metadata = sourceIdentity.Lookup <IRelationMetadata>().Properties;

            return(new RelationHeader <TSource>(this.Source, this.Add(metadata.Where(selector))));
        }
Exemple #7
0
        public RelationHeader <TSource> Select <TTarget>(Expression <Func <TSource, TTarget> > expression)
        {
            MetadataIdentity  newIdentity = this.Source.Identity.Push(this.Schema.Notation.Lambda(expression));
            IRelationMetadata metadata    = newIdentity.Lookup <IRelationMetadata>();

            return(new RelationHeader <TSource>(this.Source, this.Add(metadata)));
        }
Exemple #8
0
        private Action <IDataReader> GetUpdateAction(IDataReader dataReader)
        {
            List <ColumnName>  names      = new List <ColumnName>();
            List <FieldBuffer> bufferList = new List <FieldBuffer>();

            for (int i = 0; i < this.GetFieldCount(dataReader); i++)
            {
                string columnName = dataReader.GetName(i);

                if (this.columnHeader.TryGetValue(columnName, out FieldBuffer buffer))
                {
                    MetadataIdentity metadata   = buffer.Target.Identity.Metadata;
                    ColumnMetadata   columnInfo = GetColumnInfo(i);

                    names.Add(new ColumnName(metadata, columnInfo));
                    bufferList.Add(buffer);

                    buffer.Column.Metadata = columnInfo;
                }
            }

            BufferWriter writer = CommandCache.GetWriter(names);

            FieldBuffer[] buffers = bufferList.ToArray();

            return(dr => writer(dr, buffers));

            ColumnMetadata GetColumnInfo(int i) => new ColumnMetadata(dataReader.GetName(i), dataReader.GetFieldType(i), dataReader.GetDataTypeName(i), i);
        }
Exemple #9
0
        private static Node AddDynamicNode(NodeTree tree, MetadataIdentity identity, IBindingMetadata metadata)
        {
            AddStaticNode(tree, metadata);

            Node             thisNode       = tree.FindNode(identity);
            MetadataIdentity parentIdentity = identity.Pop();

            if (thisNode != null)
            {
                return(thisNode);
            }
            else if (parentIdentity != null)
            {
                Node parentNode = tree.FindNode(parentIdentity) ?? AddDynamicNode(tree, parentIdentity, metadata);

                if (parentNode != null)
                {
                    thisNode = new Node(identity, metadata)
                    {
                        IsDynamic = true,
                    };

                    parentNode.Properties.Add(thisNode);
                    tree.Nodes.Add(thisNode);
                }
            }

            return(thisNode);
        }
Exemple #10
0
        public ItemBuilder(RelationIdentity relation, MetadataIdentity source)
        {
            this.relation        = relation ?? throw new ArgumentNullException(nameof(relation));
            this.sourceMetadata  = source.GetMetadata <IRelationMetadata>();
            this.headingMetadata = relation.Heading.Select(m => m.GetMetadata <IRelationMetadata>()).ToArray();

            this.Validate();
        }
        public static BufferConverter GetConverter(MetadataIdentity metadata, ColumnMetadata columnMetadata)
        {
            CommandCacheKey cacheKey = new CommandCacheKey(metadata, columnMetadata);

            return(converterMap.GetOrAdd(cacheKey, k =>
            {
                CommandCompiler compiler = new CommandCompiler();

                return compiler.Compile(metadata, columnMetadata);
            }));
        }
        public CustomMetadata GetMetadata(IMetadataBuilderContext context)
        {
            MetadataIdentity parentIdentity = context.Identity.Parent();

            if (parentIdentity != null)
            {
                return(context.Schema.GetMetadata <CustomMetadata>(parentIdentity.Name));
            }

            return(null);
        }
Exemple #13
0
        private static IRelationMetadata GetMetadata(MetadataIdentity identity)
        {
            IRelationMetadata metadata = identity.Lookup <IRelationMetadata>();

            if (metadata == null)
            {
                throw new InvalidOperationException(identity.Name + " not found");
            }

            return(metadata);
        }
Exemple #14
0
        public ProcLookupKey(string prefix, IProjectionIdentity identity, MetadataIdentity metadata, IField field)
        {
            this.Prefix   = prefix;
            this.Identity = identity ?? throw new ArgumentNullException(nameof(identity));
            this.Metadata = metadata;
            this.Field    = field;

            if (string.IsNullOrWhiteSpace(this.Prefix))
            {
                throw new ArgumentException("Prefix cannot be empty.");
            }
        }
Exemple #15
0
        public CommandCacheKey(MetadataIdentity metadata, ColumnMetadata column)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            this.Columns = new List <ColumnName>()
            {
                new ColumnName(metadata, column)
            };
        }
Exemple #16
0
        public void Add(MetadataIdentity identity)
        {
            if (!this.targets.Contains(identity))
            {
                lock (this.lockState)
                {
                    this.targets.Add(identity);

                    this.UpdateFactory();
                }
            }
        }
Exemple #17
0
        public void Bind()
        {
            if (this.Target != null && this.Read(out object value))
            {
                MetadataIdentity metadata  = this.Target.Identity.Metadata;
                ColumnMetadata   column    = this.Column?.Metadata;
                BufferConverter  converter = CommandCache.GetConverter(metadata, column);

                this.Target.Update(converter(value));
                this.Target.Commit();
            }
        }
Exemple #18
0
        public static FuncDescriptor GetDescriptor(RelationIdentity relation, MetadataIdentity source)
        {
            FuncKey key = new FuncKey(relation, source);

            return(cache.GetOrAdd(key, _ =>
            {
                ListBuilder listBuilder = new ListBuilder(relation, source);
                RelationNode relationNode = listBuilder.Build();
                FuncBuilder funcBuilder = new FuncBuilder(relationNode);

                return funcBuilder.Build();
            }));
        }
Exemple #19
0
        internal static RelationException Unreachable(MetadataIdentity source, IRelationHeader header, IList <IRelationMetadata> attributes)
        {
            string attributeNames = string.Join(", ", attributes.Select(a => $"\"{a.Identity.Name}\""));

            if (attributes.Count == 1)
            {
                return(From(header, $"Attribute {attributeNames} is unreachable from source \"{source.Name}\"."));
            }
            else
            {
                return(From(header, $"Attributes {attributeNames} are unreachable from source \"{source.Name}\"."));
            }
        }
        public BufferTree Parse(MetadataIdentity source, IRelationHeader header)
        {
            NodeTree   nodeTree = NodeParser.Parse(source, header);
            BufferTree tree     = new BufferTree()
            {
                Notation = new DotNotation(),
                Header   = header,
            };

            this.CreateSource(nodeTree.Source, tree);

            return(tree);
        }
Exemple #21
0
        private static BufferWriter GetWriter(MetadataIdentity source, IRelationHeader header)
        {
            RelationCacheKey key = new RelationCacheKey(source, header);

            return(cache.GetOrAdd(key, _ =>
            {
                BufferParser parser = new BufferParser();
                BufferTree tree = parser.Parse(source, header);
                RelationCompiler compiler = new RelationCompiler();

                return compiler.Compile(tree);
            }));
        }
        public ProcLookupKey(string prefix, ProjectionIdentity identity, MetadataIdentity metadata, IField field)
        {
            this.Prefix   = prefix;
            this.Identity = identity;
            this.Metadata = metadata;
            this.Field    = field;
            this.HasValue = (identity != null || metadata != null || field != null);

            if (string.IsNullOrWhiteSpace(this.Prefix))
            {
                throw new ArgumentException("Prefix cannot be empty.");
            }
        }
Exemple #23
0
        private int GetParentIndex(MetadataIdentity identity, IReferenceKey key)
        {
            IndexKey parentKey = new IndexKey(identity, key);

            if (this.parentMap.TryGetValue(parentKey, out int parentIndex))
            {
                return(parentIndex);
            }

            lock (this.state)
            {
                return(this.parentMap.GetOrAdd(parentKey, this.parentMap.Count));
            }
        }
        public int GetJoinIndex(IReference reference)
        {
            IReference       childReference = reference.Find(ReferenceFlags.Child);
            MetadataIdentity target         = childReference.Metadata.Identity;

            int parentIndex = this.GetListIndex(reference);

            lock (this.state)
            {
                Dictionary <MetadataIdentity, int> innerMap = this.childMap.GetOrAdd(parentIndex);

                return(innerMap.GetOrAdd(target, innerMap.Count));
            }
        }
Exemple #25
0
        protected T FindData <T>(Node node, IEnumerable <T> header)
            where T : DataAttribute
        {
            foreach (T attribute in header)
            {
                MetadataIdentity metadata = new MetadataIdentity(node.Metadata.Identity.Schema, attribute.Name);

                if (metadata.Equals(node.Identity))
                {
                    return(attribute);
                }
            }

            return(null);
        }
Exemple #26
0
        public Missing(string name, MetadataIdentity metadata, IField model)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            this.Identity = new FieldIdentity(metadata, name);
            this.Model    = model ?? throw new ArgumentNullException(nameof(model));
        }
Exemple #27
0
 private static string GetAttributeName(MetadataIdentity identity)
 {
     if (identity == null)
     {
         return("<missing>");
     }
     else if (identity.Schema.Notation.Comparer.Equals(identity.Name, identity.Schema.Notation.Model()))
     {
         return("<model>");
     }
     else
     {
         return(identity.Name);
     }
 }
        private IProjectionMetadata GetMetadata(IMetadataBuilderContext context, MetadataIdentity identity)
        {
            MetadataIdentity    parentIdentity = identity.Parent();
            IProjectionMetadata parent         = context.GetMetadata <IProjectionMetadata>(parentIdentity.Name) ?? this.GetMetadata(context, parentIdentity);

            if (parent == null)
            {
                return(null);
            }
            else if (parent.Item != null && parent.Item.Identity.Equals(identity))
            {
                return(parent.Item);
            }

            return(parent.Properties.FirstOrDefault(m => m.Identity.Equals(identity)));
        }
Exemple #29
0
        private IBindingMetadata FindDynamicMetadata(MetadataIdentity identity)
        {
            IBindingMetadata metadata = identity.GetMetadata <IBindingMetadata>();

            while (metadata == null && (identity = identity.Pop()) != null)
            {
                metadata = identity.GetMetadata <IBindingMetadata>();
            }

            if (metadata != null && metadata.HasFlag(BindingMetadataFlags.Dynamic))
            {
                return(metadata);
            }

            return(null);
        }
Exemple #30
0
        private void AddValueNode(List <MetadataNode> itemNodes, ColumnIdentity value)
        {
            MetadataIdentity identity = new MetadataIdentity(this.schema, value.Name);
            IBindingMetadata metadata = identity.GetMetadata <IBindingMetadata>() ?? this.FindDynamicMetadata(identity);

            if (metadata != null && !this.IsOutsideResultScope(metadata))
            {
                if (metadata.HasFlag(BindingMetadataFlags.Dynamic))
                {
                    this.AddDynamicNode(itemNodes, identity, metadata, value);
                }
                else
                {
                    this.AddStaticNode(itemNodes, metadata, value);
                }
            }
        }