package pgtype
import (
"database/sql/driver"
"fmt"
"math"
)
type QCharCodec struct {}
func (QCharCodec ) FormatSupported (format int16 ) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (QCharCodec ) PreferredFormat () int16 {
return BinaryFormatCode
}
func (QCharCodec ) PlanEncode (m *Map , oid uint32 , format int16 , value any ) EncodePlan {
switch format {
case TextFormatCode , BinaryFormatCode :
switch value .(type ) {
case byte :
return encodePlanQcharCodecByte {}
case rune :
return encodePlanQcharCodecRune {}
}
}
return nil
}
type encodePlanQcharCodecByte struct {}
func (encodePlanQcharCodecByte ) Encode (value any , buf []byte ) (newBuf []byte , err error ) {
b := value .(byte )
buf = append (buf , b )
return buf , nil
}
type encodePlanQcharCodecRune struct {}
func (encodePlanQcharCodecRune ) Encode (value any , buf []byte ) (newBuf []byte , err error ) {
r := value .(rune )
if r > math .MaxUint8 {
return nil , fmt .Errorf (`%v cannot be encoded to "char"` , r )
}
b := byte (r )
buf = append (buf , b )
return buf , nil
}
func (QCharCodec ) PlanScan (m *Map , oid uint32 , format int16 , target any ) ScanPlan {
switch format {
case TextFormatCode , BinaryFormatCode :
switch target .(type ) {
case *byte :
return scanPlanQcharCodecByte {}
case *rune :
return scanPlanQcharCodecRune {}
}
}
return nil
}
type scanPlanQcharCodecByte struct {}
func (scanPlanQcharCodecByte ) Scan (src []byte , dst any ) error {
if src == nil {
return fmt .Errorf ("cannot scan NULL into %T" , dst )
}
if len (src ) > 1 {
return fmt .Errorf (`invalid length for "char": %v` , len (src ))
}
b := dst .(*byte )
if len (src ) == 0 {
*b = 0
} else {
*b = src [0 ]
}
return nil
}
type scanPlanQcharCodecRune struct {}
func (scanPlanQcharCodecRune ) Scan (src []byte , dst any ) error {
if src == nil {
return fmt .Errorf ("cannot scan NULL into %T" , dst )
}
if len (src ) > 1 {
return fmt .Errorf (`invalid length for "char": %v` , len (src ))
}
r := dst .(*rune )
if len (src ) == 0 {
*r = 0
} else {
*r = rune (src [0 ])
}
return nil
}
func (c QCharCodec ) DecodeDatabaseSQLValue (m *Map , oid uint32 , format int16 , src []byte ) (driver .Value , error ) {
if src == nil {
return nil , nil
}
var r rune
err := codecScan (c , m , oid , format , src , &r )
if err != nil {
return nil , err
}
return string (r ), nil
}
func (c QCharCodec ) DecodeValue (m *Map , oid uint32 , format int16 , src []byte ) (any , error ) {
if src == nil {
return nil , nil
}
var r rune
err := codecScan (c , m , oid , format , src , &r )
if err != nil {
return nil , err
}
return r , nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .