forked from go-jet/jet
/
null_types.go
175 lines (148 loc) · 3.42 KB
/
null_types.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package internal
import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/ag5/jet/v2/internal/utils/min"
"reflect"
"strconv"
"time"
)
// NullBool struct
type NullBool struct {
sql.NullBool
}
// Scan implements the Scanner interface.
func (nb *NullBool) Scan(value interface{}) error {
switch v := value.(type) {
case bool:
nb.Bool, nb.Valid = v, true
case int8, int16, int32, int64, int:
intVal := reflect.ValueOf(v).Int()
if intVal != 0 && intVal != 1 {
return fmt.Errorf("can't assign %T(%d) to bool", value, value)
}
nb.Bool = intVal == 1
nb.Valid = true
case uint8, uint16, uint32, uint64, uint:
uintVal := reflect.ValueOf(v).Uint()
if uintVal != 0 && uintVal != 1 {
return fmt.Errorf("can't assign %T(%d) to bool", value, value)
}
nb.Bool = uintVal == 1
nb.Valid = true
default:
return nb.NullBool.Scan(value)
}
return nil
}
// NullTime struct
type NullTime struct {
sql.NullTime
}
// Scan implements the Scanner interface.
func (nt *NullTime) Scan(value interface{}) error {
err := nt.NullTime.Scan(value)
if err == nil {
return nil
}
// Some of the drivers (pgx, mysql) are not parsing all of the time formats(date, time with time zone,...) and are just forwarding string value.
// At this point we try to parse those values using some of the predefined formats
nt.Time, nt.Valid = tryParseAsTime(value)
if !nt.Valid {
return fmt.Errorf("can't scan time.Time from %q", value)
}
return nil
}
var formats = []string{
"2006-01-02 15:04:05-07:00", // sqlite
"2006-01-02 15:04:05.999999", // go-sql-driver/mysql
"15:04:05-07", // pgx
"15:04:05.999999", // pgx
}
func tryParseAsTime(value interface{}) (time.Time, bool) {
var timeStr string
switch v := value.(type) {
case string:
timeStr = v
case []byte:
timeStr = string(v)
case int64:
return time.Unix(v, 0), true // sqlite
default:
return time.Time{}, false
}
for _, format := range formats {
formatLen := min.Int(len(format), len(timeStr))
t, err := time.Parse(format[:formatLen], timeStr)
if err != nil {
continue
}
return t, true
}
return time.Time{}, false
}
// NullUInt64 struct
type NullUInt64 struct {
UInt64 uint64
Valid bool
}
// Scan implements the Scanner interface.
func (n *NullUInt64) Scan(value interface{}) error {
var stringValue string
switch v := value.(type) {
case nil:
n.Valid = false
return nil
case int64:
n.UInt64, n.Valid = uint64(v), true
return nil
case uint64:
n.UInt64, n.Valid = v, true
return nil
case int32:
n.UInt64, n.Valid = uint64(v), true
return nil
case uint32:
n.UInt64, n.Valid = uint64(v), true
return nil
case int16:
n.UInt64, n.Valid = uint64(v), true
return nil
case uint16:
n.UInt64, n.Valid = uint64(v), true
return nil
case int8:
n.UInt64, n.Valid = uint64(v), true
return nil
case uint8:
n.UInt64, n.Valid = uint64(v), true
return nil
case int:
n.UInt64, n.Valid = uint64(v), true
return nil
case uint:
n.UInt64, n.Valid = uint64(v), true
return nil
case []byte:
stringValue = string(v)
case string:
stringValue = v
default:
return fmt.Errorf("can't scan uint64 from %v", value)
}
uintV, err := strconv.ParseUint(stringValue, 10, 64)
if err != nil {
return err
}
n.UInt64 = uintV
n.Valid = true
return nil
}
// Value implements the driver Valuer interface.
func (n NullUInt64) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.UInt64, nil
}