/
sql.go
51 lines (44 loc) · 1.02 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
package uuid
import (
"database/sql/driver"
satori "github.com/satori/go.uuid"
)
// Value implements the driver.Valuer interface.
func (uuid UUID) Value() (driver.Value, error) {
return satori.UUID(uuid).Value()
}
// Scan implements the sql.Scanner interface.
func (uuid *UUID) Scan(src interface{}) error {
id := satori.UUID(*uuid)
idPtr := &id
err := idPtr.Scan(src)
if err != nil {
return err
}
*uuid = UUID(id)
return nil
}
// NullUUID can be used with the standard sql package to represent a
// UUID value that can be NULL in the database
type NullUUID struct {
UUID UUID
Valid bool
}
// Value implements the driver.Valuer interface.
func (u NullUUID) Value() (driver.Value, error) {
if !u.Valid {
return nil, nil
}
// Delegate to UUID Value function
return u.UUID.Value()
}
// Scan implements the sql.Scanner interface.
func (u *NullUUID) Scan(src interface{}) error {
if src == nil {
u.UUID, u.Valid = Nil, false
return nil
}
// Delegate to UUID Scan function
u.Valid = true
return u.UUID.Scan(src)
}