File: int4.go

package info (click to toggle)
golang-github-jackc-pgtype 1.10.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,656 kB
  • sloc: sh: 32; makefile: 4
file content (90 lines) | stat: -rw-r--r-- 1,545 bytes parent folder | download
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 Int4 int32

func (dst *Int4) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
	var nullable pgtype.Int4
	err := nullable.DecodeText(ci, src)
	if err != nil {
		return err
	}

	if nullable.Status == pgtype.Present {
		*dst = Int4(nullable.Int)
	} else {
		*dst = 0
	}

	return nil
}

func (dst *Int4) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
	var nullable pgtype.Int4
	err := nullable.DecodeBinary(ci, src)
	if err != nil {
		return err
	}

	if nullable.Status == pgtype.Present {
		*dst = Int4(nullable.Int)
	} else {
		*dst = 0
	}

	return nil
}

func (src Int4) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
	if src == 0 {
		return nil, nil
	}

	nullable := pgtype.Int4{
		Int:    int32(src),
		Status: pgtype.Present,
	}

	return nullable.EncodeText(ci, buf)
}

func (src Int4) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
	if src == 0 {
		return nil, nil
	}

	nullable := pgtype.Int4{
		Int:    int32(src),
		Status: pgtype.Present,
	}

	return nullable.EncodeBinary(ci, buf)
}

// Scan implements the database/sql Scanner interface.
func (dst *Int4) Scan(src interface{}) error {
	if src == nil {
		*dst = 0
		return nil
	}

	var nullable pgtype.Int4
	err := nullable.Scan(src)
	if err != nil {
		return err
	}

	*dst = Int4(nullable.Int)

	return nil
}

// Value implements the database/sql/driver Valuer interface.
func (src Int4) Value() (driver.Value, error) {
	return pgtype.EncodeValueText(src)
}