Ejemplo n.º 1
0
        private List <KeyValuePair <String, Object> > GetParameters(OperationImportSegment importSegment, Stream requestStream, String contentType)
        {
            var parameters = new List <KeyValuePair <String, Object> >();

            foreach (OperationSegmentParameter segmentParameter in importSegment.Parameters)
            {
                Object value;
                var    constantNode = segmentParameter.Value as ConstantNode;
                if (constantNode == null)
                {
                    value = OeEdmClrHelper.GetValue(_model, segmentParameter.Value);
                }
                else
                {
                    value = OeEdmClrHelper.GetValue(_model, constantNode.Value);
                }
                parameters.Add(new KeyValuePair <String, Object>(segmentParameter.Name, value));
            }

            var operation = (EdmOperation)importSegment.OperationImports.Single().Operation;

            if (parameters.Count == 0 && requestStream != null)
            {
                FillParameters(parameters, requestStream, operation, contentType);
            }
            OrderParameters(operation.Parameters, parameters);

            return(parameters);
        }
        public static Type GetClrType(this IEdmModel edmModel, IEdmEntitySetBase entitySet)
        {
            IEdmEntityType entityType = entitySet.EntityType();

            edmModel = OeEdmClrHelper.GetEdmModel(edmModel, entityType);
            return(edmModel.GetAnnotationValue <Type>(entityType));
        }
Ejemplo n.º 3
0
        public Db.OeAsyncEnumerator GetAsyncEnumerator(ODataUri odataUri, Stream requestStream, OeRequestHeaders headers, Object dataContext, out Type returnClrType)
        {
            var importSegment = (OperationImportSegment)odataUri.Path.LastSegment;

            List <KeyValuePair <String, Object> > parameters = GetParameters(importSegment, requestStream, headers.ContentType);
            IEdmOperationImport operationImport = importSegment.OperationImports.Single();

            returnClrType = null;
            if (operationImport.Operation.ReturnType != null)
            {
                IEdmTypeReference returnEdmTypeReference = operationImport.Operation.ReturnType;
                if (returnEdmTypeReference is IEdmCollectionTypeReference)
                {
                    returnEdmTypeReference = (returnEdmTypeReference.Definition as IEdmCollectionType).ElementType;
                }
                returnClrType = OeEdmClrHelper.GetClrType(_model, returnEdmTypeReference.Definition);
            }

            if (_model.IsDbFunction(operationImport.Operation))
            {
                return(_dataAdapter.OperationAdapter.ExecuteFunction(dataContext, operationImport.Name, parameters, returnClrType));
            }
            else
            {
                return(_dataAdapter.OperationAdapter.ExecuteProcedure(dataContext, operationImport.Name, parameters, returnClrType));
            }
        }
Ejemplo n.º 4
0
        private void FillParameters(List <KeyValuePair <String, Object> > parameters, Stream requestStream, IEdmOperation operation, String contentType)
        {
            if (!operation.Parameters.Any())
            {
                return;
            }

            IODataRequestMessage requestMessage = new OeInMemoryMessage(requestStream, contentType);
            var settings = new ODataMessageReaderSettings()
            {
                EnableMessageStreamDisposal = false
            };

            using (var messageReader = new ODataMessageReader(requestMessage, settings, _model))
            {
                ODataParameterReader parameterReader = messageReader.CreateODataParameterReader(operation);
                while (parameterReader.Read())
                {
                    Object value;
                    switch (parameterReader.State)
                    {
                    case ODataParameterReaderState.Value:
                    {
                        value = OeEdmClrHelper.GetValue(_model, parameterReader.Value);
                        break;
                    }

                    case ODataParameterReaderState.Collection:
                    {
                        ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader();
                        value = OeEdmClrHelper.GetValue(_model, ReadCollection(collectionReader));
                        break;
                    }

                    case ODataParameterReaderState.Resource:
                    {
                        ODataReader reader = parameterReader.CreateResourceReader();
                        value = OeEdmClrHelper.GetValue(_model, ReadResource(reader));
                        break;
                    }

                    case ODataParameterReaderState.ResourceSet:
                    {
                        ODataReader reader = parameterReader.CreateResourceSetReader();
                        value = OeEdmClrHelper.GetValue(_model, ReadResourceSet(reader));
                        break;
                    }

                    default:
                        continue;
                    }

                    parameters.Add(new KeyValuePair <String, Object>(parameterReader.Name, value));
                }
            }
        }
Ejemplo n.º 5
0
        public static IEdmEntitySetBase GetEntitySet(IEdmModel edmModel, ExpandedNavigationSelectItem item)
        {
            var entitySet = (IEdmEntitySetBase)item.NavigationSource;

            if (entitySet == null)
            {
                var segment = (NavigationPropertySegment)item.PathToNavigationProperty.LastSegment;
                entitySet = OeEdmClrHelper.GetEntitySet(edmModel, segment.NavigationProperty);
            }
            return(entitySet);
        }
Ejemplo n.º 6
0
        public static Type GetClrType(this IEdmModel edmModel, IEdmEntitySetBase entitySet)
        {
            IEdmEntityType entityType = entitySet.EntityType();
            IEdmModel?     model      = OeEdmClrHelper.GetEdmModel(edmModel, entityType);

            if (model == null)
            {
                throw new InvalidOperationException("Add type annotation for " + entityType.FullTypeName());
            }
            return(model.GetAnnotationValue <Type>(entityType));
        }
Ejemplo n.º 7
0
        private static FilterClause CreateFilterClause(IEdmEntitySet entitySet, IEnumerable <KeyValuePair <String, Object> > keys)
        {
            ResourceRangeVariableReferenceNode refNode = OeEdmClrHelper.CreateRangeVariableReferenceNode(entitySet);
            var entityType = (IEdmEntityType)refNode.RangeVariable.TypeReference.Definition;

            var propertyValues = new List <KeyValuePair <IEdmStructuralProperty, Object> >();

            foreach (KeyValuePair <String, Object> keyValue in keys)
            {
                var property = (IEdmStructuralProperty)entityType.FindProperty(keyValue.Key);
                propertyValues.Add(new KeyValuePair <IEdmStructuralProperty, Object>(property, keyValue.Value));
            }

            return(new FilterClause(CreateFilterExpression(refNode, propertyValues), refNode.RangeVariable));
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync(ODataUri odataUri, Stream requestStream, OeRequestHeaders headers, Stream responseStream, CancellationToken cancellationToken)
        {
            var importSegment = (OperationImportSegment)odataUri.Path.LastSegment;

            List <KeyValuePair <String, Object> > parameters = GetParameters(importSegment, requestStream, headers.ContentType);
            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();

                var operation = (EdmOperation)importSegment.OperationImports.Single().Operation;

                String procedureName;
                if (String.IsNullOrEmpty(operation.Namespace))
                {
                    procedureName = operation.Name;
                }
                else
                {
                    procedureName = operation.Namespace + "." + operation.Name;
                }

                Type returnClrType = null;
                if (operation.ReturnType != null)
                {
                    IEdmTypeReference returnEdmTypeReference = operation.ReturnType;
                    if (returnEdmTypeReference is IEdmCollectionTypeReference)
                    {
                        returnEdmTypeReference = (returnEdmTypeReference.Definition as IEdmCollectionType).ElementType;
                    }
                    returnClrType = OeEdmClrHelper.GetClrType(_model, returnEdmTypeReference.Definition);
                }

                using (Db.OeEntityAsyncEnumerator asyncEnumerator = _dataAdapter.ExecuteProcedure(dataContext, procedureName, parameters, returnClrType))
                {
                    if (returnClrType == null)
                    {
                        if (await asyncEnumerator.MoveNextAsync() && asyncEnumerator.Current != null)
                        {
                            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(asyncEnumerator.Current.ToString());
                            responseStream.Write(buffer, 0, buffer.Length);
                        }
                    }
                    else
                    {
                        String        entitySetName               = _dataAdapter.EntitySetMetaAdapters.FindByClrType(returnClrType).EntitySetName;
                        IEdmEntitySet entitySet                   = _model.FindDeclaredEntitySet(entitySetName);
                        Parsers.OePropertyAccessor[] accessors    = Parsers.OePropertyAccessor.CreateFromType(returnClrType, entitySet);
                        Parsers.OeEntryFactory       entryFactory = Parsers.OeEntryFactory.CreateEntryFactory(entitySet, accessors);

                        var parseUriContext = new Parsers.OeParseUriContext(_model, odataUri, entitySet, null, false)
                        {
                            EntryFactory = entryFactory,
                            Headers      = headers
                        };
                        await Writers.OeGetWriter.SerializeAsync(_baseUri, parseUriContext, asyncEnumerator, responseStream).ConfigureAwait(false);
                    }
                }
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }

            await Task.CompletedTask;
        }
Ejemplo n.º 9
0
 public override IEdmNavigationSource?ResolveNavigationSource(IEdmModel model, String identifier)
 {
     return(OeEdmClrHelper.GetEntitySetOrNull(model, identifier, EnableCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
 }
Ejemplo n.º 10
0
 public override IEdmNavigationSource ResolveNavigationSource(IEdmModel model, String identifier)
 {
     return(OeEdmClrHelper.GetEntitySet(model, identifier));
 }