forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.go
88 lines (77 loc) · 1.57 KB
/
timestamp.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
package go_ora
import (
"database/sql/driver"
"encoding/json"
"errors"
"time"
)
type TimeStamp time.Time
func (val *TimeStamp) Value() (driver.Value, error) {
return val, nil
}
func (val *TimeStamp) Scan(value interface{}) error {
switch temp := value.(type) {
case TimeStamp:
*val = temp
case *TimeStamp:
*val = *temp
case time.Time:
*val = TimeStamp(temp)
case *time.Time:
*val = TimeStamp(*temp)
default:
return errors.New("go-ora: TimeStamp column type require time.Time value")
}
return nil
}
func (val TimeStamp) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(val))
}
func (val *TimeStamp) UnmarshalJSON(data []byte) error {
var temp time.Time
err := json.Unmarshal(data, &temp)
if err != nil {
return err
}
*val = TimeStamp(temp)
return nil
}
type NullTimeStamp struct {
TimeStamp TimeStamp
Valid bool
}
func (val NullTimeStamp) Value() (driver.Value, error) {
if val.Valid {
return val.TimeStamp.Value()
} else {
return nil, nil
}
}
func (val *NullTimeStamp) Scan(value interface{}) error {
if value == nil {
val.Valid = false
return nil
}
val.Valid = true
return val.TimeStamp.Scan(value)
}
func (val NullTimeStamp) MarshalJSON() ([]byte, error) {
if val.Valid {
return json.Marshal(time.Time(val.TimeStamp))
}
return json.Marshal(nil)
}
func (val *NullTimeStamp) UnmarshalJSON(data []byte) error {
var temp = new(time.Time)
err := json.Unmarshal(data, temp)
if err != nil {
return err
}
if temp == nil {
val.Valid = false
} else {
val.Valid = true
val.TimeStamp = TimeStamp(*temp)
}
return nil
}