package pgtype

import (
	
	
	
)

type Bytea struct {
	Bytes  []byte
	Status Status
}

func ( *Bytea) ( interface{}) error {
	if  == nil {
		* = Bytea{Status: Null}
		return nil
	}

	if ,  := .(interface{ () interface{} });  {
		 := .()
		if  !=  {
			return .()
		}
	}

	switch value := .(type) {
	case []byte:
		if  != nil {
			* = Bytea{Bytes: , Status: Present}
		} else {
			* = Bytea{Status: Null}
		}
	default:
		if ,  := underlyingBytesType();  {
			return .()
		}
		return fmt.Errorf("cannot convert %v to Bytea", )
	}

	return nil
}

func ( Bytea) () interface{} {
	switch .Status {
	case Present:
		return .Bytes
	case Null:
		return nil
	default:
		return .Status
	}
}

func ( *Bytea) ( interface{}) error {
	switch .Status {
	case Present:
		switch v := .(type) {
		case *[]byte:
			 := make([]byte, len(.Bytes))
			copy(, .Bytes)
			* = 
			return nil
		default:
			if ,  := GetAssignToDstType();  {
				return .()
			}
			return fmt.Errorf("unable to assign to %T", )
		}
	case Null:
		return NullAssignTo()
	}

	return fmt.Errorf("cannot decode %#v into %T", , )
}
DecodeText only supports the hex format. This has been the default since PostgreSQL 9.0.
func ( *Bytea) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Bytea{Status: Null}
		return nil
	}

	if len() < 2 || [0] != '\\' || [1] != 'x' {
		return fmt.Errorf("invalid hex format")
	}

	 := make([]byte, (len()-2)/2)
	,  := hex.Decode(, [2:])
	if  != nil {
		return 
	}

	* = Bytea{Bytes: , Status: Present}
	return nil
}

func ( *Bytea) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = Bytea{Status: Null}
		return nil
	}

	* = Bytea{Bytes: , Status: Present}
	return nil
}

func ( Bytea) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	 = append(, `\x`...)
	 = append(, hex.EncodeToString(.Bytes)...)
	return , nil
}

func ( Bytea) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return append(, .Bytes...), nil
}
Scan implements the database/sql Scanner interface.
func ( *Bytea) ( interface{}) error {
	if  == nil {
		* = Bytea{Status: Null}
		return nil
	}

	switch src := .(type) {
	case string:
		return .DecodeText(nil, []byte())
	case []byte:
		 := make([]byte, len())
		copy(, )
		* = Bytea{Bytes: , Status: Present}
		return nil
	}

	return fmt.Errorf("cannot scan %T", )
}
Value implements the database/sql/driver Valuer interface.
func ( Bytea) () (driver.Value, error) {
	switch .Status {
	case Present:
		return .Bytes, nil
	case Null:
		return nil, nil
	default:
		return nil, errUndefined
	}