Example #1
0
        public async Task ExecuteAsync(ODataUri odataUri, OeRequestHeaders headers, Stream stream, CancellationToken cancellationToken)
        {
            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();
                OeQueryContext queryContext = CreateQueryContext(odataUri, headers.MaxPageSize, headers.NavigationNextLink, headers.MetadataLevel);
                if (queryContext.IsCountSegment)
                {
                    headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType;
                    int    count  = _dataAdapter.ExecuteScalar <int>(dataContext, queryContext);
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(count.ToString(CultureInfo.InvariantCulture));
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    using (Db.OeAsyncEnumerator asyncEnumerator = _dataAdapter.ExecuteEnumerator(dataContext, queryContext, cancellationToken))
                        await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, stream).ConfigureAwait(false);
                }
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }
        }
Example #2
0
        public async Task ExecuteAsync(ODataUri odataUri, OeRequestHeaders headers, Stream stream, CancellationToken cancellationToken)
        {
            OeParseUriContext parseUriContext = ParseUri(odataUri);

            parseUriContext.Headers          = headers;
            parseUriContext.EntitySetAdapter = _dataAdapter.GetEntitySetAdapter(parseUriContext.EntitySet.Name);

            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();

                if (parseUriContext.IsCountSegment)
                {
                    int    count  = _dataAdapter.ExecuteScalar <int>(dataContext, parseUriContext);
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(count.ToString());
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    using (Db.OeEntityAsyncEnumerator asyncEnumerator = _dataAdapter.ExecuteEnumerator(dataContext, parseUriContext, cancellationToken))
                        await Writers.OeGetWriter.SerializeAsync(BaseUri, parseUriContext, asyncEnumerator, stream).ConfigureAwait(false);
                }
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }
        }
Example #3
0
        public async Task ExecuteAsync(Uri requestUri, OeRequestHeaders headers, Stream stream, CancellationToken cancellationToken)
        {
            OeParseUriContext parseUriContext = ParseUri(requestUri);

            parseUriContext.Headers          = headers;
            parseUriContext.EntitySetAdapter = _dataAdapter.GetEntitySetAdapter(parseUriContext.EntitySet.Name);

            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();

                if (parseUriContext.IsCountSegment)
                {
                    int    count  = _dataAdapter.ExecuteScalar <int>(parseUriContext, dataContext);
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(count.ToString());
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    long?count = null;
                    if (parseUriContext.ODataUri.QueryCount.GetValueOrDefault())
                    {
                        var countParseUriContext = new OeParseUriContext(parseUriContext.EdmModel
                                                                         , parseUriContext.ODataUri
                                                                         , parseUriContext.EntitySet
                                                                         , parseUriContext.ParseNavigationSegments
                                                                         , true);
                        countParseUriContext.Headers          = parseUriContext.Headers;
                        countParseUriContext.EntitySetAdapter = parseUriContext.EntitySetAdapter;
                        count = _dataAdapter.ExecuteScalar <int>(countParseUriContext, dataContext);
                    }
                    using (Db.OeEntityAsyncEnumerator asyncEnumerator = _dataAdapter.ExecuteEnumerator(parseUriContext, dataContext, cancellationToken))
                        await Writers.OeGetWriter.SerializeAsync(BaseUri, parseUriContext, asyncEnumerator, stream, count).ConfigureAwait(false);
                }
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }
        }
Example #4
0
        public async Task ExecuteAsync(ODataUri odataUri, Stream requestStream, OeRequestHeaders headers, Stream responseStream, CancellationToken cancellationToken)
        {
            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();
                using (Db.OeAsyncEnumerator asyncEnumerator = GetAsyncEnumerator(odataUri, requestStream, headers, dataContext, out Type returnClrType))
                {
                    if (returnClrType == null || returnClrType.IsPrimitive || returnClrType == typeof(String))
                    {
                        if (await asyncEnumerator.MoveNextAsync().ConfigureAwait(false) && asyncEnumerator.Current != null)
                        {
                            headers.ResponseContentType = OeRequestHeaders.TextDefault.ContentType;
                            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(asyncEnumerator.Current.ToString());
                            responseStream.Write(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            headers.ResponseContentType = null;
                        }
                    }
                    else
                    {
                        OeQueryContext queryContext = CreateQueryContext(odataUri, headers.MetadataLevel, returnClrType);
                        await Writers.OeGetWriter.SerializeAsync(queryContext, asyncEnumerator, headers.ContentType, responseStream).ConfigureAwait(false);
                    }
                }
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }
        }
Example #5
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;
        }