pgx/bpchar.go
Jack Christensen 8cd94a14c7 Allow types to specify preference format result and param formats
This will be useful for array and composite types that may have to
support elements that may not support binary encoding.

It also is slightly more convenient for text-ish types to have a default
format of text.
2020-05-10 14:05:16 -05:00

77 lines
1.7 KiB
Go

package pgtype
import (
"database/sql/driver"
)
// BPChar is fixed-length, blank padded char type
// character(n), char(n)
type BPChar Text
// Set converts from src to dst.
func (dst *BPChar) Set(src interface{}) error {
return (*Text)(dst).Set(src)
}
// Get returns underlying value
func (dst BPChar) Get() interface{} {
return (Text)(dst).Get()
}
// AssignTo assigns from src to dst.
func (src *BPChar) AssignTo(dst interface{}) error {
if src.Status == Present {
switch v := dst.(type) {
case *rune:
runes := []rune(src.String)
if len(runes) == 1 {
*v = runes[0]
return nil
}
}
}
return (*Text)(src).AssignTo(dst)
}
func (BPChar) PreferredResultFormat() int16 {
return TextFormatCode
}
func (dst *BPChar) DecodeText(ci *ConnInfo, src []byte) error {
return (*Text)(dst).DecodeText(ci, src)
}
func (dst *BPChar) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*Text)(dst).DecodeBinary(ci, src)
}
func (BPChar) PreferredParamFormat() int16 {
return TextFormatCode
}
func (src BPChar) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Text)(src).EncodeText(ci, buf)
}
func (src BPChar) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Text)(src).EncodeBinary(ci, buf)
}
// Scan implements the database/sql Scanner interface.
func (dst *BPChar) Scan(src interface{}) error {
return (*Text)(dst).Scan(src)
}
// Value implements the database/sql/driver Valuer interface.
func (src BPChar) Value() (driver.Value, error) {
return (Text)(src).Value()
}
func (src BPChar) MarshalJSON() ([]byte, error) {
return (Text)(src).MarshalJSON()
}
func (dst *BPChar) UnmarshalJSON(b []byte) error {
return (*Text)(dst).UnmarshalJSON(b)
}