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