diff --git a/pgtype/bit.go b/pgtype/bit.go new file mode 100644 index 00000000..f892cee5 --- /dev/null +++ b/pgtype/bit.go @@ -0,0 +1,37 @@ +package pgtype + +import ( + "database/sql/driver" +) + +type Bit Varbit + +func (dst *Bit) Set(src interface{}) error { + return (*Varbit)(dst).Set(src) +} + +func (dst *Bit) Get() interface{} { + return (*Varbit)(dst).Get() +} + +func (src *Bit) AssignTo(dst interface{}) error { + return (*Varbit)(src).AssignTo(dst) +} + +func (dst *Bit) DecodeBinary(ci *ConnInfo, src []byte) error { + return (*Varbit)(dst).DecodeBinary(ci, src) +} + +func (src *Bit) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) { + return (*Varbit)(src).EncodeBinary(ci, buf) +} + +// Scan implements the database/sql Scanner interface. +func (dst *Bit) Scan(src interface{}) error { + return (*Varbit)(dst).Scan(src) +} + +// Value implements the database/sql/driver Valuer interface. +func (src *Bit) Value() (driver.Value, error) { + return (*Varbit)(src).Value() +} diff --git a/pgtype/bit_test.go b/pgtype/bit_test.go new file mode 100644 index 00000000..19492bc9 --- /dev/null +++ b/pgtype/bit_test.go @@ -0,0 +1,25 @@ +package pgtype_test + +import ( + "testing" + + "github.com/jackc/pgx/pgtype" + "github.com/jackc/pgx/pgtype/testutil" +) + +func TestBitTranscode(t *testing.T) { + testutil.TestSuccessfulTranscode(t, "bit(40)", []interface{}{ + &pgtype.Varbit{Bytes: []byte{0, 0, 0, 0, 0}, Len: 40, Status: pgtype.Present}, + &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Status: pgtype.Present}, + &pgtype.Varbit{Status: pgtype.Null}, + }) +} + +func TestBitNormalize(t *testing.T) { + testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{ + { + SQL: "select B'111111111'", + Value: &pgtype.Bit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present}, + }, + }) +} diff --git a/pgtype/pgtype.go b/pgtype/pgtype.go index 83311cf4..f7a1a300 100644 --- a/pgtype/pgtype.go +++ b/pgtype/pgtype.go @@ -227,6 +227,7 @@ func init() { "_uuid": &UUIDArray{}, "_varchar": &VarcharArray{}, "aclitem": &ACLItem{}, + "bit": &Bit{}, "bool": &Bool{}, "box": &Box{}, "bytea": &Bytea{},