public void GetEnumerator_returns_rows_from_the_current_result_set()
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(
                new[] { new object[] { 1, "a" }, new object[] { 2, "b" } },
                new[] { new object[] { 3, "c" } });

            var bufferedReader = new BufferedDataReader(reader);

            bufferedReader.Initialize("2008", FakeSqlProviderServices.Instance);

            var enumerator = bufferedReader.GetEnumerator();

            var list = new List <object>();

            do
            {
                while (enumerator.MoveNext())
                {
                    var dataRecord = (DbDataRecord)enumerator.Current;
                    for (var i = 0; i < dataRecord.FieldCount; i++)
                    {
                        list.Add(dataRecord.GetValue(i));
                    }
                }
            }while (bufferedReader.NextResult());

            Assert.Equal(new object[] { 1, "a", 2, "b", 3, "c" }, list);
        }
        public void Metadata_methods_throw_if_reader_is_closed()
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(new[] { new[] { new object() } });

            var bufferedDataReader = new BufferedDataReader(reader);

            bufferedDataReader.Initialize("2008", FakeSqlProviderServices.Instance);

            bufferedDataReader.Close();
            Assert.Equal(
                Strings.ADP_ClosedDataReaderError,
                Assert.Throws <InvalidOperationException>(() => bufferedDataReader.FieldCount).Message);
            Assert.Equal(
                Strings.ADP_ClosedDataReaderError,
                Assert.Throws <InvalidOperationException>(() => bufferedDataReader.GetOrdinal("columnName")).Message);
            Assert.Equal(
                Strings.ADP_ClosedDataReaderError,
                Assert.Throws <InvalidOperationException>(() => bufferedDataReader.GetDataTypeName(0)).Message);
            Assert.Equal(
                Strings.ADP_ClosedDataReaderError,
                Assert.Throws <InvalidOperationException>(() => bufferedDataReader.GetFieldType(0)).Message);
            Assert.Equal(
                Strings.ADP_ClosedDataReaderError,
                Assert.Throws <InvalidOperationException>(() => bufferedDataReader.GetName(0)).Message);
        }
        private void Metadata_methods_return_expected_results(bool async)
        {
            var reader     = Common.Internal.Materialization.MockHelper.CreateDbDataReader(new[] { new[] { new object() } });
            var readerMock = Mock.Get(reader);

            readerMock.Setup(m => m.RecordsAffected).Returns(2);
            readerMock.Setup(m => m.GetOrdinal(It.IsAny <string>())).Returns(3);
            readerMock.Setup(m => m.GetDataTypeName(It.IsAny <int>())).Returns("dataTypeName");
            readerMock.Setup(m => m.GetFieldType(It.IsAny <int>())).Returns(typeof(DBNull));
            readerMock.Setup(m => m.GetName(It.IsAny <int>())).Returns("columnName");

            var bufferedDataReader = new BufferedDataReader(reader);

            if (async)
            {
#if !NET40
                bufferedDataReader.InitializeAsync("2008", FakeSqlProviderServices.Instance, CancellationToken.None).Wait();
#endif
            }
            else
            {
                bufferedDataReader.Initialize("2008", FakeSqlProviderServices.Instance);
            }

            Assert.Equal(1, bufferedDataReader.FieldCount);
            Assert.Equal(0, bufferedDataReader.GetOrdinal("columnName"));
            Assert.Equal("dataTypeName", bufferedDataReader.GetDataTypeName(0));
            Assert.Equal(typeof(DBNull), bufferedDataReader.GetFieldType(0));
            Assert.Equal("columnName", bufferedDataReader.GetName(0));
            Assert.Throws <NotSupportedException>(() => bufferedDataReader.Depth);
            Assert.Throws <NotSupportedException>(() => bufferedDataReader.GetSchemaTable());

            bufferedDataReader.Close();
            Assert.Equal(2, bufferedDataReader.RecordsAffected);
        }
示例#4
0
        public void ReadAsync_throws_OperationCanceledException_if_task_is_cancelled()
        {
            var reader = new BufferedDataReader(new Mock <DbDataReader>().Object);

            Assert.Throws <OperationCanceledException>(
                () => reader.ReadAsync(new CancellationToken(canceled: true))
                .GetAwaiter().GetResult());
        }
        public void Close_disposes_underlying_reader()
        {
            var reader         = Common.Internal.Materialization.MockHelper.CreateDbDataReader(new[] { new[] { new object() } });
            var bufferedReader = new BufferedDataReader(reader);

            Assert.False(reader.IsClosed);
            bufferedReader.Close();
            Assert.True(reader.IsClosed);
        }
示例#6
0
        public void InitializeAsync_throws_OperationCanceledException_if_task_is_cancelled()
        {
            var reader = new BufferedDataReader(new Mock <DbDataReader>().Object);

            Assert.Throws <OperationCanceledException>(
                () => reader.InitializeAsync("manifestToken", new Mock <DbProviderServices>().Object,
                                             new Type[0], new bool[0], new CancellationToken(canceled: true))
                .GetAwaiter().GetResult());
        }
示例#7
0
        public void InitializeAsync_does_not_throw_OperationCanceledException_if_reader_is_null_even_if_task_is_cancelled()
        {
            var reader = new BufferedDataReader(new Mock <DbDataReader>().Object);

            reader.Close();

            Assert.False(reader.InitializeAsync("manifestToken", new Mock <DbProviderServices>().Object,
                                                new Type[0], new bool[0], new CancellationToken(canceled: true)).IsCanceled);
        }
        public void InitializeAsync_is_idempotent()
        {
            var reader         = Common.Internal.Materialization.MockHelper.CreateDbDataReader(new[] { new[] { new object() } });
            var bufferedReader = new BufferedDataReader(reader);

            Assert.False(reader.IsClosed);
            bufferedReader.InitializeAsync("2008", FakeSqlProviderServices.Instance, CancellationToken.None).Wait();
            Assert.True(reader.IsClosed);
            bufferedReader.InitializeAsync("2008", FakeSqlProviderServices.Instance, CancellationToken.None).Wait();
        }
        private void Verify_method_throws_when_no_data <T>(
            Func <BufferedDataReader, T> method, params IEnumerable <object[]>[] dataReaderContents)
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(dataReaderContents);

            var bufferedReader = new BufferedDataReader(reader);

            bufferedReader.Initialize("2008", FakeSqlProviderServices.Instance);

            Assert.Equal(
                Strings.ADP_NoData,
                Assert.Throws <InvalidOperationException>(() => method(bufferedReader)).Message);
        }
示例#10
0
        internal virtual async Task <ObjectResult <TResultType> > ExecuteAsync <TResultType>(
            ObjectContext context,
            ObjectParameterCollection parameterValues,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            DbDataReader               storeReader    = (DbDataReader)null;
            BufferedDataReader         bufferedReader = (BufferedDataReader)null;
            ObjectResult <TResultType> objectResult;

            try
            {
                using (EntityCommand entityCommand = this.PrepareEntityCommand(context, parameterValues))
                    storeReader = await entityCommand.GetCommandDefinition().ExecuteStoreCommandsAsync(entityCommand, this.Streaming ? CommandBehavior.Default : CommandBehavior.SequentialAccess, cancellationToken).WithCurrentCulture <DbDataReader>();
                ShaperFactory <TResultType> shaperFactory = (ShaperFactory <TResultType>) this.ResultShaperFactory;
                Shaper <TResultType>        shaper;
                if (this.Streaming)
                {
                    shaper = shaperFactory.Create(storeReader, context, context.MetadataWorkspace, this.MergeOption, true, this.Streaming);
                }
                else
                {
                    StoreItemCollection storeItemCollection = (StoreItemCollection)context.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                    DbProviderServices  providerServices    = DbConfiguration.DependencyResolver.GetService <DbProviderServices>((object)storeItemCollection.ProviderInvariantName);
                    bufferedReader = new BufferedDataReader(storeReader);
                    await bufferedReader.InitializeAsync(storeItemCollection.ProviderManifestToken, providerServices, shaperFactory.ColumnTypes, shaperFactory.NullableColumns, cancellationToken).WithCurrentCulture();

                    shaper = shaperFactory.Create((DbDataReader)bufferedReader, context, context.MetadataWorkspace, this.MergeOption, true, this.Streaming);
                }
                TypeUsage resultItemEdmType = this.ResultType.EdmType.BuiltInTypeKind != BuiltInTypeKind.CollectionType ? this.ResultType : ((CollectionType)this.ResultType.EdmType).TypeUsage;
                objectResult = new ObjectResult <TResultType>(shaper, this._singleEntitySet, resultItemEdmType);
            }
            catch (Exception ex)
            {
                if (this.Streaming && storeReader != null)
                {
                    storeReader.Dispose();
                }
                if (!this.Streaming && bufferedReader != null)
                {
                    bufferedReader.Dispose();
                }
                throw;
            }
            return(objectResult);
        }
示例#11
0
        internal virtual ObjectResult <TResultType> Execute <TResultType>(
            ObjectContext context,
            ObjectParameterCollection parameterValues)
        {
            DbDataReader       reader             = (DbDataReader)null;
            BufferedDataReader bufferedDataReader = (BufferedDataReader)null;

            try
            {
                using (EntityCommand entityCommand = this.PrepareEntityCommand(context, parameterValues))
                    reader = entityCommand.GetCommandDefinition().ExecuteStoreCommands(entityCommand, this.Streaming ? CommandBehavior.Default : CommandBehavior.SequentialAccess);
                ShaperFactory <TResultType> resultShaperFactory = (ShaperFactory <TResultType>) this.ResultShaperFactory;
                Shaper <TResultType>        shaper;
                if (this.Streaming)
                {
                    shaper = resultShaperFactory.Create(reader, context, context.MetadataWorkspace, this.MergeOption, true, this.Streaming);
                }
                else
                {
                    StoreItemCollection itemCollection = (StoreItemCollection)context.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                    DbProviderServices  service        = DbConfiguration.DependencyResolver.GetService <DbProviderServices>((object)itemCollection.ProviderInvariantName);
                    bufferedDataReader = new BufferedDataReader(reader);
                    bufferedDataReader.Initialize(itemCollection.ProviderManifestToken, service, resultShaperFactory.ColumnTypes, resultShaperFactory.NullableColumns);
                    shaper = resultShaperFactory.Create((DbDataReader)bufferedDataReader, context, context.MetadataWorkspace, this.MergeOption, true, this.Streaming);
                }
                TypeUsage resultItemType = this.ResultType.EdmType.BuiltInTypeKind != BuiltInTypeKind.CollectionType ? this.ResultType : ((CollectionType)this.ResultType.EdmType).TypeUsage;
                return(new ObjectResult <TResultType>(shaper, this._singleEntitySet, resultItemType));
            }
            catch (Exception ex)
            {
                if (this.Streaming && reader != null)
                {
                    reader.Dispose();
                }
                if (!this.Streaming && bufferedDataReader != null)
                {
                    bufferedDataReader.Dispose();
                }
                throw;
            }
        }
        private void Verify_method_result <T>(
            Func <BufferedDataReader, T> method, bool async, T expectedResult, params IEnumerable <object[]>[] dataReaderContents)
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(dataReaderContents);

            var bufferedReader = new BufferedDataReader(reader);

            if (async)
            {
#if !NET40
                bufferedReader.InitializeAsync("2008", FakeSqlProviderServices.Instance, CancellationToken.None).Wait();
                Assert.True(bufferedReader.ReadAsync().Result);
#endif
            }
            else
            {
                bufferedReader.Initialize("2008", FakeSqlProviderServices.Instance);
                Assert.True(bufferedReader.Read());
            }

            Assert.Equal(expectedResult, method(bufferedReader));
        }
示例#13
0
        internal virtual ObjectResult <TResultType> Execute <TResultType>(ObjectContext context, ObjectParameterCollection parameterValues)
        {
            DbDataReader       storeReader    = null;
            BufferedDataReader bufferedReader = null;

            try
            {
                using (var entityCommand = PrepareEntityCommand(context, parameterValues))
                {
                    // acquire store reader
                    storeReader = entityCommand.GetCommandDefinition().ExecuteStoreCommands(
                        entityCommand,
                        Streaming
                            ? CommandBehavior.Default
                            : CommandBehavior.SequentialAccess);
                }

                var shaperFactory = (ShaperFactory <TResultType>)ResultShaperFactory;
                Shaper <TResultType> shaper;
                if (Streaming)
                {
                    shaper = shaperFactory.Create(
                        storeReader, context, context.MetadataWorkspace, MergeOption, true, useSpatialReader: true,
                        shouldReleaseConnection: Streaming);
                }
                else
                {
                    var storeItemCollection = (StoreItemCollection)context.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                    var providerServices    = DbConfiguration.DependencyResolver.GetService <DbProviderServices>(storeItemCollection.StoreProviderInvariantName);

                    bufferedReader = new BufferedDataReader(storeReader);
                    bufferedReader.Initialize(storeItemCollection.StoreProviderManifestToken, providerServices);

                    shaper = shaperFactory.Create(
                        bufferedReader, context, context.MetadataWorkspace, MergeOption, true, useSpatialReader: false,
                        shouldReleaseConnection: Streaming);
                }

                // create materializer delegate
                TypeUsage resultItemEdmType;
                if (ResultType.EdmType.BuiltInTypeKind == BuiltInTypeKind.CollectionType)
                {
                    resultItemEdmType = ((CollectionType)ResultType.EdmType).TypeUsage;
                }
                else
                {
                    resultItemEdmType = ResultType;
                }

                return(new ObjectResult <TResultType>(shaper, _singleEntitySet, resultItemEdmType));
            }
            catch (Exception)
            {
                // Note: The ObjectResult is responsible for disposing the reader if creating
                // the enumerator fails.
                if (Streaming && storeReader != null)
                {
                    storeReader.Dispose();
                }

                if (!Streaming &&
                    bufferedReader != null)
                {
                    bufferedReader.Dispose();
                }
                throw;
            }
        }
        private void Manipulation_methods_perform_expected_actions(bool spatial, bool async)
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(
                new[] { new object[] { 1, "a" } }, new object[0][]);

            var bufferedDataReader = new BufferedDataReader(reader);


            var spatialDataReaderMock = new Mock <DbSpatialDataReader>();
            var providerServicesMock  = new Mock <DbProviderServices>();

            if (spatial)
            {
                spatialDataReaderMock.Setup(m => m.IsGeographyColumn(0)).Returns(true);
                spatialDataReaderMock.Setup(m => m.IsGeometryColumn(1)).Returns(true);
#if !NET40
                if (async)
                {
                    spatialDataReaderMock.Setup(m => m.GetGeographyAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()))
                    .Returns(() => Task.FromResult((DbGeography)null));
                    spatialDataReaderMock.Setup(m => m.GetGeometryAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()))
                    .Returns(() => Task.FromResult((DbGeometry)null));
                }
#endif
                providerServicesMock.Protected()
                .Setup <DbSpatialDataReader>("GetDbSpatialDataReader", reader, "2008")
                .Returns(spatialDataReaderMock.Object);
            }
            try
            {
                Assert.False(bufferedDataReader.IsClosed);
                if (async)
                {
#if !NET40
                    bufferedDataReader.InitializeAsync("2008", providerServicesMock.Object, CancellationToken.None).Wait();
#endif
                }
                else
                {
                    bufferedDataReader.Initialize("2008", providerServicesMock.Object);
                }
                Assert.False(bufferedDataReader.IsClosed);
            }
            finally
            {
                MutableResolver.ClearResolvers();
            }

            if (spatial)
            {
                if (async)
                {
#if !NET40
                    spatialDataReaderMock.Verify(m => m.GetGeographyAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once());
                    spatialDataReaderMock.Verify(m => m.GetGeometryAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once());
#endif
                }
                else
                {
                    spatialDataReaderMock.Verify(m => m.GetGeography(It.IsAny <int>()), Times.Once());
                    spatialDataReaderMock.Verify(m => m.GetGeometry(It.IsAny <int>()), Times.Once());
                }
            }
            Assert.True(bufferedDataReader.HasRows);

            if (async)
            {
#if !NET40
                Assert.True(bufferedDataReader.ReadAsync().Result);
                Assert.False(bufferedDataReader.ReadAsync().Result);
#endif
            }
            else
            {
                Assert.True(bufferedDataReader.Read());
                Assert.False(bufferedDataReader.Read());
            }

            Assert.True(bufferedDataReader.HasRows);

            if (async)
            {
#if !NET40
                Assert.True(bufferedDataReader.NextResultAsync().Result);
#endif
            }
            else
            {
                Assert.True(bufferedDataReader.NextResult());
            }

            Assert.False(bufferedDataReader.HasRows);

            if (async)
            {
#if !NET40
                Assert.False(bufferedDataReader.ReadAsync().Result);
                Assert.False(bufferedDataReader.NextResultAsync().Result);
#endif
            }
            else
            {
                Assert.False(bufferedDataReader.Read());
                Assert.False(bufferedDataReader.NextResult());
            }

            Assert.False(bufferedDataReader.IsClosed);
            bufferedDataReader.Close();
            Assert.True(bufferedDataReader.IsClosed);
        }