public void Initialize_is_idempotent()
        {
            var reader         = Common.Internal.Materialization.MockHelper.CreateDbDataReader(new[] { new[] { new object() } });
            var bufferedReader = new BufferedDataReader(reader);

            Assert.False(reader.IsClosed);
            bufferedReader.Initialize("2008", FakeSqlProviderServices.Instance);
            Assert.True(reader.IsClosed);
            bufferedReader.Initialize("2008", FakeSqlProviderServices.Instance);
        }
예제 #2
0
        private void Metadata_methods_return_expected_results(bool async, bool shaped)
        {
            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 columnTypes        = new[] { typeof(object) };
            var nullableColumns    = new[] { true };
            var bufferedDataReader = new BufferedDataReader(reader);

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

            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);
        }
        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);
        }
        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);
        }
        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);
        }
예제 #6
0
        private void Verify_method_result <T>(
            Func <BufferedDataReader, T> method, bool async, bool shaped, T expectedResult, params IEnumerable <object[]>[] dataReaderContents)
        {
            var reader = Common.Internal.Materialization.MockHelper.CreateDbDataReader(dataReaderContents);

            var columnTypes     = new[] { typeof(T) };
            var nullableColumns = new[] { true };
            var bufferedReader  = new BufferedDataReader(reader);

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

            Assert.Equal(expectedResult, method(bufferedReader));
        }
예제 #7
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;
            }
        }
예제 #8
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);
        }