-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.go
More file actions
84 lines (73 loc) · 1.72 KB
/
sql.go
File metadata and controls
84 lines (73 loc) · 1.72 KB
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
package usid
import (
"database/sql"
"database/sql/driver"
"encoding"
"encoding/json"
)
// NullID can be used with the standard sql package to represent an
// ID value that can be NULL in the database.
type NullID struct {
ID ID
Valid bool
}
// Compile-time interface checks for NullID
var (
_ driver.Valuer = NullID{}
_ sql.Scanner = (*NullID)(nil)
_ json.Marshaler = NullID{}
_ json.Unmarshaler = (*NullID)(nil)
_ encoding.TextMarshaler = NullID{}
_ encoding.TextUnmarshaler = (*NullID)(nil)
)
// Value implements the driver.Valuer interface.
func (n NullID) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.ID.Value()
}
// Scan implements the sql.Scanner interface.
func (n *NullID) Scan(src interface{}) error {
if src == nil {
n.ID, n.Valid = Nil, false
return nil
}
n.Valid = true
return n.ID.Scan(src)
}
var nullJSON = []byte("null")
// MarshalJSON marshals the NullID as null or the nested ID as a string.
func (n NullID) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullJSON, nil
}
return n.ID.MarshalJSON()
}
// UnmarshalJSON unmarshals a NullID.
func (n *NullID) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
n.ID, n.Valid = Nil, false
return nil
}
err := n.ID.UnmarshalJSON(b)
n.Valid = (err == nil)
return err
}
// MarshalText implements encoding.TextMarshaler.
func (n NullID) MarshalText() ([]byte, error) {
if !n.Valid {
return nil, nil
}
return n.ID.MarshalText()
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (n *NullID) UnmarshalText(b []byte) error {
if len(b) == 0 {
n.ID, n.Valid = Nil, false
return nil
}
err := n.ID.UnmarshalText(b)
n.Valid = (err == nil)
return err
}