mirror of
https://github.com/jackc/pgx.git
synced 2025-05-02 05:30:04 +00:00
This adds PostgreSQL numeric mapping to and from github.com/shopspring/decimal. Makes pgtype.NullAssignTo public as external types need this functionality. Begin extraction of pgtype testing functionality so it can easily be used by external types.
162 lines
2.8 KiB
Go
162 lines
2.8 KiB
Go
package pgtype
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
)
|
|
|
|
type Bool struct {
|
|
Bool bool
|
|
Status Status
|
|
}
|
|
|
|
func (dst *Bool) Set(src interface{}) error {
|
|
switch value := src.(type) {
|
|
case bool:
|
|
*dst = Bool{Bool: value, Status: Present}
|
|
case string:
|
|
bb, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*dst = Bool{Bool: bb, Status: Present}
|
|
default:
|
|
if originalSrc, ok := underlyingBoolType(src); ok {
|
|
return dst.Set(originalSrc)
|
|
}
|
|
return fmt.Errorf("cannot convert %v to Bool", value)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (dst *Bool) Get() interface{} {
|
|
switch dst.Status {
|
|
case Present:
|
|
return dst.Bool
|
|
case Null:
|
|
return nil
|
|
default:
|
|
return dst.Status
|
|
}
|
|
}
|
|
|
|
func (src *Bool) AssignTo(dst interface{}) error {
|
|
switch src.Status {
|
|
case Present:
|
|
switch v := dst.(type) {
|
|
case *bool:
|
|
*v = src.Bool
|
|
return nil
|
|
default:
|
|
if nextDst, retry := GetAssignToDstType(dst); retry {
|
|
return src.AssignTo(nextDst)
|
|
}
|
|
}
|
|
case Null:
|
|
return NullAssignTo(dst)
|
|
}
|
|
|
|
return fmt.Errorf("cannot decode %v into %T", src, dst)
|
|
}
|
|
|
|
func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Bool{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) != 1 {
|
|
return fmt.Errorf("invalid length for bool: %v", len(src))
|
|
}
|
|
|
|
*dst = Bool{Bool: src[0] == 't', Status: Present}
|
|
return nil
|
|
}
|
|
|
|
func (dst *Bool) DecodeBinary(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Bool{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) != 1 {
|
|
return fmt.Errorf("invalid length for bool: %v", len(src))
|
|
}
|
|
|
|
*dst = Bool{Bool: src[0] == 1, Status: Present}
|
|
return nil
|
|
}
|
|
|
|
func (src Bool) EncodeText(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
var buf []byte
|
|
if src.Bool {
|
|
buf = []byte{'t'}
|
|
} else {
|
|
buf = []byte{'f'}
|
|
}
|
|
|
|
_, err := w.Write(buf)
|
|
return false, err
|
|
}
|
|
|
|
func (src Bool) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
var buf []byte
|
|
if src.Bool {
|
|
buf = []byte{1}
|
|
} else {
|
|
buf = []byte{0}
|
|
}
|
|
|
|
_, err := w.Write(buf)
|
|
return false, err
|
|
}
|
|
|
|
// Scan implements the database/sql Scanner interface.
|
|
func (dst *Bool) Scan(src interface{}) error {
|
|
if src == nil {
|
|
*dst = Bool{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
switch src := src.(type) {
|
|
case bool:
|
|
*dst = Bool{Bool: src, Status: Present}
|
|
return nil
|
|
case string:
|
|
return dst.DecodeText(nil, []byte(src))
|
|
case []byte:
|
|
return dst.DecodeText(nil, src)
|
|
}
|
|
|
|
return fmt.Errorf("cannot scan %T", src)
|
|
}
|
|
|
|
// Value implements the database/sql/driver Valuer interface.
|
|
func (src Bool) Value() (driver.Value, error) {
|
|
switch src.Status {
|
|
case Present:
|
|
return src.Bool, nil
|
|
case Null:
|
|
return nil, nil
|
|
default:
|
|
return nil, errUndefined
|
|
}
|
|
}
|