private OeEntryFactory CreateNestedEntryFactory(Type sourceType, IEdmEntitySet entitySet, ODataNestedResourceInfo resourceInfo)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            IReadOnlyList <MemberExpression> itemExpressions = OeExpressionHelper.GetPropertyExpression(Expression.Convert(parameter, sourceType));

            List <OeEntryFactory> navigationLinks = GetNavigationLinks(itemExpressions, parameter);

            OePropertyAccessor[] accessors;
            if (_pathSelect)
            {
                var accessorsList = new List <OePropertyAccessor>(_selectItemInfos.Count);
                for (int i = 0; i < _selectItemInfos.Count; i++)
                {
                    SelectItemInfo info = _selectItemInfos[i];
                    if (info.EdmProperty is IEdmStructuralProperty)
                    {
                        accessorsList.Add(OePropertyAccessor.CreatePropertyAccessor(info.EdmProperty, itemExpressions[i], parameter));
                    }
                }
                accessors = accessorsList.ToArray();
            }
            else
            {
                accessors = OePropertyAccessor.CreateFromExpression(itemExpressions[0], parameter, entitySet);
            }

            return(OeEntryFactory.CreateEntryFactoryNested(entitySet, accessors, resourceInfo, navigationLinks));
        }
        private List <OeEntryFactory> GetNavigationLinks(IReadOnlyList <MemberExpression> itemExpressions, ParameterExpression parameter)
        {
            var navigationLinks = new List <OeEntryFactory>(_selectItemInfos.Count);

            for (int i = 0; i < _selectItemInfos.Count; i++)
            {
                SelectItemInfo itemInfo = _selectItemInfos[i];
                if (itemInfo.EdmProperty is IEdmNavigationProperty)
                {
                    MemberExpression expression = itemExpressions[_pathSelect ? i : i + 1];

                    OeEntryFactory entryFactory;
                    if (itemInfo.EntryFactory == null)
                    {
                        Type type = expression.Type;
                        if (itemInfo.ResourceInfo.IsCollection.GetValueOrDefault())
                        {
                            type = OeExpressionHelper.GetCollectionItemType(type);
                        }

                        OePropertyAccessor[] accessors = OePropertyAccessor.CreateFromType(type, itemInfo.EntitySet);
                        entryFactory             = OeEntryFactory.CreateEntryFactoryChild(itemInfo.EntitySet, accessors, itemInfo.ResourceInfo);
                        entryFactory.CountOption = itemInfo.CountOption;
                    }
                    else
                    {
                        entryFactory = itemInfo.EntryFactory;
                    }
                    entryFactory.LinkAccessor = (Func <Object, Object>)Expression.Lambda(expression, parameter).Compile();

                    navigationLinks.Add(entryFactory);
                }
            }
            return(navigationLinks);
        }
Example #3
0
        private static OePropertyAccessor[] GetAccessorsWithoutSkiptoken(OePropertyAccessor[] accessors)
        {
            int skiptokenCount = 0;

            for (int i = 0; i < accessors.Length; i++)
            {
                if (accessors[i].SkipToken)
                {
                    skiptokenCount++;
                }
            }

            if (skiptokenCount == 0)
            {
                return(accessors);
            }

            var accessorsWithoutSkiptoken = new OePropertyAccessor[accessors.Length - skiptokenCount];
            int index = 0;

            for (int i = 0; i < accessors.Length; i++)
            {
                if (!accessors[i].SkipToken)
                {
                    accessorsWithoutSkiptoken[index++] = accessors[i];
                }
            }
            return(accessorsWithoutSkiptoken);
        }
        public OeEntryFactory CreateEntryFactory(Type entityType, IEdmEntitySetBase entitySet, Type sourceType)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            IReadOnlyList <MemberExpression> itemExpressions = OeExpressionHelper.GetPropertyExpression(Expression.Convert(parameter, sourceType));

            OeEntryFactory        entryFactory;
            List <OeEntryFactory> navigationLinks = GetNavigationLinks(itemExpressions, parameter);

            if (_pathSelect)
            {
                var accessors = new List <OePropertyAccessor>(_selectItemInfos.Count);
                for (int i = 0; i < _selectItemInfos.Count; i++)
                {
                    SelectItemInfo info = _selectItemInfos[i];
                    if (info.EdmProperty is IEdmStructuralProperty)
                    {
                        accessors.Add(OePropertyAccessor.CreatePropertyAccessor(info.EdmProperty, itemExpressions[i], parameter));
                    }
                }
                entryFactory = OeEntryFactory.CreateEntryFactoryParent(entitySet, accessors.ToArray(), navigationLinks);
            }
            else
            {
                OePropertyAccessor[] accessors = OePropertyAccessor.CreateFromType(entityType, entitySet);
                entryFactory = OeEntryFactory.CreateEntryFactoryParent(entitySet, accessors, navigationLinks);
                entryFactory.LinkAccessor = (Func <Object, Object>)Expression.Lambda(itemExpressions[0], parameter).Compile();
            }
            return(entryFactory);
        }
Example #5
0
        public static                          OePropertyAccessor[] CreateFromTuple(Type tupleType, IReadOnlyList <IEdmProperty> edmProperties, int groupItemIndex)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            IReadOnlyList <MemberExpression> itemExpressions = OeExpressionHelper.GetPropertyExpressions(Expression.Convert(parameter, tupleType));

            int aliasIndex = 0;
            var accessors  = new OePropertyAccessor[edmProperties.Count];

            if (groupItemIndex >= 0)
            {
                IReadOnlyList <MemberExpression> groupExpressions = OeExpressionHelper.GetPropertyExpressions(itemExpressions[groupItemIndex]);
                for (; aliasIndex < groupExpressions.Count; aliasIndex++)
                {
                    accessors[aliasIndex] = CreatePropertyAccessor(edmProperties[aliasIndex], groupExpressions[aliasIndex], parameter, false);
                }
            }

            for (int itemIndex = 0; itemIndex < itemExpressions.Count; itemIndex++)
            {
                if (itemIndex != groupItemIndex)
                {
                    accessors[aliasIndex] = CreatePropertyAccessor(edmProperties[aliasIndex], itemExpressions[itemIndex], parameter, false);
                    aliasIndex++;
                }
            }
            return(accessors);
        }
Example #6
0
        private OeEntryFactory CreateEntryFactoryFromTuple(IEdmModel edmModel, OeEntryFactory?parentEntryFactory, OePropertyAccessor[] skipTokenAccessors)
        {
            OePropertyAccessor[] accessors = _allAccessors;
            if (IsTuple)
            {
                OePropertyAccessor[] propertyAccessors = OePropertyAccessor.CreateFromType(edmModel.GetClrType(EntitySet), EntitySet);
                accessors = new OePropertyAccessor[_allAccessors.Length];
                for (int i = 0; i < accessors.Length; i++)
                {
                    OePropertyAccessor accessor = Array.Find(propertyAccessors, pa => pa.EdmProperty == _allAccessors[i].EdmProperty);
                    if (Array.IndexOf(Accessors, _allAccessors[i]) == -1)
                    {
                        var convertExpression   = (UnaryExpression)accessor.PropertyExpression.Expression;
                        var parameterExpression = (ParameterExpression)convertExpression.Operand;
                        accessor = OePropertyAccessor.CreatePropertyAccessor(accessor.EdmProperty, accessor.PropertyExpression, parameterExpression, true);
                    }
                    accessors[i] = accessor;
                }
            }

            var navigationLinks = new OeEntryFactory[NavigationLinks.Count];

            for (int i = 0; i < NavigationLinks.Count; i++)
            {
                navigationLinks[i] = NavigationLinks[i].CreateEntryFactoryFromTuple(edmModel, this, Array.Empty <OePropertyAccessor>());
            }

            OeEntryFactoryOptions options;

            if (parentEntryFactory == null)
            {
                options = new OeEntryFactoryOptions()
                {
                    Accessors          = accessors,
                    EntitySet          = EntitySet,
                    NavigationLinks    = navigationLinks,
                    SkipTokenAccessors = skipTokenAccessors,
                };
                return(new OeEntryFactory(ref options));
            }

            ParameterExpression parameter      = Expression.Parameter(typeof(Object));
            UnaryExpression     typedParameter = Expression.Convert(parameter, edmModel.GetClrType(parentEntryFactory.EntitySet));
            MemberExpression    navigationPropertyExpression = Expression.Property(typedParameter, EdmNavigationProperty.Name);
            LambdaExpression    linkAccessor = Expression.Lambda(navigationPropertyExpression, parameter);

            options = new OeEntryFactoryOptions()
            {
                Accessors             = accessors,
                EdmNavigationProperty = EdmNavigationProperty,
                EntitySet             = EntitySet,
                LinkAccessor          = linkAccessor,
                NavigationLinks       = navigationLinks,
                NavigationSelectItem  = NavigationSelectItem,
                SkipTokenAccessors    = skipTokenAccessors,
            };
            return(new OeEntryFactory(ref options));
        }
Example #7
0
        public ODataResource CreateEntry(Object entity)
        {
            var odataProperties = new ODataProperty[Accessors.Length];

            for (int i = 0; i < Accessors.Length; i++)
            {
                OePropertyAccessor accessor = Accessors[i];
                Object             value    = accessor.Accessor(entity);

                ODataValue odataValue = null;
                if (value == null)
                {
                    odataValue = new ODataNullValue();
                }
                else if (value.GetType().IsEnum)
                {
                    odataValue = new ODataEnumValue(value.ToString());
                }
                else if (value is DateTime dateTime)
                {
                    switch (dateTime.Kind)
                    {
                    case DateTimeKind.Unspecified:
                        value = new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
                        break;

                    case DateTimeKind.Utc:
                        value = new DateTimeOffset(dateTime);
                        break;

                    case DateTimeKind.Local:
                        value = new DateTimeOffset(dateTime.ToUniversalTime());
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("unknown DateTimeKind " + dateTime.Kind.ToString());
                    }
                    odataValue = new ODataPrimitiveValue(value);
                }
                else
                {
                    odataValue = new ODataPrimitiveValue(value);
                }

                odataValue.TypeAnnotation = accessor.TypeAnnotation;
                odataProperties[i]        = new ODataProperty()
                {
                    Name = accessor.Name, Value = odataValue
                };
            }

            return(new ODataResource
            {
                TypeName = _typeName,
                Properties = odataProperties
            });
        }
Example #8
0
 public OeQueryContext CreateQueryContext(ODataUri odataUri, IEdmEntitySet entitySet, OeMetadataLevel metadataLevel)
 {
     OePropertyAccessor[]  accessors        = OePropertyAccessor.CreateFromType(_edmModel.GetClrType(entitySet), entitySet);
     Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(entitySet);
     return(new OeQueryContext(_edmModel, odataUri, entitySetAdapter)
     {
         EntryFactory = new OeEntryFactory(entitySet, accessors, null),
         MetadataLevel = metadataLevel
     });
 }
        public OeEntryFactory CreateEntryFactory(IEdmEntitySet entitySet)
        {
            if (_entryFactoryFactory != null)
            {
                return(_entryFactoryFactory(entitySet, ParameterType));
            }

            OePropertyAccessor[] accessors = OePropertyAccessor.CreateFromType(ParameterType, entitySet);
            return(OeEntryFactory.CreateEntryFactory(entitySet, accessors));
        }
Example #10
0
 public OeEntryFactory CreateEntryFactory(Type entityType, IEdmEntitySet entitySet, Type sourceType)
 {
     OePropertyAccessor[] accessors;
     if (_aggProperties.Count == 0)
     {
         accessors = OePropertyAccessor.CreateFromType(entityType, entitySet);
     }
     else
     {
         accessors = OePropertyAccessor.CreateFromTuple(sourceType, _aggProperties, 0);
     }
     return(OeEntryFactory.CreateEntryFactory(entitySet, accessors));
 }
Example #11
0
 public OeEntryFactory CreateEntryFactory(Type entityType, IEdmEntitySet entitySet, Type sourceType)
 {
     OePropertyAccessor[] accessors;
     if (_aggProperties.Count == 0)
     {
         accessors = OePropertyAccessor.CreateFromType(entityType, entitySet);
     }
     else
     {
         int groupIndex = _aggProperties.FindIndex(a => a.IsGroup);
         accessors = OePropertyAccessor.CreateFromTuple(sourceType, _aggProperties, groupIndex);
     }
     return(OeEntryFactory.CreateEntryFactory(entitySet, accessors));
 }
Example #12
0
        public OeQueryContext CreateQueryContext(ODataUri odataUri, OeMetadataLevel metadataLevel)
        {
            var importSegment = (OperationImportSegment)odataUri.Path.FirstSegment;
            IEdmEntitySet entitySet = OeOperationHelper.GetEntitySet(importSegment.OperationImports.Single());
            if (entitySet == null)
                return null;

            Type clrType = _edmModel.GetClrType(entitySet.EntityType());
            OePropertyAccessor[] accessors = OePropertyAccessor.CreateFromType(clrType, entitySet);

            Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(entitySet);
            return new OeQueryContext(_edmModel, odataUri, null, false, 0, false, metadataLevel, entitySetAdapter)
            {
                EntryFactory = OeEntryFactory.CreateEntryFactory(entitySet, accessors),
            };
        }
Example #13
0
        public static OePropertyAccessor[] GetAccessors(Expression source, OrderByClause orderByClause, Translators.OeJoinBuilder joinBuilder)
        {
            var accessors = new List <OePropertyAccessor>();

            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            UnaryExpression     instance  = Expression.Convert(parameter, OeExpressionHelper.GetCollectionItemType(source.Type));

            while (orderByClause != null)
            {
                MemberExpression       propertyExpression = Translators.OeOrderByTranslator.GetPropertyExpression(joinBuilder, source, instance, orderByClause.Expression);
                IEdmStructuralProperty edmProperty        = GetEdmProperty(orderByClause.Expression, propertyExpression.Type);
                accessors.Add(OePropertyAccessor.CreatePropertyAccessor(edmProperty, propertyExpression, parameter, true));
                orderByClause = orderByClause.ThenBy;
            }

            return(accessors.ToArray());
        }
Example #14
0
        public static OePropertyAccessor[] GetAccessors(Expression source, OrderByClause orderByClause, Translators.OeJoinBuilder joinBuilder)
        {
            var accessors = new List <OePropertyAccessor>();

            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            UnaryExpression     instance  = Expression.Convert(parameter, OeExpressionHelper.GetCollectionItemType(source.Type));

            while (orderByClause != null)
            {
                var propertyNode = (SingleValuePropertyAccessNode)orderByClause.Expression;
                MemberExpression propertyExpression = joinBuilder.GetJoinPropertyExpression(source, instance, propertyNode);
                accessors.Add(OePropertyAccessor.CreatePropertyAccessor(propertyNode.Property, propertyExpression, parameter, true));
                orderByClause = orderByClause.ThenBy;
            }

            return(accessors.ToArray());
        }
Example #15
0
        public static OePropertyAccessor[] GetAccessors(Expression source, OrderByClause orderByClause, Translators.OeJoinBuilder joinBuilder)
        {
            var accessors = new List <OePropertyAccessor>();

            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            UnaryExpression     instance  = Expression.Convert(parameter, OeExpressionHelper.GetCollectionItemType(source.Type));

            while (orderByClause != null)
            {
                MemberExpression       propertyExpression = Translators.OeOrderByTranslator.GetPropertyExpression(joinBuilder, source, instance, orderByClause.Expression);
                IEdmStructuralProperty edmProperty        = GetEdmProperty(orderByClause.Expression, propertyExpression.Type);
                OePropertyAccessor     accessor;
                if (typeof(OeDynamicType).IsAssignableFrom(propertyExpression.Expression !.Type))
                {
                    PropertyInfo         indexProperty = typeof(OeDynamicType).GetProperty("Item") !;
                    MethodCallExpression expression    = Expression.Call(propertyExpression.Expression !, indexProperty.GetGetMethod() !, Expression.Constant(edmProperty.Name));
                    accessor = OePropertyAccessor.CreatePropertyAccessor(edmProperty, expression, parameter, true);
                }
Example #16
0
        public OeQueryContext CreateQueryContext(ODataUri odataUri, OeMetadataLevel metadataLevel)
        {
            var           importSegment = (OperationImportSegment)odataUri.Path.FirstSegment;
            IEdmEntitySet entitySet     = OeOperationHelper.GetEntitySet(importSegment.OperationImports.Single());

            if (entitySet == null)
            {
                return(null);
            }

            OePropertyAccessor[]  accessors        = OePropertyAccessor.CreateFromType(_edmModel.GetClrType(entitySet), entitySet);
            Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(entitySet);
            return(new OeQueryContext(_edmModel, odataUri, entitySetAdapter)
            {
                EntryFactory = new OeEntryFactory(entitySet, accessors, Array.Empty <OePropertyAccessor>()),
                MetadataLevel = metadataLevel
            });
        }
Example #17
0
        private OePropertyAccessor[] GetSkipTokenAccessors(IEdmModel edmModel, OrderByClause?orderByClause)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            Type clrEntityType            = edmModel.GetClrType(EntitySet);
            var  visitor = new OeQueryNodeVisitor(Expression.Parameter(clrEntityType));

            var skipTokenAccessors = new List <OePropertyAccessor>();

            while (orderByClause != null)
            {
                var propertyNode       = (SingleValuePropertyAccessNode)orderByClause.Expression;
                var propertyExpression = (MemberExpression)visitor.Visit(propertyNode);
                propertyExpression = OeExpressionHelper.ReplaceParameter(propertyExpression, parameter);
                skipTokenAccessors.Add(OePropertyAccessor.CreatePropertyAccessor(propertyNode.Property, propertyExpression, parameter, true));

                orderByClause = orderByClause.ThenBy;
            }

            return(skipTokenAccessors.ToArray());
        }
Example #18
0
        private OeEntryFactory CreateEntryFactory(OeExpressionBuilder expressionBuilder)
        {
            IEdmEntitySet entitySet;

            if (ODataUri.Path.LastSegment is OperationSegment)
            {
                entitySet = OeOperationHelper.GetEntitySet(ODataUri.Path);
                Type clrEntityType             = EdmModel.GetClrType(entitySet.EntityType());
                OePropertyAccessor[] accessors = OePropertyAccessor.CreateFromType(clrEntityType, entitySet);
                return(OeEntryFactory.CreateEntryFactory(entitySet, accessors));
            }

            entitySet = OeParseNavigationSegment.GetEntitySet(ParseNavigationSegments);
            if (entitySet == null)
            {
                entitySet = OeEdmClrHelper.GetEntitySet(EdmModel, EntitySetAdapter.EntitySetName);
            }

            return(expressionBuilder.CreateEntryFactory(entitySet));
        }
Example #19
0
 protected OePropertyAccessor[] GetAccessorsFromTuple(IEdmModel edmModel)
 {
     OePropertyAccessor[] accessors = _allAccessors;
     if (IsTuple)
     {
         OePropertyAccessor[] propertyAccessors = OePropertyAccessor.CreateFromType(edmModel.GetClrType(EntitySet), EntitySet);
         accessors = new OePropertyAccessor[_allAccessors.Length];
         for (int i = 0; i < accessors.Length; i++)
         {
             OePropertyAccessor accessor = Array.Find(propertyAccessors, pa => pa.EdmProperty == _allAccessors[i].EdmProperty);
             if (Array.IndexOf(Accessors, _allAccessors[i]) == -1)
             {
                 var convertExpression   = (UnaryExpression)accessor.PropertyExpression.Expression;
                 var parameterExpression = (ParameterExpression)convertExpression.Operand;
                 accessor = OePropertyAccessor.CreatePropertyAccessor(accessor.EdmProperty, accessor.PropertyExpression, parameterExpression, true);
             }
             accessors[i] = accessor;
         }
     }
     return(accessors);
 }
Example #20
0
        public ODataResource CreateEntry(Object entity)
        {
            for (int i = 0; i < _accessors.Length; i++)
            {
                OePropertyAccessor accessor = _accessors[i];
                Object             value    = accessor.Accessor(entity);
                if (value is DateTime)
                {
                    value = (DateTimeOffset)(DateTime)value;
                }
                ODataValue odataValue = null;
                if (value == null)
                {
                    odataValue = new ODataNullValue()
                    {
                        TypeAnnotation = accessor.TypeAnnotation
                    }
                }
                ;
                else
                {
                    if (value.GetType().GetTypeInfo().IsEnum)
                    {
                        odataValue = new ODataEnumValue(value.ToString());
                    }
                    else
                    {
                        odataValue = new ODataPrimitiveValue(value);
                    }
                    odataValue.TypeAnnotation = accessor.TypeAnnotation;
                }
                _odataProperties[i].Value = odataValue;
            }

            return(new ODataResource
            {
                TypeName = _typeName,
                Properties = _odataProperties
            });
        }
Example #21
0
        private OeEntryFactory CreateEntryFactoryFromTuple(OeEntryFactory parentEntryFactory)
        {
            OePropertyAccessor[] accessors = _allAccessors;
            if (IsTuple)
            {
                accessors = new OePropertyAccessor[_allAccessors.Length];
                OePropertyAccessor[] propertyAccessors = OePropertyAccessor.CreateFromType(ClrEntityType, EntitySet);
                for (int i = 0; i < accessors.Length; i++)
                {
                    OePropertyAccessor accessor = Array.Find(propertyAccessors, pa => pa.EdmProperty == _allAccessors[i].EdmProperty);
                    if (Array.IndexOf(Accessors, _allAccessors[i]) == -1)
                    {
                        var convertExpression   = (UnaryExpression)accessor.PropertyExpression.Expression;
                        var parameterExpression = (ParameterExpression)convertExpression.Operand;
                        accessor = OePropertyAccessor.CreatePropertyAccessor(accessor.EdmProperty, accessor.PropertyExpression, parameterExpression, true);
                    }
                    accessors[i] = accessor;
                }
            }

            var navigationLinks = new OeEntryFactory[NavigationLinks.Count];

            for (int i = 0; i < NavigationLinks.Count; i++)
            {
                navigationLinks[i] = NavigationLinks[i].CreateEntryFactoryFromTuple(this);
            }

            if (parentEntryFactory == null)
            {
                return(OeEntryFactory.CreateEntryFactoryParent(ClrEntityType, EntitySet, accessors, navigationLinks, null));
            }

            ParameterExpression parameter      = Expression.Parameter(typeof(Object));
            UnaryExpression     typedParameter = Expression.Convert(parameter, parentEntryFactory.ClrEntityType);
            MemberExpression    navigationPropertyExpression = Expression.Property(typedParameter, ResourceInfo.Name);
            LambdaExpression    linkAccessor = Expression.Lambda(navigationPropertyExpression, parameter);

            return(OeEntryFactory.CreateEntryFactoryNested(ClrEntityType, EntitySet, accessors, navigationLinks, linkAccessor, ResourceInfo));
        }
Example #22
0
        public static OePropertyAccessor[] GetAccessors(Expression source, OrderByClause orderByClause)
        {
            var accessors = new List <OePropertyAccessor>();

            var  tupleProperty            = new OePropertyTranslator(source);
            Type itemType                 = OeExpressionHelper.GetCollectionItemType(source.Type);
            ParameterExpression parameter = Expression.Parameter(typeof(Object));
            UnaryExpression     instance  = Expression.Convert(parameter, itemType);

            while (orderByClause != null)
            {
                var propertyNode = (SingleValuePropertyAccessNode)orderByClause.Expression;
                MemberExpression propertyExpression = tupleProperty.Build(instance, propertyNode.Property);
                if (propertyExpression == null)
                {
                    throw new InvalidOperationException("order by property " + propertyNode.Property.Name + "not found");
                }

                accessors.Add(OePropertyAccessor.CreatePropertyAccessor(propertyNode.Property, propertyExpression, parameter));
                orderByClause = orderByClause.ThenBy;
            }

            return(accessors.ToArray());
        }