1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package zeronull
import (
"database/sql/driver"
"github.com/jackc/pgtype"
)
type Text string
func (dst *Text) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.Text
err := nullable.DecodeText(ci, src)
if err != nil {
return err
}
if nullable.Status == pgtype.Present {
*dst = Text(nullable.String)
} else {
*dst = Text("")
}
return nil
}
func (dst *Text) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.Text
err := nullable.DecodeBinary(ci, src)
if err != nil {
return err
}
if nullable.Status == pgtype.Present {
*dst = Text(nullable.String)
} else {
*dst = Text("")
}
return nil
}
func (src Text) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if src == Text("") {
return nil, nil
}
nullable := pgtype.Text{
String: string(src),
Status: pgtype.Present,
}
return nullable.EncodeText(ci, buf)
}
func (src Text) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if src == Text("") {
return nil, nil
}
nullable := pgtype.Text{
String: string(src),
Status: pgtype.Present,
}
return nullable.EncodeBinary(ci, buf)
}
// Scan implements the database/sql Scanner interface.
func (dst *Text) Scan(src interface{}) error {
if src == nil {
*dst = Text("")
return nil
}
var nullable pgtype.Text
err := nullable.Scan(src)
if err != nil {
return err
}
*dst = Text(nullable.String)
return nil
}
// Value implements the database/sql/driver Valuer interface.
func (src Text) Value() (driver.Value, error) {
return pgtype.EncodeValueText(src)
}
|