Add support for bit type

pull/356/head
Jack Christensen 2017-11-18 21:13:34 -06:00
parent 6c3e88bb82
commit 5e08a4a5f1
3 changed files with 63 additions and 0 deletions

37
pgtype/bit.go Normal file
View File

@ -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()
}

25
pgtype/bit_test.go Normal file
View File

@ -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},
},
})
}

View File

@ -227,6 +227,7 @@ func init() {
"_uuid": &UUIDArray{},
"_varchar": &VarcharArray{},
"aclitem": &ACLItem{},
"bit": &Bit{},
"bool": &Bool{},
"box": &Box{},
"bytea": &Bytea{},