-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
97 lines (86 loc) · 2.38 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package timex
import (
"database/sql/driver"
"fmt"
"time"
)
// Scan implements the sql.Scanner interface.
func (d *Date) Scan(value interface{}) (err error) {
switch v := value.(type) {
case []byte:
*d, err = ParseDate(RFC3339Date, string(v))
case string:
*d, err = ParseDate(RFC3339Date, v)
case time.Time:
*d = DateFromTime(v)
default:
err = fmt.Errorf("unsupported type %T", value)
}
return err
}
// Value implements the driver.Valuer interface.
func (d Date) Value() (driver.Value, error) {
return d.Format(RFC3339Date), nil
}
// NullDate represents a specific day in Gregorian calendar that may be null.
// NullDate implements the sql.Scanner interface, so it can be used as a scan destination, similar to sql.NullString.
type NullDate struct {
Date Date
Valid bool // Valid is true if Date is not NULL.
}
// Scan implements the sql.Scanner interface.
func (d *NullDate) Scan(value interface{}) error {
if value == nil {
d.Date, d.Valid = Date{}, false
return nil
}
d.Valid = true
return d.Date.Scan(value)
}
// Value implements the driver.Valuer interface.
func (d NullDate) Value() (driver.Value, error) {
if !d.Valid {
return nil, nil
}
return d.Date.Value()
}
// Scan implements the sql.Scanner interface.
func (t *TimeOfDay) Scan(value interface{}) (err error) {
switch v := value.(type) {
case []byte:
*t, err = ParseTimeOfDay(RFC3339Time, string(v))
case string:
*t, err = ParseTimeOfDay(RFC3339Time, v)
case time.Time:
*t = TimeOfDayFromTime(v)
default:
err = fmt.Errorf("unsupported type %T", value)
}
return err
}
// Value implements the driver.Valuer interface.
func (t TimeOfDay) Value() (driver.Value, error) {
return t.Format(RFC3339Time), nil
}
// NullTimeOfDay represents a specific time in a day that may be null.
// NullTimeOfDay implements the sql.Scanner interface, so it can be used as a scan destination, similar to sql.NullString.
type NullTimeOfDay struct {
TimeOfDay TimeOfDay
Valid bool // Valid is true if TimeOfDay is not NULL.
}
// Scan implements the sql.Scanner interface.
func (t *NullTimeOfDay) Scan(value interface{}) error {
if value == nil {
t.TimeOfDay, t.Valid = TimeOfDay{}, false
return nil
}
t.Valid = true
return t.TimeOfDay.Scan(value)
}
// Value implements the driver.Valuer interface.
func (t NullTimeOfDay) Value() (driver.Value, error) {
if !t.Valid {
return nil, nil
}
return t.TimeOfDay.Value()
}