Esempio n. 1
0
        public OrderByPropertyNode(Stack <OrderByQueryNode> nodes)
        {
            if (nodes == null)
            {
                throw Error.ArgumentNull("nodes");
            }

            if (nodes.Count == 0)
            {
                throw new ODataException(SRResources.OrderByNodeNotFound);
            }

            OrderByQueryNode        currentNode = nodes.Pop();
            PropertyAccessQueryNode property    = currentNode.Expression as PropertyAccessQueryNode;

            if (property == null)
            {
                throw new ODataException(SRResources.OrderByPropertyNotFound);
            }

            Property  = property.Property;
            Direction = currentNode.Direction;

            if (nodes.Count > 0)
            {
                ThenBy = new OrderByPropertyNode(nodes);
            }
        }
Esempio n. 2
0
        public void CreateCollection_From_OrderByQueryNode_Succeeds()
        {
            // Arrange
            Mock <IEdmTypeReference> mockTypeReference1 = new Mock <IEdmTypeReference>();
            Mock <IEdmTypeReference> mockTypeReference2 = new Mock <IEdmTypeReference>();
            Mock <IEdmProperty>      mockProperty1      = new Mock <IEdmProperty>();

            mockProperty1.SetupGet <IEdmTypeReference>(p => p.Type).Returns(mockTypeReference1.Object);
            Mock <IEdmProperty> mockProperty2 = new Mock <IEdmProperty>();

            mockProperty1.SetupGet <IEdmTypeReference>(p => p.Type).Returns(mockTypeReference2.Object);
            PropertyAccessQueryNode propertyAccessQueryNode1 = new PropertyAccessQueryNode()
            {
                Property = mockProperty1.Object,
            };
            PropertyAccessQueryNode propertyAccessQueryNode2 = new PropertyAccessQueryNode()
            {
                Property = mockProperty2.Object,
            };

            OrderByQueryNode queryNode1 = new OrderByQueryNode()
            {
                Direction  = OrderByDirection.Descending,
                Collection = null,
                Expression = propertyAccessQueryNode1
            };

            OrderByQueryNode queryNode2 = new OrderByQueryNode()
            {
                Direction  = OrderByDirection.Ascending,
                Collection = queryNode1,
                Expression = propertyAccessQueryNode2
            };

            // Act
            ICollection <OrderByPropertyNode> nodes = OrderByPropertyNode.CreateCollection(queryNode2);

            // Assert
            Assert.Equal(2, nodes.Count);
            Assert.ReferenceEquals(mockProperty1.Object, nodes.First().Property);
            Assert.Equal(OrderByDirection.Descending, nodes.First().Direction);

            Assert.ReferenceEquals(mockProperty2.Object, nodes.Last().Property);
            Assert.Equal(OrderByDirection.Ascending, nodes.Last().Direction);
        }
        public void CreateCollection_From_OrderByQueryNode_Succeeds()
        {
            // Arrange
            Mock<IEdmTypeReference> mockTypeReference1 = new Mock<IEdmTypeReference>();
            Mock<IEdmTypeReference> mockTypeReference2 = new Mock<IEdmTypeReference>();
            Mock<IEdmProperty> mockProperty1 = new Mock<IEdmProperty>();
            mockProperty1.SetupGet<IEdmTypeReference>(p => p.Type).Returns(mockTypeReference1.Object);
            Mock<IEdmProperty> mockProperty2 = new Mock<IEdmProperty>();
            mockProperty1.SetupGet<IEdmTypeReference>(p => p.Type).Returns(mockTypeReference2.Object);
            PropertyAccessQueryNode propertyAccessQueryNode1 = new PropertyAccessQueryNode()
            {
                Property = mockProperty1.Object,
            };
            PropertyAccessQueryNode propertyAccessQueryNode2 = new PropertyAccessQueryNode()
            {
                Property = mockProperty2.Object,
            };

            OrderByQueryNode queryNode1 = new OrderByQueryNode()
            {
                Direction = OrderByDirection.Descending,
                Collection = null,
                Expression = propertyAccessQueryNode1
            };

            OrderByQueryNode queryNode2 = new OrderByQueryNode()
            {
                Direction = OrderByDirection.Ascending,
                Collection = queryNode1,
                Expression = propertyAccessQueryNode2
            };

            // Act
            ICollection<OrderByPropertyNode> nodes = OrderByPropertyNode.CreateCollection(queryNode2);

            // Assert
            Assert.Equal(2, nodes.Count);
            Assert.ReferenceEquals(mockProperty1.Object, nodes.First().Property);
            Assert.Equal(OrderByDirection.Descending, nodes.First().Direction);

            Assert.ReferenceEquals(mockProperty2.Object, nodes.Last().Property);
            Assert.Equal(OrderByDirection.Ascending, nodes.Last().Direction);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a collection of <see cref="OrderByPropertyNode"/>
        /// instances from a linked list of <see cref="OrderByQueryNode"/>
        /// instances.
        /// </summary>
        /// <remarks>The order of the items in the <see cref="OrderByQueryNode"/>
        /// linked list will be reversed in the <see cref="OrderByPropertyNode"/>
        /// collection.</remarks>
        /// <param name="node">The head of the <see cref="OrderByQueryNode"/>
        /// linked list.</param>
        /// <returns>The collection of new <see cref="OrderByPropertyNode"/> instances.</returns>
        public static ICollection <OrderByPropertyNode> CreateCollection(OrderByQueryNode node)
        {
            LinkedList <OrderByPropertyNode> result = new LinkedList <OrderByPropertyNode>();

            for (OrderByQueryNode currentNode = node;
                 currentNode != null;
                 currentNode = currentNode.Collection as OrderByQueryNode)
            {
                PropertyAccessQueryNode property = currentNode.Expression as PropertyAccessQueryNode;

                if (property == null)
                {
                    throw new ODataException(SRResources.OrderByPropertyNotFound);
                }
                result.AddFirst(new OrderByPropertyNode(property.Property, currentNode.Direction));
            }

            return(result);
        }
Esempio n. 5
0
        private object ProcessNode(PropertyAccessQueryNode propertyAccessQueryNode)
        {
            string srcVariable;

            if (propertyAccessQueryNode.Source is NavigationPropertyNode)
            {
                srcVariable = ProcessNode(propertyAccessQueryNode.Source as NavigationPropertyNode).ToString();
            }
            else
            {
                srcVariable = AssertInstancesVariable(propertyAccessQueryNode.Source.TypeReference);
            }
            var declType = propertyAccessQueryNode.Property.DeclaringType as IEdmEntityType;

            if (declType == null)
            {
                throw new NotImplementedException("Cannot handle property access on declaring type " +
                                                  propertyAccessQueryNode.Property.DeclaringType);
            }
            var propertyUri = _map.GetUriForProperty(
                declType,
                propertyAccessQueryNode.Property.Name);

            if (propertyUri != null)
            {
                return("?" + AssertPropertyAccessVariable(srcVariable, propertyUri));
            }
            string identifierPrefix;

            if (_map.TryGetIdentifierPrefixForProperty(declType.FullName(), propertyAccessQueryNode.Property.Name,
                                                       out identifierPrefix))
            {
                return(new VariableIdentifierBinding(srcVariable, identifierPrefix));
            }
            throw new NotImplementedException(
                      String.Format("Cannot handle property access for {0}.{1}", propertyAccessQueryNode.TypeReference.FullName(), propertyAccessQueryNode.Property.Name));
        }
Esempio n. 6
0
        private Expression BindPropertyAccessQueryNode(PropertyAccessQueryNode propertyAccessNode)
        {
            Expression source = Bind(propertyAccessNode.Source);

            return(CreatePropertyAccessExpression(source, propertyAccessNode.Property.Name));
        }
        /// <summary>
        /// Translates a property access node.
        /// </summary>
        /// <param name="propertyAccessNode">The property access node to translate.</param>
        /// <returns>Expression which evaluates to the result of the property access.</returns>
        protected virtual Expression TranslatePropertyAccess(PropertyAccessQueryNode propertyAccessNode)
        {
            ExceptionUtils.CheckArgumentNotNull(propertyAccessNode, "propertyAccessNode");
            ExceptionUtils.CheckArgumentNotNull(propertyAccessNode.Source, "propertyAccessNode.Source");
            ExceptionUtils.CheckArgumentNotNull(propertyAccessNode.Source.TypeReference, "propertyAccessNode.Source.TypeReference");
            ExceptionUtils.CheckArgumentNotNull(propertyAccessNode.Property, "propertyAccessNode.Property");

            Expression source = this.Translate(propertyAccessNode.Source);
            if (!propertyAccessNode.Source.TypeReference.GetInstanceType(this.model).IsAssignableFrom(source.Type))
            {
                throw new ODataException(Strings.QueryExpressionTranslator_PropertyAccessSourceWrongType(source.Type, propertyAccessNode.Source.TypeReference.GetInstanceType(this.model)));
            }

            if (propertyAccessNode.Property.GetCanReflectOnInstanceTypeProperty(this.model))
            {
                Debug.Assert(source != nullLiteralExpression, "source != nullLiteralExpression");

                IEdmTypeReference sourceType = propertyAccessNode.Source.TypeReference;
                IEdmStructuredTypeReference structuredSourceTypeReference = sourceType.AsStructuredOrNull();
                if (structuredSourceTypeReference == null)
                {
                    throw new ODataException(Strings.QueryExpressionTranslator_PropertyAccessSourceNotStructured(sourceType.TypeKind()));
                }

                Expression propertyAccessExpression = Expression.Property(source, structuredSourceTypeReference.GetPropertyInfo(propertyAccessNode.Property, this.model));

                // Add null propagation code if the parent is not the parameter node (which will never be null) and is nullable
                if (this.nullPropagationRequired && source.NodeType != ExpressionType.Parameter && TypeUtils.TypeAllowsNull(source.Type))
                {
                    Expression test = Expression.Equal(source, Expression.Constant(null, source.Type));
                    Type propagatedType = TypeUtils.GetNullableType(propertyAccessExpression.Type);

                    if (propagatedType != propertyAccessExpression.Type)
                    {
                        // we need to convert the property access result to the propagated type
                        propertyAccessExpression = Expression.Convert(propertyAccessExpression, propagatedType);
                    }

                    propertyAccessExpression = Expression.Condition(test, Expression.Constant(null, propagatedType), propertyAccessExpression);
                }

                return propertyAccessExpression;
            }
            else
            {
                // TODO: support for untyped and open properties
                throw new NotImplementedException();
            }
        }
Esempio n. 8
0
        private object ProcessNode(PropertyAccessQueryNode propertyAccessQueryNode)
        {
            string srcVariable;

            if (propertyAccessQueryNode.Source is NavigationPropertyNode)
            {
                srcVariable = ProcessNode(propertyAccessQueryNode.Source as NavigationPropertyNode).ToString();
            }
            else
            {
                srcVariable = AssertInstancesVariable(propertyAccessQueryNode.Source.TypeReference);
            }
            var declType = propertyAccessQueryNode.Property.DeclaringType as IEdmEntityType;
            if (declType == null)
            {
                throw new NotImplementedException("Cannot handle property access on declaring type " +
                                                  propertyAccessQueryNode.Property.DeclaringType);
            }
            var propertyUri = _map.GetUriForProperty(
                declType,
                propertyAccessQueryNode.Property.Name);
            if (propertyUri != null)
            {
                return "?" + AssertPropertyAccessVariable(srcVariable, propertyUri);
            }
            string identifierPrefix;
            if (_map.TryGetIdentifierPrefixForProperty(declType.FullName(), propertyAccessQueryNode.Property.Name,
                                                       out identifierPrefix))
            {
                return new VariableIdentifierBinding(srcVariable, identifierPrefix);
            }
            throw new NotImplementedException(
               String.Format("Cannot handle property access for {0}.{1}", propertyAccessQueryNode.TypeReference.FullName(), propertyAccessQueryNode.Property.Name));
        }