-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
62 lines (47 loc) · 1.04 KB
/
uuid.go
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
package zeronull
import (
"database/sql/driver"
"github.com/jackc/pgx/v5/pgtype"
)
type UUID [16]byte
func (UUID) SkipUnderlyingTypePlan() {}
// ScanUUID implements the UUIDScanner interface.
func (u *UUID) ScanUUID(v pgtype.UUID) error {
if !v.Valid {
*u = UUID{}
return nil
}
*u = UUID(v.Bytes)
return nil
}
func (u UUID) UUIDValue() (pgtype.UUID, error) {
if u == (UUID{}) {
return pgtype.UUID{}, nil
}
return pgtype.UUID{Bytes: u, Valid: true}, nil
}
// Scan implements the database/sql Scanner interface.
func (u *UUID) Scan(src any) error {
if src == nil {
*u = UUID{}
return nil
}
var nullable pgtype.UUID
err := nullable.Scan(src)
if err != nil {
return err
}
*u = UUID(nullable.Bytes)
return nil
}
// Value implements the database/sql/driver Valuer interface.
func (u UUID) Value() (driver.Value, error) {
if u == (UUID{}) {
return nil, nil
}
buf, err := pgtype.UUIDCodec{}.PlanEncode(nil, pgtype.UUIDOID, pgtype.TextFormatCode, u).Encode(u, nil)
if err != nil {
return nil, err
}
return string(buf), nil
}