-
Notifications
You must be signed in to change notification settings - Fork 9
/
sql.go
78 lines (71 loc) · 1.61 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package synchro
import (
"database/sql/driver"
"fmt"
"time"
"github.com/Code-Hex/synchro/iso8601"
)
// Scan implements the sql.Scanner interface.
func (t *Time[T]) Scan(src any) error {
if src == nil {
*t = Time[T]{} // zero value
return nil
}
var tz T
switch s := src.(type) {
case time.Time:
*t = In[T](s)
return nil
case string:
parsed, err := iso8601.ParseDateTime[string](
s,
iso8601.WithTimeDesignators(' '),
iso8601.WithInLocation(tz.Location()),
)
if err != nil {
return err
}
*t = In[T](parsed)
return nil
case []byte:
parsed, err := iso8601.ParseDateTime[[]byte](
s,
iso8601.WithTimeDesignators(' '),
iso8601.WithInLocation(tz.Location()),
)
if err != nil {
return err
}
*t = In[T](parsed)
return nil
default:
return fmt.Errorf("unknown type of: %T", s)
}
}
// Value implements the driver.Valuer interface.
func (t Time[T]) Value() (driver.Value, error) {
return t.tm, nil
}
// NullTime represents a Time[T] that may be null.
// NullTime implements the sql.Scanner interface so
// it can be used as a scan destination, similar to sql.NullString.
type NullTime[T TimeZone] struct {
Time Time[T]
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the sql.Scanner interface.
func (n *NullTime[T]) Scan(src any) error {
if src == nil {
n.Time, n.Valid = Time[T]{}, false
return nil
}
n.Valid = true // almost the same behavior as sql.NullTime
return n.Time.Scan(src)
}
// Value implements the driver.Valuer interface.
func (n NullTime[T]) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time.tm, nil
}