Exemple #1
0
        internal OeQueryContext(IEdmModel edmModel, ODataUri odataUri,
                                IEdmEntitySet entitySet, IReadOnlyList <OeParseNavigationSegment> parseNavigationSegments,
                                bool isCountSegment, int pageSize, bool navigationNextLink, bool isDatabaseNullHighestValue,
                                OeMetadataLevel metadataLevel, Db.OeEntitySetAdapter entitySetAdapter)
        {
            EntitySetAdapter           = entitySetAdapter;
            EdmModel                   = edmModel;
            ODataUri                   = odataUri;
            EntitySet                  = entitySet;
            ParseNavigationSegments    = parseNavigationSegments;
            IsCountSegment             = isCountSegment;
            PageSize                   = pageSize;
            NavigationNextLink         = navigationNextLink;
            IsDatabaseNullHighestValue = isDatabaseNullHighestValue;
            MetadataLevel              = metadataLevel;

            var visitor = new OeQueryNodeVisitor(edmModel, Expression.Parameter(entitySetAdapter.EntityType));

            JoinBuilder = new Translators.OeJoinBuilder(visitor);

            if (pageSize > 0 || (odataUri.OrderBy != null && odataUri.Skip != null && odataUri.Top != null))
            {
                SkipTokenNameValues = OeSkipTokenParser.CreateNameValues(edmModel, odataUri.OrderBy, odataUri.SkipToken);
            }
        }
Exemple #2
0
        private void Initialize()
        {
            if (!_initialized)
            {
                _initialized = true;

                int pageSize = ODataUri.GetPageSize();
                if (pageSize > 0)
                {
                    _restCount   = (int?)ODataUri.Top ?? Int32.MaxValue;
                    ODataUri.Top = pageSize;
                }

                if (!(ODataUri.Path.LastSegment is OperationSegment))
                {
                    ODataUri.OrderBy = OeSkipTokenParser.GetUniqueOrderBy(GetEntitySet(), ODataUri.OrderBy, ODataUri.Apply);
                    if (ODataUri.SkipToken == null)
                    {
                        SkipTokenNameValues = Array.Empty <OeSkipTokenNameValue>();
                    }
                    else
                    {
                        SkipTokenNameValues = OeSkipTokenParser.ParseSkipToken(EdmModel, ODataUri.OrderBy, ODataUri.SkipToken, out _restCount);
                    }
                }
            }
        }
Exemple #3
0
        public Expression CreateExpression(OeConstantToVariableVisitor constantToVariableVisitor)
        {
            Expression expression;
            var        expressionBuilder = new OeExpressionBuilder(EdmModel, EntitySetAdapter.EntityType);

            expression = Expression.Constant(null, typeof(IEnumerable <>).MakeGenericType(EntitySetAdapter.EntityType));
            expression = expressionBuilder.ApplyNavigation(expression, ParseNavigationSegments);
            expression = expressionBuilder.ApplyFilter(expression, ODataUri.Filter);
            expression = expressionBuilder.ApplySkipToken(expression, SkipTokenNameValues, ODataUri.OrderBy, IsDatabaseNullHighestValue);
            expression = expressionBuilder.ApplyAggregation(expression, ODataUri.Apply);
            expression = expressionBuilder.ApplySelect(expression, this);
            expression = expressionBuilder.ApplyOrderBy(expression, ODataUri.OrderBy);
            expression = expressionBuilder.ApplySkip(expression, ODataUri.Skip, ODataUri.Path);
            expression = expressionBuilder.ApplyTake(expression, ODataUri.Top, ODataUri.Path);
            expression = expressionBuilder.ApplyCount(expression, IsCountSegment);

            if (!IsCountSegment)
            {
                EntryFactory = CreateEntryFactory(expressionBuilder);
            }
            if (SkipTokenNameValues != null)
            {
                SkipTokenAccessors = OeSkipTokenParser.GetAccessors(expression, ODataUri.OrderBy);
            }

            return(constantToVariableVisitor.Translate(expression, expressionBuilder.Constants));
        }
 public OeCacheContext(OeQueryContext queryContext)
 {
     _odataUri  = queryContext.ODataUri;
     _entitySet = queryContext.EntitySet;
     _parseNavigationSegments = queryContext.ParseNavigationSegments;
     _metadataLevel           = queryContext.MetadataLevel;
     _navigationNextLink      = queryContext.NavigationNextLink;
     _skipTokenParser         = queryContext.SkipTokenParser;
 }
Exemple #5
0
        public OeQueryContext CreateQueryContext(ODataUri odataUri, int pageSize, bool navigationNextLink, OeMetadataLevel metadataLevel)
        {
            List <OeParseNavigationSegment> navigationSegments = null;

            if (odataUri.Path.LastSegment is KeySegment ||
                odataUri.Path.LastSegment is NavigationPropertySegment)
            {
                navigationSegments = new List <OeParseNavigationSegment>();
                ODataPathSegment previousSegment = null;
                foreach (ODataPathSegment segment in odataUri.Path)
                {
                    if (segment is NavigationPropertySegment navigationSegment)
                    {
                        navigationSegments.Add(new OeParseNavigationSegment(navigationSegment, null));
                    }
                    else if (segment is KeySegment keySegment)
                    {
                        IEdmEntitySet previousEntitySet;
                        navigationSegment = null;
                        if (previousSegment is EntitySetSegment)
                        {
                            var previousEntitySetSegment = previousSegment as EntitySetSegment;
                            previousEntitySet = previousEntitySetSegment.EntitySet;
                        }
                        else if (previousSegment is NavigationPropertySegment)
                        {
                            navigationSegment = previousSegment as NavigationPropertySegment;
                            previousEntitySet = (IEdmEntitySet)navigationSegment.NavigationSource;
                        }
                        else
                        {
                            throw new InvalidOperationException("invalid segment");
                        }

                        FilterClause keyFilter = CreateFilterClause(previousEntitySet, keySegment.Keys);
                        navigationSegments.Add(new OeParseNavigationSegment(navigationSegment, keyFilter));
                    }
                    previousSegment = segment;
                }
            }

            var entitySetSegment = (EntitySetSegment)odataUri.Path.FirstSegment;

            if (pageSize > 0)
            {
                odataUri.Top = pageSize;
                IEdmEntitySet resultEntitySet = OeParseNavigationSegment.GetEntitySet(navigationSegments) ?? entitySetSegment.EntitySet;
                odataUri.OrderBy = OeSkipTokenParser.GetUniqueOrderBy(_edmModel, resultEntitySet, odataUri.OrderBy, odataUri.Apply);
            }

            Db.OeEntitySetAdapter entitySetAdapter = _edmModel.GetEntitySetAdapter(entitySetSegment.EntitySet);
            bool isCountSegment = odataUri.Path.LastSegment is CountSegment;

            return(new OeQueryContext(_edmModel, odataUri, navigationSegments, isCountSegment, pageSize, navigationNextLink, metadataLevel, entitySetAdapter));
        }
        private static BinaryExpression CreateBinaryExpression(OeQueryNodeVisitor visitor, bool isDatabaseNullHighestValue, ref OrderProperty orderProperty)
        {
            MemberExpression propertyExpression = orderProperty.PropertyExpression;

            ConstantExpression parameterExpression = orderProperty.ParmeterExpression;

            if (parameterExpression == null)
            {
                parameterExpression = Expression.Constant(orderProperty.Value, propertyExpression.Type);
                orderProperty.ParmeterExpression = parameterExpression;
                visitor.AddSkipTokenConstant(parameterExpression, OeSkipTokenParser.GetPropertyName(propertyExpression));
            }

            ExpressionType   binaryType = orderProperty.Direction == OrderByDirection.Ascending ? ExpressionType.GreaterThan : ExpressionType.LessThan;
            BinaryExpression compare;

            if (propertyExpression.Type == typeof(String))
            {
                Func <String, String, int> compareToFunc = String.Compare;
                MethodCallExpression       compareToCall = Expression.Call(null, compareToFunc.GetMethodInfo(), propertyExpression, parameterExpression);
                compare = Expression.MakeBinary(binaryType, compareToCall, OeConstantToVariableVisitor.ZeroStringCompareConstantExpression);
            }
            else
            {
                compare = Expression.MakeBinary(binaryType, propertyExpression, parameterExpression);
            }

            if (orderProperty.PropertyNode.TypeReference.IsNullable)
            {
                BinaryExpression isNull;
                UnaryExpression  typedNull = Expression.Convert(OeConstantToVariableVisitor.NullConstantExpression, parameterExpression.Type);

                OrderByDirection direction = orderProperty.Direction;
                if (isDatabaseNullHighestValue)
                {
                    direction = orderProperty.Direction == OrderByDirection.Ascending ? OrderByDirection.Descending : OrderByDirection.Ascending;
                }

                if (direction == OrderByDirection.Ascending)
                {
                    BinaryExpression isNullParameter   = Expression.Equal(parameterExpression, typedNull);
                    BinaryExpression isNotNullProperty = Expression.NotEqual(propertyExpression, OeConstantToVariableVisitor.NullConstantExpression);
                    isNull = Expression.AndAlso(isNullParameter, isNotNullProperty);
                }
                else
                {
                    BinaryExpression isNotNullParameter = Expression.NotEqual(parameterExpression, typedNull);
                    BinaryExpression isNullProperty     = Expression.Equal(propertyExpression, OeConstantToVariableVisitor.NullConstantExpression);
                    isNull = Expression.AndAlso(isNotNullParameter, isNullProperty);
                }
                compare = Expression.OrElse(compare, isNull);
            }

            return(compare);
        }
        public Expression ApplySkipToken(Expression source, OeSkipTokenParser skipTokenParser, String skipToken)
        {
            if (skipToken == null)
            {
                return(source);
            }

            var skipTokenTranslator = new OeSkipTokenTranslator(_visitor, skipTokenParser);

            return(skipTokenTranslator.Build(source, skipToken));
        }
        private static OrderProperty[] CreateOrderProperies(OeSkipTokenParser skipTokenParser, String skipToken)
        {
            var orderProperties = new List <OrderProperty>();

            foreach (KeyValuePair <String, Object> keyValue in skipTokenParser.ParseSkipToken(skipToken))
            {
                OrderByClause orderBy      = GetOrderBy(skipTokenParser.UniqueOrderBy, keyValue.Key);
                var           propertyNode = (SingleValuePropertyAccessNode)orderBy.Expression;
                orderProperties.Add(new OrderProperty(propertyNode, orderBy.Direction, keyValue.Value));
            }
            return(orderProperties.ToArray());
        }
        private static OrderByClause GetOrderBy(OrderByClause orderByClause, String propertyName)
        {
            while (orderByClause != null)
            {
                IEdmProperty edmProperty = ((SingleValuePropertyAccessNode)orderByClause.Expression).Property;
                if (String.Compare(OeSkipTokenParser.GetPropertyName(edmProperty), propertyName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(orderByClause);
                }

                orderByClause = orderByClause.ThenBy;
            }

            throw new InvalidOperationException("Property " + propertyName + " not found in OrderBy");
        }
Exemple #10
0
        public OeQueryContext CreateQueryContext(ODataUri odataUri, int pageSize, bool navigationNextLink, OeMetadataLevel metadataLevel)
        {
            IReadOnlyList <OeParseNavigationSegment> navigationSegments = OeParseNavigationSegment.GetNavigationSegments(odataUri.Path);
            var entitySetSegment = (EntitySetSegment)odataUri.Path.FirstSegment;

            if (pageSize > 0)
            {
                odataUri.Top = pageSize;
                IEdmEntitySet resultEntitySet = OeParseNavigationSegment.GetEntitySet(navigationSegments) ?? entitySetSegment.EntitySet;
                odataUri.OrderBy = OeSkipTokenParser.GetUniqueOrderBy(_edmModel, resultEntitySet, odataUri.OrderBy, odataUri.Apply);
            }

            Db.OeEntitySetAdapter entitySetAdapter = _edmModel.GetEntitySetAdapter(entitySetSegment.EntitySet);
            bool isCountSegment = odataUri.Path.LastSegment is CountSegment;

            return(new OeQueryContext(_edmModel, odataUri, navigationSegments, isCountSegment, pageSize, navigationNextLink, metadataLevel, entitySetAdapter));
        }
        internal OeQueryContext(IEdmModel edmModel, ODataUri odataUri,
                                IEdmEntitySet entitySet, IReadOnlyList <OeParseNavigationSegment> parseNavigationSegments,
                                bool isCountSegment, int pageSize, bool navigationNextLink, bool isDatabaseNullHighestValue,
                                OeMetadataLevel metadataLevel, ref Db.OeEntitySetAdapter entitySetAdapter)
        {
            EntitySetAdapter        = entitySetAdapter;
            EdmModel                = edmModel;
            ODataUri                = odataUri;
            EntitySet               = entitySet;
            ParseNavigationSegments = parseNavigationSegments;
            IsCountSegment          = isCountSegment;
            PageSize                = pageSize;
            NavigationNextLink      = navigationNextLink;
            MetadataLevel           = metadataLevel;

            if (pageSize > 0 || (odataUri.OrderBy != null && odataUri.Skip != null && odataUri.Top != null))
            {
                IEdmEntityType edmEntityType = OeGetParser.GetEntityType(odataUri.Path, parseNavigationSegments);
                SkipTokenParser = new OeSkipTokenParser(edmModel, edmEntityType, isDatabaseNullHighestValue, odataUri.OrderBy);
            }
        }
Exemple #12
0
        public Expression CreateExpression(out IReadOnlyDictionary <ConstantExpression, ConstantNode> constants)
        {
            Initialize();

            Expression expression;
            var        expressionBuilder = new OeExpressionBuilder(_joinBuilder);

            IEdmEntitySet entitySet = OeEdmClrHelper.GetEntitySet(EdmModel, EntitySetAdapter.EntitySetName);

            expression = OeEnumerableStub.CreateEnumerableStubExpression(EntitySetAdapter.EntityType, entitySet);
            expression = expressionBuilder.ApplyNavigation(expression, ParseNavigationSegments);
            if (ODataUri.Apply == null)
            {
                expression = expressionBuilder.ApplyFilter(expression, ODataUri.Filter);
                expression = expressionBuilder.ApplySelect(expression, this);
            }
            else
            {
                expression = expressionBuilder.ApplyAggregation(expression, ODataUri.Apply);
                expression = expressionBuilder.ApplyFilter(expression, ODataUri.Filter);
                expression = expressionBuilder.ApplySkipToken(expression, SkipTokenNameValues, ODataUri.OrderBy, IsDatabaseNullHighestValue); //order by aggregation
                expression = expressionBuilder.ApplyOrderBy(expression, ODataUri.OrderBy);
                expression = expressionBuilder.ApplySkip(expression, ODataUri.Skip, ODataUri.Path);
                expression = expressionBuilder.ApplyTake(expression, ODataUri.Top, ODataUri.Path);
            }

            if (ODataUri.Path.LastSegment is CountSegment)
            {
                expression = expressionBuilder.ApplyCount(expression, true);
            }
            else
            {
                OePropertyAccessor[] skipTokenAccessors = OeSkipTokenParser.GetAccessors(expression, ODataUri.OrderBy, _joinBuilder);
                EntryFactory = CreateEntryFactory(expressionBuilder, skipTokenAccessors);
            }

            constants = expressionBuilder.Constants;
            return(expression);
        }
Exemple #13
0
        public Expression CreateExpression(OeConstantToVariableVisitor constantToVariableVisitor)
        {
            Expression expression;
            var        expressionBuilder = new OeExpressionBuilder(JoinBuilder);

            IEdmEntitySet entitySet = OeEdmClrHelper.GetEntitySet(EdmModel, EntitySetAdapter.EntitySetName);

            expression = OeEnumerableStub.CreateEnumerableStubExpression(EntitySetAdapter.EntityType, entitySet);
            expression = expressionBuilder.ApplyNavigation(expression, ParseNavigationSegments);
            expression = expressionBuilder.ApplyFilter(expression, ODataUri.Filter);
            if (ODataUri.Apply == null)
            {
                expression = expressionBuilder.ApplySelect(expression, this);
            }
            else
            {
                expression = expressionBuilder.ApplySkipToken(expression, SkipTokenNameValues, ODataUri.OrderBy, IsDatabaseNullHighestValue);
                expression = expressionBuilder.ApplyAggregation(expression, ODataUri.Apply);
                expression = expressionBuilder.ApplyOrderBy(expression, ODataUri.OrderBy);
                expression = expressionBuilder.ApplySkip(expression, ODataUri.Skip, ODataUri.Path);
                expression = expressionBuilder.ApplyTake(expression, ODataUri.Top, ODataUri.Path);
            }
            expression = expressionBuilder.ApplyCount(expression, IsCountSegment);

            if (!IsCountSegment)
            {
                EntryFactory = CreateEntryFactory(expressionBuilder);
            }

            if (SkipTokenNameValues != null)
            {
                SkipTokenAccessors = OeSkipTokenParser.GetAccessors(expression, ODataUri.OrderBy, JoinBuilder);
            }

            return(constantToVariableVisitor.Translate(expression, expressionBuilder.Constants));
        }
 public OeSkipTokenTranslator(OeQueryNodeVisitor visitor, OeSkipTokenParser skipTokenParser)
 {
     _skipTokenParser = skipTokenParser;
     _visitor         = visitor;
 }