internal RecordStateEnumerator(Shaper <T> .RowNestedResultEnumerator rowEnumerator)
 {
     this._rowEnumerator  = rowEnumerator;
     this._current        = (RecordState)null;
     this._depth          = -1;
     this._readerConsumed = false;
 }
            public bool MoveNext()
            {
                switch (this._state)
                {
                case Shaper <T> .ObjectQueryNestedEnumerator.State.Start:
                    if (this.TryReadToNextElement())
                    {
                        this.ReadElement();
                        break;
                    }
                    this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows;
                    break;

                case Shaper <T> .ObjectQueryNestedEnumerator.State.Reading:
                    this.ReadElement();
                    break;

                case Shaper <T> .ObjectQueryNestedEnumerator.State.NoRowsLastElementPending:
                    this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows;
                    break;
                }
                bool flag;

                if (this._state == Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows)
                {
                    this._previousElement = default(T);
                    flag = false;
                }
                else
                {
                    flag = true;
                }
                return(flag);
            }
        internal RecordState GatherData(Shaper shaper)
        {
            int num = this.RecordStateFactory.GatherData(shaper) ? 1 : 0;

            this._pendingIsNull = false;
            return(this);
        }
Пример #4
0
        internal override void ResetCollection(Shaper shaper)
        {
            // Check to see if anyone has registered for notification when the current coordinator
            // is reset.
            if (null != _handleClose)
            {
                _handleClose(shaper, _wrappedElements);
                _handleClose = null;
            }

            // Reset is entered for this collection.
            IsEntered = false;

            if (IsUsingElementCollection)
            {
                _elements        = TypedCoordinatorFactory.InitializeCollection(shaper);
                _wrappedElements = new List <IEntityWrapper>();
            }

            if (null != Child)
            {
                Child.ResetCollection(shaper);
            }
            if (null != Next)
            {
                Next.ResetCollection(shaper);
            }
        }
Пример #5
0
            private void Returns_ObjectQueryNestedEnumerator_for_nested_coordinatorFactories(
                Func <IDbEnumerator <object>, List <object> > toList)
            {
                var sourceEnumerable = new[]
                {
                    new object[] { 1, "A", null },
                    new object[] { 2, null, "X" },
                    new object[] { 3, "B", "Z" },
                    // Should stop reading at "B", since the coordinators are at the same depth
                    new object[] { 4, "C", null },
                    new object[] { 4, "D", null }                            // 4 shouldn't be added as it's repeated
                };

                var actualValuesFromNestedCoordinatorOne = new List <string>();
                var nestedCoordinatorFactoryOne          = Objects.MockHelper.CreateCoordinatorFactory <string, string>(
                    depth: 1,
                    stateSlot: 1,
                    ordinal: 1,
                    nestedCoordinators: new CoordinatorFactory[0],
                    producedValues: actualValuesFromNestedCoordinatorOne);

                var actualValuesFromNestedCoordinatorTwo = new List <string>();
                var nestedCoordinatorFactoryTwo          = Objects.MockHelper.CreateCoordinatorFactory <string, string>(
                    depth: 1,
                    stateSlot: 2,
                    ordinal: 2,
                    nestedCoordinators: new CoordinatorFactory[0],
                    producedValues: actualValuesFromNestedCoordinatorTwo);

                var actualValuesFromRootCoordinator = new List <object>();
                var rootCoordinatorFactory          = Objects.MockHelper.CreateCoordinatorFactory <int, object>(
                    depth: 0,
                    stateSlot: 0,
                    ordinal: 0,
                    nestedCoordinators: new[] { nestedCoordinatorFactoryOne, nestedCoordinatorFactoryTwo },
                    producedValues: actualValuesFromRootCoordinator);

                var shaper = new Shaper <object>(
                    MockHelper.CreateDbDataReader(sourceEnumerable),
                    /*context*/ null,
                    /*workspace*/ null,
                    MergeOption.AppendOnly,
                    /*stateCount*/ 3,
                    rootCoordinatorFactory,
                    /*readerOwned*/ false,
                    /*useSpatialReader*/ false,
                    shouldReleaseConnection: true);

                var actualEnumerator = shaper.GetEnumerator();

                Assert.Equal(new object[] { 1, 2, 3, 4 }.ToList(), toList(actualEnumerator));
                Assert.Equal(new object[] { 1, 2, 3, 4 }.ToList(), actualValuesFromRootCoordinator);
                Assert.Equal(new[] { "A", "B", "C", "D" }.ToList(), actualValuesFromNestedCoordinatorOne);
                Assert.Equal(new[] { "X" }.ToList(), actualValuesFromNestedCoordinatorTwo);
            }
        internal RecordState GetDefaultRecordState(Shaper <RecordState> shaper)
        {
            RecordState recordState = (RecordState)null;

            if (this.RecordStateFactories.Count > 0)
            {
                recordState = (RecordState)shaper.State[this.RecordStateFactories[0].StateSlotNumber];
                recordState.ResetToDefaultState();
            }
            return(recordState);
        }
        internal override void ReadNextElement(Shaper shaper)
        {
            T element;
            IEntityWrapper wrappedElement = null;

            try
            {
                if (TypedCoordinatorFactory.WrappedElement == null)
                {
                    element = TypedCoordinatorFactory.Element(shaper);
                }
                else
                {
                    wrappedElement = TypedCoordinatorFactory.WrappedElement(shaper);
                    // This cast may throw, in which case it will be immediately caught
                    // and the error handling expression will be used to get the appropriate error message.
                    element = (T)wrappedElement.Entity;
                }
            }
            catch (Exception e)
            {
                if (e.IsCatchableExceptionType() &&
                    !shaper.Reader.IsClosed)
                {
                    // Some errors can occur while a close handler is registered.  This clears
                    // out the handler so that ElementWithErrorHandling will report the correct
                    // error rather than asserting on the missing close handler.
                    ResetCollection(shaper);
                    // call a variation of the "Element" delegate with more detailed
                    // error handling (to produce a better exception message)
                    element = TypedCoordinatorFactory.ElementWithErrorHandling(shaper);
                }

                // rethrow
                throw;
            }

            // Z.EntityFramework.Classic: QueryResultFilter
            CurrentWrapper = wrappedElement;

            if (IsUsingElementCollection)
            {
                _elements.Add(element);
                if (wrappedElement != null)
                {
                    _wrappedElements.Add(wrappedElement);
                }
            }
            else
            {
                _current = element;
            }
        }
 private void ReadElement()
 {
     this._previousElement = this._rowEnumerator.RootCoordinator.Current;
     if (this.TryReadToNextElement())
     {
         this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.Reading;
     }
     else
     {
         this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.NoRowsLastElementPending;
     }
 }
Пример #9
0
        /// <summary>
        ///     Returns the "default" record state (that is, the one we use for PreRead/PastEnd reader states
        /// </summary>
        internal RecordState GetDefaultRecordState(Shaper <RecordState> shaper)
        {
            RecordState result = null;

            if (RecordStateFactories.Count > 0)
            {
                // CONSIDER: We're relying upon having the default for polymorphic types be
                // the first item in the list; that sounds kind of risky.
                result = (RecordState)shaper.State[RecordStateFactories[0].StateSlotNumber];
                Debug.Assert(null != result, "did you initialize the record states?");
                result.ResetToDefaultState();
            }
            return(result);
        }
        public virtual IDbEnumerator <T> GetEnumerator()
        {
            if (this.RootCoordinator.CoordinatorFactory.IsSimple)
            {
                return((IDbEnumerator <T>) new Shaper <T> .SimpleEnumerator(this));
            }
            Shaper <T> .RowNestedResultEnumerator rowEnumerator = new Shaper <T> .RowNestedResultEnumerator(this);

            if (this._isObjectQuery)
            {
                return((IDbEnumerator <T>) new Shaper <T> .ObjectQueryNestedEnumerator(rowEnumerator));
            }
            return((IDbEnumerator <T>) new Shaper <T> .RecordStateEnumerator(rowEnumerator));
        }
        // <summary>
        // Precondition: the current row has data for the coordinator.
        // Side-effects: updates keys currently stored in state and updates IsEntered if a new value is encountered.
        // Determines whether the row contains the next element in this collection.
        // </summary>
        internal bool HasNextElement(Shaper shaper)
        {
            // check if this row contains a new element for this coordinator
            var result = false;

            if (!IsEntered ||
                !CoordinatorFactory.CheckKeys(shaper))
            {
                // remember initial keys values
                CoordinatorFactory.SetKeys(shaper);
                IsEntered = true;
                result    = true;
            }

            return(result);
        }
        // <summary>
        // Registers this hierarchy of coordinators in the given shaper.
        // </summary>
        internal void Initialize(Shaper shaper)
        {
            ResetCollection(shaper);

            // Add this coordinator to the appropriate state slot in the
            // shaper so that it is available to materialization delegates.
            shaper.State[CoordinatorFactory.StateSlot] = this;

            if (null != Child)
            {
                Child.Initialize(shaper);
            }
            if (null != Next)
            {
                Next.Initialize(shaper);
            }
        }
Пример #13
0
            private void Returns_RecordStateEnumerator_for_nested_coordinatorFactories_of_RecordState(
                Func <IDbEnumerator <RecordState>, List <object> > toList)
            {
                var sourceEnumerable = new[]
                {
                    new object[] { 1, "A", null },
                    new object[] { 2, null, "X" },
                    new object[] { 3, "B", "Z" },
                    // Should stop reading at "B", since the coordinators are at the same depth
                    new object[] { 4, "C", null },
                    new object[] { 4, "D", null }                            // 4 shouldn't be added as it's repeated
                };

                var nestedCoordinatorFactoryOne = Objects.MockHelper.CreateCoordinatorFactory <string, RecordState>(
                    depth: 1,
                    stateSlot: 2,
                    ordinal: 1,
                    nestedCoordinators: new CoordinatorFactory[0],
                    producedValues: null);

                var nestedCoordinatorFactoryTwo = Objects.MockHelper.CreateCoordinatorFactory <string, RecordState>(
                    depth: 1,
                    stateSlot: 4,
                    ordinal: 2,
                    nestedCoordinators: new CoordinatorFactory[0],
                    producedValues: null);

                var rootCoordinatorFactory = Objects.MockHelper.CreateCoordinatorFactory <int, RecordState>(
                    depth: 0,
                    stateSlot: 0,
                    ordinal: 0,
                    nestedCoordinators: new[] { nestedCoordinatorFactoryOne, nestedCoordinatorFactoryTwo },
                    producedValues: null);

                var shaper = new Shaper <RecordState>(
                    MockHelper.CreateDbDataReader(sourceEnumerable),
                    /*context*/ null,
                    /*workspace*/ null,
                    MergeOption.AppendOnly,
                    /*stateCount*/ 6,
                    rootCoordinatorFactory,
                    /*readerOwned*/ false,
                    /*useSpatialReader*/ false);

                Assert.Equal(new object[] { 1, "A", 2, "X", 3, "B", 4, "C", "D" }.ToList(), toList(shaper.RootEnumerator));
            }
Пример #14
0
            private void Returns_SimpleEnumerator_for_simple_CoordinatorFactory(
                Func <IDbEnumerator <object>, List <object> > toList)
            {
                var sourceEnumerable = new[] { new object[] { 1 }, new object[] { 2 } };

                var coordinatorFactory = Objects.MockHelper.CreateCoordinatorFactory(s => s.Reader.GetValue(0));

                var shaper = new Shaper <object>(
                    MockHelper.CreateDbDataReader(sourceEnumerable),
                    /*context*/ null,
                    /*workspace*/ null,
                    MergeOption.AppendOnly,
                    /*stateCount*/ 1,
                    coordinatorFactory,
                    /*readerOwned*/ false,
                    /*useSpatialReader*/ false);

                var actualEnumerator = shaper.GetEnumerator();

                Assert.Equal(sourceEnumerable.SelectMany(e => e).ToList(), toList(actualEnumerator));
            }
Пример #15
0
        internal override void ReadNextElement(Shaper shaper)
        {
            IEntityWrapper entityWrapper = (IEntityWrapper)null;
            T obj1;

            try
            {
                if (this.TypedCoordinatorFactory.WrappedElement == null)
                {
                    obj1 = this.TypedCoordinatorFactory.Element(shaper);
                }
                else
                {
                    entityWrapper = this.TypedCoordinatorFactory.WrappedElement(shaper);
                    obj1          = (T)entityWrapper.Entity;
                }
            }
            catch (Exception ex)
            {
                if (ex.IsCatchableExceptionType() && !shaper.Reader.IsClosed)
                {
                    this.ResetCollection(shaper);
                    T obj2 = this.TypedCoordinatorFactory.ElementWithErrorHandling(shaper);
                }
                throw;
            }
            if (this.IsUsingElementCollection)
            {
                this._elements.Add(obj1);
                if (entityWrapper == null)
                {
                    return;
                }
                this._wrappedElements.Add(entityWrapper);
            }
            else
            {
                this._current = obj1;
            }
        }
Пример #16
0
        public void RecordStateEnumerator_MoveNextAsync_returns_reader_consumed_even_if_task_is_being_cancelled()
        {
            var coordinatorFactory = Objects.MockHelper.CreateCoordinatorFactory(s => (RecordState)null, s => true);

            var shaper = new Shaper <RecordState>(
                new Mock <DbDataReader>().Object,
                /*context*/ null,
                /*workspace*/ null,
                MergeOption.AppendOnly,
                /*stateCount*/ 1,
                coordinatorFactory,
                /*readerOwned*/ false,
                /*streaming*/ false);


            using (var enumerator = shaper.GetEnumerator())
            {
                Assert.Contains("RecordStateEnumerator", enumerator.GetType().FullName);
                enumerator.MoveNext();
                Assert.False(enumerator.MoveNextAsync(new CancellationToken(canceled: true)).GetAwaiter().GetResult());
            }
        }
Пример #17
0
 internal override void ResetCollection(Shaper shaper)
 {
     if (this._handleClose != null)
     {
         this._handleClose(shaper, this._wrappedElements);
         this._handleClose = (Action <Shaper, List <IEntityWrapper> >)null;
     }
     this.IsEntered = false;
     if (this.IsUsingElementCollection)
     {
         this._elements        = this.TypedCoordinatorFactory.InitializeCollection(shaper);
         this._wrappedElements = new List <IEntityWrapper>();
     }
     if (this.Child != null)
     {
         this.Child.ResetCollection(shaper);
     }
     if (this.Next == null)
     {
         return;
     }
     this.Next.ResetCollection(shaper);
 }
Пример #18
0
        public void ObjectQueryNestedEnumerator_MoveNextAsync_throws_OperationCanceledException_if_task_is_cancelled()
        {
            var coordinatorFactory = Objects.MockHelper.CreateCoordinatorFactory(s => s.Reader.GetValue(0), s => true);

            var shaper = new Shaper <object>(
                new Mock <DbDataReader>().Object,
                /*context*/ null,
                /*workspace*/ null,
                MergeOption.AppendOnly,
                /*stateCount*/ 1,
                coordinatorFactory,
                /*readerOwned*/ false,
                /*streaming*/ false);

            using (var enumerator = shaper.GetEnumerator())
            {
                Assert.Contains("ObjectQueryNestedEnumerator", enumerator.GetType().FullName);

                Assert.Throws <OperationCanceledException>(
                    () => enumerator.MoveNextAsync(new CancellationToken(canceled: true))
                    .GetAwaiter().GetResult());
            }
        }
            public async Task <bool> MoveNextAsync(CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                switch (this._state)
                {
                case Shaper <T> .ObjectQueryNestedEnumerator.State.Start:
                    if (await this.TryReadToNextElementAsync(cancellationToken).WithCurrentCulture <bool>())
                    {
                        await this.ReadElementAsync(cancellationToken).WithCurrentCulture();

                        break;
                    }
                    this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows;
                    break;

                case Shaper <T> .ObjectQueryNestedEnumerator.State.Reading:
                    await this.ReadElementAsync(cancellationToken).WithCurrentCulture();

                    break;

                case Shaper <T> .ObjectQueryNestedEnumerator.State.NoRowsLastElementPending:
                    this._state = Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows;
                    break;
                }
                bool result;

                if (this._state == Shaper <T> .ObjectQueryNestedEnumerator.State.NoRows)
                {
                    this._previousElement = default(T);
                    result = false;
                }
                else
                {
                    result = true;
                }
                return(result);
            }
Пример #20
0
        public void SimpleEnumerator_MoveNextAsync_does_not_throw_if_shaper_is_not_active_even_if_task_is_being_cancelled()
        {
            var coordinatorFactory = Objects.MockHelper.CreateCoordinatorFactory(s => s.Reader.GetValue(0));

            var shaper = new Shaper <object>(
                new Mock <DbDataReader>().Object,
                /*context*/ null,
                /*workspace*/ null,
                MergeOption.AppendOnly,
                /*stateCount*/ 1,
                coordinatorFactory,
                /*readerOwned*/ false,
                /*streaming*/ false);

            using (var enumerator = shaper.GetEnumerator())
            {
                Assert.Contains("SimpleEnumerator", enumerator.GetType().FullName);

                enumerator.MoveNext();

                Assert.False(enumerator.MoveNextAsync(new CancellationToken(canceled: true))
                             .GetAwaiter().GetResult());
            }
        }
 internal SimpleEnumerator(Shaper <T> shaper)
 {
     _shaper = shaper;
 }
 // <summary>
 // Precondition: the current row has data and contains a new element for the coordinator.
 // Reads the next element in this collection.
 // </summary>
 internal abstract void ReadNextElement(Shaper shaper);
 internal ObjectQueryNestedEnumerator(Shaper <T> .RowNestedResultEnumerator rowEnumerator)
 {
     this._rowEnumerator   = rowEnumerator;
     this._previousElement = default(T);
     this._state           = Shaper <T> .ObjectQueryNestedEnumerator.State.Start;
 }
 private async Task ReadElementAsync(CancellationToken cancellationToken)
 {
     this._previousElement = this._rowEnumerator.RootCoordinator.Current;
     this._state           = !await this.TryReadToNextElementAsync(cancellationToken).WithCurrentCulture <bool>() ? Shaper <T> .ObjectQueryNestedEnumerator.State.NoRowsLastElementPending : Shaper <T> .ObjectQueryNestedEnumerator.State.Reading;
 }
 internal RowNestedResultEnumerator(Shaper <T> shaper)
 {
     _shaper  = shaper;
     _current = new Coordinator[_shaper.RootCoordinator.MaxDistanceToLeaf() + 1];
 }
Пример #26
0
 // <summary>
 // Called from the Element expression on the Coordinator to gather all
 // the data for the record; we just turn around and call the expression
 // we build on the RecordStateFactory.
 // </summary>
 internal RecordState GatherData(Shaper shaper)
 {
     RecordStateFactory.GatherData(shaper);
     _pendingIsNull = false;
     return(this);
 }
 // <summary>
 // This method is called when the current collection is finished and it's time to move to the next collection.
 // Recursively initializes children and siblings as well.
 // </summary>
 internal abstract void ResetCollection(Shaper shaper);