-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.go
More file actions
51 lines (47 loc) · 1.23 KB
/
Copy pathsql.go
File metadata and controls
51 lines (47 loc) · 1.23 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
package uuid
import (
"database/sql/driver"
"fmt"
)
// Compile-time checks that UUID implements the database interfaces.
var (
_ driver.Valuer = UUID{}
_ interface{ Scan(any) error } = (*UUID)(nil)
)
// Value implements driver.Valuer, allowing a UUID to be used directly as a SQL
// query argument. It is stored as its canonical string representation.
func (u UUID) Value() (driver.Value, error) {
return u.String(), nil
}
// Scan implements sql.Scanner, allowing a UUID to be read from a database. It
// accepts a string, a []byte holding either the textual representation or the
// raw 16 bytes, and nil (decoded as the Nil UUID).
func (u *UUID) Scan(src any) error {
switch v := src.(type) {
case nil:
*u = Nil
return nil
case string:
parsed, err := Parse(v)
if err != nil {
return err
}
*u = parsed
return nil
case []byte:
// A 16 byte slice is treated as the raw UUID; anything else is
// interpreted as text (e.g. a driver returning the string form as bytes).
if len(v) == TOTAL_BYTES {
copy(u[:], v)
return nil
}
parsed, err := ParseBytes(v)
if err != nil {
return err
}
*u = parsed
return nil
default:
return fmt.Errorf("%w: cannot scan %T into UUID", ErrInvalidFormat, src)
}
}