示例#1
0
        T DoRead <T>(TypeHandler handler)
        {
            ReadColumnLenIfNeeded();
            if (_columnLen == -1)
            {
                // TODO: What actual exception to throw here? Oracle throws InvalidCast, SqlClient throws its
                // own SqlNullValueException
                throw new InvalidCastException("Column is null");
            }

            var result = handler.Read <T>(_buf, _columnLen);

            _leftToReadInDataMsg -= _columnLen;
            _columnLen            = int.MinValue; // Mark that the (next) column length hasn't been read yet
            _column++;
            return(result);
        }
        T DoRead <T>(TypeHandler handler)
        {
            try {
                ReadColumnLenIfNeeded();
                if (_columnLen == -1)
                {
                    throw new InvalidCastException("Column is null");
                }

                var result = handler.Read <T>(_buf, _columnLen);
                _leftToReadInDataMsg -= _columnLen;
                _columnLen            = int.MinValue; // Mark that the (next) column length hasn't been read yet
                _column++;
                return(result);
            } catch {
                _connector.Break();
                Cleanup();
                throw;
            }
        }
示例#3
0
        T DoRead <T>(TypeHandler handler)
        {
            try {
                ReadColumnLenIfNeeded();
                if (_columnLen == -1)
                {
                    throw new InvalidCastException("Column is null");
                }

                // TODO: Duplication with NpgsqlDataReader.GetFieldValueInternal

                T result;

                // The type handler supports the requested type directly
                var tHandler = handler as ITypeHandler <T>;
                if (tHandler != null)
                {
                    result = handler.Read <T>(_buf, _columnLen, false).Result;
                }
                else
                {
                    var t = typeof(T);
                    if (!t.IsArray)
                    {
                        throw new InvalidCastException($"Can't cast database type {handler.PgDisplayName} to {typeof(T).Name}");
                    }

                    // Getting an array

                    // We need to treat this as an actual array type, these need special treatment because of
                    // typing/generics reasons (there is no way to express "array of X" with generics
                    var elementType  = t.GetElementType();
                    var arrayHandler = handler as ArrayHandler;
                    if (arrayHandler == null)
                    {
                        throw new InvalidCastException($"Can't cast database type {handler.PgDisplayName} to {typeof(T).Name}");
                    }

                    if (arrayHandler.GetElementFieldType() == elementType)
                    {
                        result = (T)handler.ReadAsObject(_buf, _columnLen, false).Result;
                    }
                    else if (arrayHandler.GetElementPsvType() == elementType)
                    {
                        result = (T)handler.ReadPsvAsObject(_buf, _columnLen, false).Result;
                    }
                    else
                    {
                        throw new InvalidCastException($"Can't cast database type {handler.PgDisplayName} to {typeof(T).Name}");
                    }
                }

                _leftToReadInDataMsg -= _columnLen;
                _columnLen            = int.MinValue; // Mark that the (next) column length hasn't been read yet
                _column++;
                return(result);
            } catch {
                _connector.Break();
                Cleanup();
                throw;
            }
        }