-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuuid-null.go
72 lines (60 loc) · 1.71 KB
/
uuid-null.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
63
64
65
66
67
68
69
70
71
72
package pgtypes
import (
"database/sql/driver"
"fmt"
"github.com/jackc/pgx"
)
// NullUUID represents an UUID that may be NULL.
// NullUUID implements the pgx.PgxScanner and pgx.Encoder interfaces so it may be used both as an argument to Query[Row] and a destination for ScanPgx.
//
// If Valid is false then the value is NULL.
type NullUUID struct {
UUID UUID
Valid bool // Valid is true if UUID is not NULL
}
// ScanPgx implements the pgx.PgxScanner interface.
func (u *NullUUID) ScanPgx(vr *pgx.ValueReader) error {
if vr.Type().DataType != pgx.UuidOid {
return pgx.SerializationError(fmt.Sprintf("NullUUID.ScanPgx cannot decode %s (OID %d)", vr.Type().DataTypeName, vr.Type().DataType))
}
if vr.Len() == -1 {
u.UUID, u.Valid = zeroUUID, false
return nil
}
u.Valid = true
return u.UUID.ScanPgx(vr)
}
// FormatCode implements the pgx.Encoder interface.
func (u NullUUID) FormatCode() int16 { return pgx.BinaryFormatCode }
// Encode implements the pgx.Encoder interface.
func (u NullUUID) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
if oid != pgx.UuidOid {
return pgx.SerializationError(fmt.Sprintf("NullUUID.Encode cannot encode into OID %d", oid))
}
if !u.Valid {
w.WriteInt32(-1)
return nil
}
return u.UUID.Encode(w, oid)
}
// Nullable returns valid NullUUID with UUID u.
func (u UUID) Nullable() NullUUID {
return NullUUID{u, true}
}
// Scan implements the sql.Scanner interface.
func (u *NullUUID) Scan(src interface{}) (err error) {
if src == nil {
*u = NullUUID{}
return nil
}
err = u.UUID.Scan(src)
u.Valid = err == nil
return
}
// Value implements the driver.Valuer interface.
func (u NullUUID) Value() (driver.Value, error) {
if !u.Valid {
return nil, nil
}
return u.UUID.Value()
}