mirror of https://github.com/jackc/pgx.git
All tests passing
parent
1516a0d8db
commit
93cc21199f
|
@ -2,70 +2,32 @@ package zeronull
|
|||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Int2 int16
|
||||
|
||||
func (dst *Int2) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int2
|
||||
err := nullable.DecodeText(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int2) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int2(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
if n < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
if n > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
*dst = Int2(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Int2) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int2
|
||||
err := nullable.DecodeBinary(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int2(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Int2) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int2{
|
||||
Int: int16(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeText(ci, buf)
|
||||
}
|
||||
|
||||
func (src Int2) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int2{
|
||||
Int: int16(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeBinary(ci, buf)
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int2) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
|
@ -86,5 +48,8 @@ func (dst *Int2) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int2) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue