-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
54 lines (48 loc) · 1.17 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
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(RFC3339, string(v))
case string:
*d, err = ParseDate(RFC3339, 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(RFC3339), nil
}
// NullDate represents a Date that may be null.
// NullDate implements the sql.Scanner interface, so it can be used as a scan destination, similar to sql.NullString.
//
// swagger:strfmt date
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()
}