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