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