-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsql.go
More file actions
37 lines (33 loc) · 921 Bytes
/
sql.go
File metadata and controls
37 lines (33 loc) · 921 Bytes
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
package typeid
import (
"database/sql/driver"
"fmt"
)
// For nullable TypeID columns, use sql.Null[TypeID].
// Scan implements the sql.Scanner interface so the TypeIDs can be read from
// databases transparently. Currently database types that map to string are
// supported.
func (tid *TypeID) Scan(src any) error {
switch obj := src.(type) {
case nil:
return &validationError{
Message: "cannot scan NULL into TypeID",
}
case string:
if obj == "" {
return &validationError{
Message: "cannot scan empty string into TypeID",
}
}
return tid.UnmarshalText([]byte(obj))
default:
return &validationError{
Message: fmt.Sprintf("unsupported scan type %T", obj),
}
}
}
// Value implements the sql.Valuer interface so that TypeIDs can be written
// to databases transparently. Currently, TypeIDs map to strings.
func (tid TypeID) Value() (driver.Value, error) {
return tid.String(), nil
}