-
Notifications
You must be signed in to change notification settings - Fork 335
/
types.go
333 lines (269 loc) · 8.02 KB
/
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package drivers
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/google/uuid"
"github.com/modern-go/reflect2"
"github.com/spf13/cast"
)
type (
Type interface {
MakeScanBuffer() any
Decode(any) (any, bool, error)
Encode(any) (driver.Value, error)
}
// @todo makes sense to rethink this strategy
// we do not need to have 1:1 type pairings with the pkg/dal
// it makes sense to define a few most common ones here (in the rdbms/drivers)
// and introduce per/driver exceptions to handle implementation specific things
// like MySQLs lack of BOOLEAN that is replaced with TINYINT(1)
TypeID struct{ *dal.TypeID }
TypeRef struct{ *dal.TypeRef }
TypeTimestamp struct{ *dal.TypeTimestamp }
TypeTime struct{ *dal.TypeTime }
TypeDate struct{ *dal.TypeDate }
TypeNumber struct{ *dal.TypeNumber }
TypeText struct{ *dal.TypeText }
TypeBoolean struct{ *dal.TypeBoolean }
TypeEnum struct{ *dal.TypeEnum }
TypeGeometry struct{ *dal.TypeGeometry }
TypeJSON struct{ *dal.TypeJSON }
TypeBlob struct{ *dal.TypeBlob }
TypeUUID struct{ *dal.TypeUUID }
)
// TypeWrap wraps type from data package
func TypeWrap(dt dal.Type) Type {
switch c := dt.(type) {
case *dal.TypeID:
return &TypeID{c}
case *dal.TypeRef:
return &TypeRef{c}
case *dal.TypeTimestamp:
return &TypeTimestamp{c}
case *dal.TypeTime:
return &TypeTime{c}
case *dal.TypeDate:
return &TypeDate{c}
case *dal.TypeNumber:
return &TypeNumber{c}
case *dal.TypeText:
return &TypeText{c}
case *dal.TypeBoolean:
return &TypeBoolean{c}
case *dal.TypeEnum:
return &TypeEnum{c}
case *dal.TypeGeometry:
return &TypeGeometry{c}
case *dal.TypeJSON:
return &TypeJSON{c}
case *dal.TypeBlob:
return &TypeBlob{c}
case *dal.TypeUUID:
return &TypeUUID{c}
}
if dt == nil {
panic(fmt.Sprintf("attribute type not set (nil)"))
}
panic(fmt.Sprintf("type implementation missing: %s", dt.Type()))
}
func (*TypeID) MakeScanBuffer() any { return new(ID) }
func (*TypeRef) MakeScanBuffer() any { return new(ID) }
func (*TypeTimestamp) MakeScanBuffer() any { return new(sql.NullTime) }
func (*TypeTime) MakeScanBuffer() any { return new(sql.NullString) }
func (*TypeDate) MakeScanBuffer() any { return new(sql.NullTime) }
func (*TypeNumber) MakeScanBuffer() any { return new(sql.NullString) }
func (*TypeText) MakeScanBuffer() any { return new(sql.NullString) }
func (*TypeBoolean) MakeScanBuffer() any { return new(sql.NullBool) }
func (*TypeEnum) MakeScanBuffer() any { return new(sql.NullString) }
func (*TypeGeometry) MakeScanBuffer() any { return new(sql.RawBytes) }
func (*TypeJSON) MakeScanBuffer() any { return new(sql.RawBytes) }
func (*TypeBlob) MakeScanBuffer() any { return new(sql.RawBytes) }
func (t *TypeID) Decode(raw any) (any, bool, error) {
dec, is := raw.(*ID)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for ID", raw)
}
return dec.ID, dec.Valid, nil
}
func (t *TypeID) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeRef) Decode(raw any) (any, bool, error) {
dec, is := raw.(*ID)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Ref", raw)
}
return dec.ID, dec.Valid, nil
}
func (t *TypeRef) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeTimestamp) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullTime)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Timestamp", raw)
}
if dec.Valid {
return dec.Time.UTC().Format(TimestampLayout(t.Timezone, t.Precision)), dec.Valid, nil
}
return nil, false, nil
}
func (t *TypeTimestamp) Encode(val any) (driver.Value, error) {
if reflect2.IsNil(val) {
return nil, nil
}
return cast.ToTimeE(val)
}
func (t *TypeTime) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullString)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Time", raw)
}
if !dec.Valid {
return time.Time{}, false, nil
}
parsed, err := time.Parse(TimeLayout(t.Timezone, t.Precision), dec.String)
if err != nil {
return time.Time{}, false, err
}
// @todo should we gracefully handle other combinations of time&precision?
// maybe with Strict flag?
return parsed.Format(TimeLayout(t.Timezone, t.Precision)), true, nil
}
func (t *TypeTime) Encode(val any) (driver.Value, error) {
if reflect2.IsNil(val) {
return nil, nil
}
return cast.ToTimeE(val)
}
func (t *TypeDate) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullTime)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Date", raw)
}
return dec.Time.Format(DateLayout), dec.Valid, nil
}
func (t *TypeDate) Encode(val any) (driver.Value, error) {
if reflect2.IsNil(val) {
return nil, nil
}
return cast.ToTimeE(val)
}
func (t *TypeNumber) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullString)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Number", raw)
}
return dec.String, dec.Valid, nil
}
func (t *TypeNumber) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeText) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullString)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Text", raw)
}
return dec.String, dec.Valid, nil
}
func (t *TypeText) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeBoolean) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullBool)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Boolean", raw)
}
return dec.Bool, dec.Valid, nil
}
func (t *TypeBoolean) Encode(val any) (driver.Value, error) {
if val == nil {
return nil, nil
}
return cast.ToBool(val), nil
}
func (t *TypeEnum) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullString)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Enum", raw)
}
return dec.String, dec.Valid, nil
}
func (t *TypeEnum) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeGeometry) Decode(raw any) (any, bool, error) {
bb, is := raw.(*sql.RawBytes)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Geometry", raw)
}
return []byte(*bb), bb != nil, nil
}
func (t *TypeGeometry) Encode(val any) (driver.Value, error) {
return val, nil
}
func (t *TypeJSON) Decode(raw any) (any, bool, error) {
bb, is := raw.(*sql.RawBytes)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for JSON", raw)
}
return []byte(*bb), bb != nil, nil
}
func (t *TypeJSON) Encode(val any) (driver.Value, error) {
switch c := val.(type) {
case driver.Valuer:
// does the value type know how to encode itself for the DB?
return c.Value()
// These types are native to driver.Value
case int64, float64, bool, []byte, string, time.Time:
return c, nil
case json.Marshaler:
// does the value type know how to encode itself as JSON?
return c.MarshalJSON()
default:
// Last resort - just encode with JSON pkg
return json.Marshal(val)
}
}
func (t *TypeBlob) Decode(raw any) (any, bool, error) {
bb, is := raw.(*sql.RawBytes)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Blob", raw)
}
return []byte(*bb), bb != nil, nil
}
func (t *TypeBlob) Encode(val any) (driver.Value, error) {
return val, nil
}
func (*TypeUUID) MakeScanBuffer() any { return new(uuid.UUID) }
func (t *TypeUUID) Decode(raw any) (any, bool, error) {
dec, is := raw.(*uuid.UUID)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for UUID", raw)
}
return *dec, dec != nil, nil
}
func (t *TypeUUID) Encode(val any) (driver.Value, error) {
return val, nil
}
const (
DateLayout = "2006-01-02"
)
func TimestampLayout(tz bool, precision int) string {
return DateLayout + "T" + TimeLayout(tz, precision)
}
func TimeLayout(tz bool, precision int) string {
var layout = "15:04:05"
if precision > 0 {
layout += "." + strings.Repeat("9", precision)
}
if tz {
layout += "Z07:00"
}
return layout
}