-
Notifications
You must be signed in to change notification settings - Fork 6
/
sql.go
64 lines (52 loc) · 1.49 KB
/
sql.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
package uuid
import (
"database/sql/driver"
"fmt"
"reflect"
)
// ErrInvalidType occurs when *UUID.Scan() does not receive a string.
type ErrInvalidType struct {
Type reflect.Type
}
func (e ErrInvalidType) Error() string {
t := "<nil>"
if e.Type != nil {
t = e.Type.String()
}
return fmt.Sprintf("uuid Scan(): invalid type '%s', expected string or []byte.", t)
}
// Scan scans a uuid from the given interface instance.
// If scanning fails the state of the UUID is undetermined.
func (u *UUID) Scan(val interface{}) error {
if s, ok := val.(string); ok {
return u.SetString(s)
}
if b, ok := val.([]byte); ok {
return u.ReadBytes(b)
}
return &ErrInvalidType{reflect.TypeOf(val)}
}
// Value gives the database driver representation of the UUID.
func (u UUID) Value() (driver.Value, error) {
// The return here causes a second allocation because of the driver.Value interface{} box
return u.String(), nil
}
// Scan scans a uuid or null from the given value.
// If the supplied value is nil, Valid will be set to false and the
// UUID will be zeroed.
func (nu *NullUUID) Scan(val interface{}) error {
if val == nil {
nu.UUID, nu.Valid = [16]byte{}, false
return nil
}
nu.Valid = true
return nu.UUID.Scan(val)
}
// Value gives the database driver representation of the UUID or NULL.
func (nu NullUUID) Value() (driver.Value, error) {
if !nu.Valid {
return nil, nil
}
// The return here causes a second allocation because of the driver.Value interface{} box
return nu.UUID.String(), nil
}