/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// <param name="cmd">The command this instance will be associated with.</param>
		protected EnumerableExecutor(IEnumerableCommand cmd)
		{
			if (cmd == null) throw new ArgumentNullException("command", "Command cannot be null.");
			if (cmd.IsDisposed) throw new ObjectDisposedException(cmd.ToString());
			if (cmd.Link.IsDisposed) throw new ObjectDisposedException("Link '{0}' of command '{1}' is disposed.".FormatWith(cmd.Link, cmd));

			_Command = cmd;
		}
예제 #2
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="cmd">The command this instance will be associated with.</param>
        protected EnumerableExecutor(IEnumerableCommand cmd)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException("command", "Command cannot be null.");
            }
            if (cmd.IsDisposed)
            {
                throw new ObjectDisposedException(cmd.ToString());
            }
            if (cmd.Link.IsDisposed)
            {
                throw new ObjectDisposedException("Link '{0}' of command '{1}' is disposed.".FormatWith(cmd.Link, cmd));
            }

            _Command = cmd;
        }
예제 #3
0
        /// <summary>
        /// Executes the command if it has not been executed yet in this instance. Returns true
        /// if a new record is available, or false otherwise.
        /// </summary>
        public bool MoveNext()
        {
            // Always needed...
            _CurrentRecord = null;
            _Current       = null;

            try
            {
                // First execution...
                if (!_Started)
                {
                    if (IsDisposed)
                    {
                        throw new ObjectDisposedException(this.ToString());
                    }
                    if (_Command.IsDisposed)
                    {
                        throw new ObjectDisposedException(_Command.ToString());
                    }
                    if (_Command.Link.IsDisposed)
                    {
                        throw new ObjectDisposedException(_Command.Link.ToString());
                    }

                    if (!Command.CanBeExecuted)
                    {
                        throw new CannotExecuteException(
                                  "State of command '{0}' is not ready for execution.".FormatWith(Command));
                    }

                    _Started       = true;
                    _TakeOnGoing   = false;
                    _TakeRemaining = -1;

                    _Schema = OnReaderStart();
                    if (_Schema == null)
                    {
                        throw new InvalidOperationException("Schema resulting from execution of '{0}' is null.".FormatWith(this));
                    }
                    if (_Schema.Count == 0)
                    {
                        throw new EmptyException("Schema resulting of execution from '{0}' is empty.".FormatWith(this));
                    }

                    // Emulation of skip/take if needed...
                    var cmd = _Command as IQueryCommand; if (cmd != null)
                    {
                        int  skip  = cmd.GetSkipValue();
                        int  take  = cmd.GetTakeValue();
                        bool valid = cmd.IsValidForNativeSkipTake();

                        if (!valid && skip > 0)
                        {
                            for (int i = 0; i < skip; i++)
                            {
                                if (OnReaderNext() == null)
                                {
                                    Reset(); return(false);
                                }
                            }
                        }

                        if (!valid && take > 0)
                        {
                            _TakeOnGoing   = true;
                            _TakeRemaining = take;
                        }
                    }
                }

                // Current iteration...
                if (_TakeOnGoing)
                {
                    if (_TakeRemaining > 0)
                    {
                        _CurrentRecord = OnReaderNext();
                        _TakeRemaining--;
                    }
                    else
                    {
                        _CurrentRecord = null;
                    }
                }
                else
                {
                    _CurrentRecord = OnReaderNext();
                }

                // Finalizing...
                if (_CurrentRecord == null)
                {
                    Reset(); return(false);
                }
                else
                {
                    _Current = _Converter == null ? _CurrentRecord : _Converter(_CurrentRecord);
                    return(true);
                }
            }
            catch
            {
                OnDispose(disposing: true);
                throw;
            }
        }