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