Example #1
0
        /// <summary>
        /// Reads the current column, returns its value according to <paramref name="type"/> and
        /// moves ahead to the next column.
        /// If the column is null an exception is thrown.
        /// </summary>
        /// <param name="type">
        /// In some cases <typeparamref name="T"/> isn't enough to infer the data type coming in from the
        /// database. This parameter and be used to unambiguously specify the type. An example is the JSONB
        /// type, for which <typeparamref name="T"/> will be a simple string but for which
        /// <paramref name="type"/> must be specified as <see cref="NpgsqlDbType.Jsonb"/>.
        /// </param>
        /// <typeparam name="T">The .NET type of the column to be read.</typeparam>
        /// <returns>The value of the column</returns>
        public T Read <T>(NpgsqlDbType type)
        {
            CheckDisposed();
            if (_column == -1 || _column == NumColumns)
            {
                throw new InvalidOperationException("Not reading a row");
            }

            var handler = _typeHandlerCache[_column];

            if (handler == null)
            {
                handler = _typeHandlerCache[_column] = _typeMapper.GetByNpgsqlDbType(type);
            }
            return(DoRead <T>(handler));
        }
Example #2
0
        ValueTask <T> Read <T>(NpgsqlDbType type, bool async, CancellationToken cancellationToken = default)
        {
            CheckDisposed();
            if (_column == -1 || _column == NumColumns)
            {
                throw new InvalidOperationException("Not reading a row");
            }

            var handler = _typeHandlerCache[_column];

            if (handler == null)
            {
                handler = _typeHandlerCache[_column] = _typeMapper.GetByNpgsqlDbType(type);
            }

            return(DoRead <T>(handler, async, cancellationToken));
        }