-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
64 lines (58 loc) · 1.13 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
/*
* @file: sql.go
* @author: Jorge Quitério
* @copyright (c) 2022 Jorge Quitério
* @license: MIT
*/
package uuid
import (
"database/sql/driver"
"fmt"
)
// Value implements the driver.Valuer interface.
func (u UUID) Value() (driver.Value, error) {
return u.String(), nil
}
// Scan implements the sql.Scanner interface.
func (u *UUID) Scan(src interface{}) error {
switch src := src.(type) {
case UUID:
*u = src
return nil
case nil:
return nil
case []byte:
if len(src) == 0 {
return nil
}
copy((*u)[:], src)
case string:
if src == "" {
return nil
}
uuid := Parse(src)
if uuid == Nil {
return fmt.Errorf("uuid.Scan: invalid UUID: %s", src)
}
*u = uuid
default:
return fmt.Errorf("uuid.Scan: invalid type %T", src)
}
return nil
}
// Value implements the driver.Valuer interface.
func (nu NullUUID) Value() (driver.Value, error) {
if !nu.Valid {
return nil, nil
}
return nu.UUID.Value()
}
// Scan implements the sql.Scanner interface.
func (nu *NullUUID) Scan(src interface{}) error {
if src == nil {
nu.UUID, nu.Valid = Nil, false
return nil
}
nu.Valid = true
return nu.UUID.Scan(src)
}