Beispiel #1
0
        /// <summary>
        /// Seeks to the given column. The 4-byte length is read and stored in <see cref="NpgsqlDataReader.ColumnLen"/>.
        /// </summary>
        internal override async Task SeekToColumn(int column, bool async)
        {
            if (_column == -1)
            {
                await Buffer.Ensure(2, async);

                _numColumns = Buffer.ReadInt16();
            }

            if (column < 0 || column >= _numColumns)
            {
                throw new IndexOutOfRangeException("Column index out of range");
            }

            if (column < _column)
            {
                throw new InvalidOperationException($"Invalid attempt to read from column ordinal '{column}'. With CommandBehavior.SequentialAccess, you may only read from column ordinal '{_column}' or greater.");
            }

            if (column > _column)
            {
                // Skip to end of column if needed
                // TODO: Simplify by better initializing _columnLen/_posInColumn
                var remainingInColumn = ColumnLen == -1 ? 0 : ColumnLen - PosInColumn;
                if (remainingInColumn > 0)
                {
                    await Buffer.Skip(remainingInColumn, async);
                }

                // Shut down any streaming going on on the column
                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }

                // Skip over unwanted fields
                for (; _column < column - 1; _column++)
                {
                    await Buffer.Ensure(4, async);

                    var len = Buffer.ReadInt32();
                    if (len != -1)
                    {
                        await Buffer.Skip(len, async);
                    }
                }

                await Buffer.Ensure(4, async);

                ColumnLen   = Buffer.ReadInt32();
                PosInColumn = 0;
                _column     = column;
            }
        }
        internal override Stream GetStream()
        {
            Debug.Assert(PosInColumn == 0);
            if (_stream != null)
            {
                throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
            }
            var stream = new SequentialByteaStream(this);

            _stream = stream;
            return(stream);
        }
Beispiel #3
0
        internal override async Task <Stream> GetStreamInternal(int column, bool async)
        {
            CheckRowAndOrdinal(column);
            if (_stream != null)
            {
                throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
            }

            await SeekToColumn(column, false);

            if (ColumnLen == -1)
            {
                throw new InvalidCastException("Column is null");
            }

            return(_stream = new SequentialByteaStream(this));
        }
Beispiel #4
0
        internal override async Task ConsumeRow(bool async)
        {
            if (_column == -1)
            {
                await Buffer.Ensure(2, async);

                _numColumns = Buffer.ReadInt16();
            }

            if (_stream != null)
            {
                _stream.Dispose();
                _stream = null;
            }

            // TODO: Potential for code-sharing with ReadColumn above, which also skips
            // Skip to end of column if needed
            var remainingInColumn = ColumnLen == -1 ? 0 : ColumnLen - PosInColumn;

            if (remainingInColumn > 0)
            {
                await Buffer.Skip(remainingInColumn, async);
            }

            // Skip over the remaining columns in the row
            for (; _column < _numColumns - 1; _column++)
            {
                await Buffer.Ensure(4, async);

                var len = Buffer.ReadInt32();
                if (len != -1)
                {
                    await Buffer.Skip(len, async);
                }
            }
        }
 internal override Stream GetStream()
 {
     Contract.Requires(PosInColumn == 0);
     if (_stream != null) {
         throw new InvalidOperationException("Attempt to read a position in the column which has already been read");
     }
     var stream = new SequentialByteaStream(this);
     _stream = stream;
     return stream;
 }