package pgtype

import (
	
	
	
	
	

	
)

type Uint32Scanner interface {
	ScanUint32(v Uint32) error
}

type Uint32Valuer interface {
	Uint32Value() (Uint32, error)
}

// Uint32 is the core type that is used to represent PostgreSQL types such as OID, CID, and XID.
type Uint32 struct {
	Uint32 uint32
	Valid  bool
}

func ( *Uint32) ( Uint32) error {
	* = 
	return nil
}

func ( Uint32) () (Uint32, error) {
	return , nil
}

// Scan implements the database/sql Scanner interface.
func ( *Uint32) ( any) error {
	if  == nil {
		* = Uint32{}
		return nil
	}

	var  int64

	switch src := .(type) {
	case int64:
		 = 
	case string:
		,  := strconv.ParseUint(, 10, 32)
		if  != nil {
			return 
		}
		 = int64()
	default:
		return fmt.Errorf("cannot scan %T", )
	}

	if  < 0 {
		return fmt.Errorf("%d is less than the minimum value for Uint32", )
	}
	if  > math.MaxUint32 {
		return fmt.Errorf("%d is greater than maximum value for Uint32", )
	}

	* = Uint32{Uint32: uint32(), Valid: true}

	return nil
}

// Value implements the database/sql/driver Valuer interface.
func ( Uint32) () (driver.Value, error) {
	if !.Valid {
		return nil, nil
	}
	return int64(.Uint32), nil
}

type Uint32Codec struct{}

func (Uint32Codec) ( int16) bool {
	return  == TextFormatCode ||  == BinaryFormatCode
}

func (Uint32Codec) () int16 {
	return BinaryFormatCode
}

func (Uint32Codec) ( *Map,  uint32,  int16,  any) EncodePlan {
	switch  {
	case BinaryFormatCode:
		switch .(type) {
		case uint32:
			return encodePlanUint32CodecBinaryUint32{}
		case Uint32Valuer:
			return encodePlanUint32CodecBinaryUint32Valuer{}
		case Int64Valuer:
			return encodePlanUint32CodecBinaryInt64Valuer{}
		}
	case TextFormatCode:
		switch .(type) {
		case uint32:
			return encodePlanUint32CodecTextUint32{}
		case Int64Valuer:
			return encodePlanUint32CodecTextInt64Valuer{}
		}
	}

	return nil
}

type encodePlanUint32CodecBinaryUint32 struct{}

func (encodePlanUint32CodecBinaryUint32) ( any,  []byte) ( []byte,  error) {
	 := .(uint32)
	return pgio.AppendUint32(, ), nil
}

type encodePlanUint32CodecBinaryUint32Valuer struct{}

func (encodePlanUint32CodecBinaryUint32Valuer) ( any,  []byte) ( []byte,  error) {
	,  := .(Uint32Valuer).Uint32Value()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	return pgio.AppendUint32(, .Uint32), nil
}

type encodePlanUint32CodecBinaryInt64Valuer struct{}

func (encodePlanUint32CodecBinaryInt64Valuer) ( any,  []byte) ( []byte,  error) {
	,  := .(Int64Valuer).Int64Value()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	if .Int64 < 0 {
		return nil, fmt.Errorf("%d is less than minimum value for uint32", .Int64)
	}
	if .Int64 > math.MaxUint32 {
		return nil, fmt.Errorf("%d is greater than maximum value for uint32", .Int64)
	}

	return pgio.AppendUint32(, uint32(.Int64)), nil
}

type encodePlanUint32CodecTextUint32 struct{}

func (encodePlanUint32CodecTextUint32) ( any,  []byte) ( []byte,  error) {
	 := .(uint32)
	return append(, strconv.FormatUint(uint64(), 10)...), nil
}

type encodePlanUint32CodecTextUint32Valuer struct{}

func (encodePlanUint32CodecTextUint32Valuer) ( any,  []byte) ( []byte,  error) {
	,  := .(Uint32Valuer).Uint32Value()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	return append(, strconv.FormatUint(uint64(.Uint32), 10)...), nil
}

type encodePlanUint32CodecTextInt64Valuer struct{}

func (encodePlanUint32CodecTextInt64Valuer) ( any,  []byte) ( []byte,  error) {
	,  := .(Int64Valuer).Int64Value()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	if .Int64 < 0 {
		return nil, fmt.Errorf("%d is less than minimum value for uint32", .Int64)
	}
	if .Int64 > math.MaxUint32 {
		return nil, fmt.Errorf("%d is greater than maximum value for uint32", .Int64)
	}

	return append(, strconv.FormatInt(.Int64, 10)...), nil
}

func (Uint32Codec) ( *Map,  uint32,  int16,  any) ScanPlan {

	switch  {
	case BinaryFormatCode:
		switch .(type) {
		case *uint32:
			return scanPlanBinaryUint32ToUint32{}
		case Uint32Scanner:
			return scanPlanBinaryUint32ToUint32Scanner{}
		}
	case TextFormatCode:
		switch .(type) {
		case *uint32:
			return scanPlanTextAnyToUint32{}
		case Uint32Scanner:
			return scanPlanTextAnyToUint32Scanner{}
		}
	}

	return nil
}

func ( Uint32Codec) ( *Map,  uint32,  int16,  []byte) (driver.Value, error) {
	if  == nil {
		return nil, nil
	}

	var  uint32
	 := codecScan(, , , , , &)
	if  != nil {
		return nil, 
	}
	return int64(), nil
}

func ( Uint32Codec) ( *Map,  uint32,  int16,  []byte) (any, error) {
	if  == nil {
		return nil, nil
	}

	var  uint32
	 := codecScan(, , , , , &)
	if  != nil {
		return nil, 
	}
	return , nil
}

type scanPlanBinaryUint32ToUint32 struct{}

func (scanPlanBinaryUint32ToUint32) ( []byte,  any) error {
	if  == nil {
		return fmt.Errorf("cannot scan NULL into %T", )
	}

	if len() != 4 {
		return fmt.Errorf("invalid length for uint32: %v", len())
	}

	 := ().(*uint32)
	* = binary.BigEndian.Uint32()

	return nil
}

type scanPlanBinaryUint32ToUint32Scanner struct{}

func (scanPlanBinaryUint32ToUint32Scanner) ( []byte,  any) error {
	,  := ().(Uint32Scanner)
	if ! {
		return ErrScanTargetTypeChanged
	}

	if  == nil {
		return .ScanUint32(Uint32{})
	}

	if len() != 4 {
		return fmt.Errorf("invalid length for uint32: %v", len())
	}

	 := binary.BigEndian.Uint32()

	return .ScanUint32(Uint32{Uint32: , Valid: true})
}

type scanPlanTextAnyToUint32Scanner struct{}

func (scanPlanTextAnyToUint32Scanner) ( []byte,  any) error {
	,  := ().(Uint32Scanner)
	if ! {
		return ErrScanTargetTypeChanged
	}

	if  == nil {
		return .ScanUint32(Uint32{})
	}

	,  := strconv.ParseUint(string(), 10, 32)
	if  != nil {
		return 
	}

	return .ScanUint32(Uint32{Uint32: uint32(), Valid: true})
}