-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsql_driver.go
More file actions
34 lines (31 loc) · 836 Bytes
/
sql_driver.go
File metadata and controls
34 lines (31 loc) · 836 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
package optional
import (
"database/sql"
"database/sql/driver"
)
// Scan assigns a value from a database driver.
// This method is required from database/sql.Scanner interface.
func (o *Option[T]) Scan(src any) error {
// The detour through sql.Null[T] allows us to access the standard rules for
// assigning scanned values into builtin types and std types like *sql.Rows,
// which are not exported from std directly.
var v sql.Null[T]
err := v.Scan(src)
if err != nil {
return err
}
if v.Valid {
*o = Some[T](v.V)
} else {
*o = None[T]()
}
return nil
}
// Value returns a driver Value.
// This method is required from database/sql/driver.Valuer interface.
func (o Option[T]) Value() (driver.Value, error) {
if o.IsNone() {
return nil, nil
}
return driver.DefaultParameterConverter.ConvertValue(o.Unwrap())
}