package pgtype

import (
	
	
	
)

// QCharCodec is for PostgreSQL's special 8-bit-only "char" type more akin to the C
// language's char type, or Go's byte type. (Note that the name in PostgreSQL
// itself is "char", in double-quotes, and not char.) It gets used a lot in
// PostgreSQL's system tables to hold a single ASCII character value (eg
// pg_class.relkind). It is named Qchar for quoted char to disambiguate from SQL
// standard type char.
type QCharCodec struct{}

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

func (QCharCodec) () int16 {
	return BinaryFormatCode
}

func (QCharCodec) ( *Map,  uint32,  int16,  any) EncodePlan {
	switch  {
	case TextFormatCode, BinaryFormatCode:
		switch .(type) {
		case byte:
			return encodePlanQcharCodecByte{}
		case rune:
			return encodePlanQcharCodecRune{}
		}
	}

	return nil
}

type encodePlanQcharCodecByte struct{}

func (encodePlanQcharCodecByte) ( any,  []byte) ( []byte,  error) {
	 := .(byte)
	 = append(, )
	return , nil
}

type encodePlanQcharCodecRune struct{}

func (encodePlanQcharCodecRune) ( any,  []byte) ( []byte,  error) {
	 := .(rune)
	if  > math.MaxUint8 {
		return nil, fmt.Errorf(`%v cannot be encoded to "char"`, )
	}
	 := byte()
	 = append(, )
	return , nil
}

func (QCharCodec) ( *Map,  uint32,  int16,  any) ScanPlan {
	switch  {
	case TextFormatCode, BinaryFormatCode:
		switch .(type) {
		case *byte:
			return scanPlanQcharCodecByte{}
		case *rune:
			return scanPlanQcharCodecRune{}
		}
	}

	return nil
}

type scanPlanQcharCodecByte struct{}

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

	if len() > 1 {
		return fmt.Errorf(`invalid length for "char": %v`, len())
	}

	 := .(*byte)
	// In the text format the zero value is returned as a zero byte value instead of 0
	if len() == 0 {
		* = 0
	} else {
		* = [0]
	}

	return nil
}

type scanPlanQcharCodecRune struct{}

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

	if len() > 1 {
		return fmt.Errorf(`invalid length for "char": %v`, len())
	}

	 := .(*rune)
	// In the text format the zero value is returned as a zero byte value instead of 0
	if len() == 0 {
		* = 0
	} else {
		* = rune([0])
	}

	return nil
}

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

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

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

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