-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_null.go
75 lines (66 loc) · 1.44 KB
/
sql_null.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
65
66
67
68
69
70
71
72
73
74
75
package und
import (
"database/sql"
"database/sql/driver"
)
var (
_ sql.Scanner = (*SqlNull[any])(nil)
_ driver.Valuer = SqlNull[any]{}
)
// SqlNull[T] adapts Und[T] to sql.Scanner and driver.Valuer.
type SqlNull[T any] struct {
Und[T]
}
// Scan implements sql.Scanner.
//
// If T or *T implements sql.Scanner, the implementation is used.
// Otherwise, SqlNull[T] falls back to sql.Null[T] as sql.Scanner.
func (n *SqlNull[T]) Scan(src any) error {
if src == nil {
n.Und = Null[T]()
return nil
}
var (
t T
scanner sql.Scanner
err error
)
scanner, _ = any(t).(sql.Scanner)
if scanner == nil {
scanner, _ = any(&t).(sql.Scanner)
}
if scanner != nil {
err = scanner.Scan(src)
if err != nil {
return err
}
n.Und = Defined(t)
return nil
}
var null sql.Null[T]
err = null.Scan(src)
if err != nil {
return err
}
n.Und = FromSqlNull(null)
return nil
}
// Value implements driver.Valuer.
//
// If T or *T implements driver.Valuer, the implementation is used.
// In this respect, T should not be a pointer type or Und[T] should not store nil value.
// Otherwise, SqlNull[T] falls back to sql.Null[T] as driver.Valuer.
func (n SqlNull[T]) Value() (driver.Value, error) {
if !n.Und.IsDefined() {
return nil, nil
}
v := n.Und.Value()
valuer, _ := any(v).(driver.Valuer)
if valuer == nil {
valuer, _ = any(&v).(driver.Valuer)
}
if valuer != nil {
return valuer.Value()
}
return n.Und.SqlNull().Value()
}