Blame view

nullables.go 10.6 KB
18fcd6d6b   Marko Tikvić   merged with util ...
1
  package webutility
cacf57bd4   Marko Tikvić   merging with /uti...
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
  
  import (
  	"database/sql"
  	"database/sql/driver"
  	"encoding/json"
  	"fmt"
  	"time"
  )
  
  // NullBool is a wrapper for sql.NullBool with added JSON (un)marshalling
  type NullBool sql.NullBool
  
  // Scan ...
  func (nb *NullBool) Scan(value interface{}) error {
  	var b sql.NullBool
  	if err := b.Scan(value); err != nil {
  		nb.Bool, nb.Valid = false, false
  		return err
  	}
  	nb.Bool, nb.Valid = b.Bool, b.Valid
  	return nil
  }
  
  // Value ...
  func (nb *NullBool) Value() (driver.Value, error) {
  	if !nb.Valid {
  		return nil, nil
  	}
  	return nb.Bool, nil
  }
  
  // MarshalJSON ...
  func (nb NullBool) MarshalJSON() ([]byte, error) {
  	if nb.Valid {
  		return json.Marshal(nb.Bool)
  	}
  
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (nb *NullBool) UnmarshalJSON(b []byte) error {
  	var temp *bool
  	if err := json.Unmarshal(b, &temp); err != nil {
  		return err
  	}
  	if temp != nil {
  		nb.Valid = true
  		nb.Bool = *temp
  	} else {
  		nb.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
56
57
  // CastToSQL ...
  func (nb *NullBool) CastToSQL() sql.NullBool {
cacf57bd4   Marko Tikvić   merging with /uti...
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
  	return sql.NullBool(*nb)
  }
  
  // NullString is a wrapper for sql.NullString with added JSON (un)marshalling
  type NullString sql.NullString
  
  // Scan ...
  func (ns *NullString) Scan(value interface{}) error {
  	var s sql.NullString
  	if err := s.Scan(value); err != nil {
  		ns.String, ns.Valid = "", false
  		return err
  	}
  	ns.String, ns.Valid = s.String, s.Valid
  	return nil
  }
  
  // Value ...
  func (ns *NullString) Value() (driver.Value, error) {
  	if !ns.Valid {
  		return nil, nil
  	}
  	return ns.String, nil
  }
  
  // MarshalJSON ...
  func (ns NullString) MarshalJSON() ([]byte, error) {
  	if ns.Valid {
  		return json.Marshal(ns.String)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (ns *NullString) UnmarshalJSON(b []byte) error {
  	var temp *string
  	if err := json.Unmarshal(b, &temp); err != nil {
  		return err
  	}
  	if temp != nil {
  		ns.Valid = true
  		ns.String = *temp
  	} else {
  		ns.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
105
106
  // CastToSQL ...
  func (ns *NullString) CastToSQL() sql.NullString {
cacf57bd4   Marko Tikvić   merging with /uti...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	return sql.NullString(*ns)
  }
  
  // NullInt64 is a wrapper for sql.NullInt64 with added JSON (un)marshalling
  type NullInt64 sql.NullInt64
  
  // Scan ...
  func (ni *NullInt64) Scan(value interface{}) error {
  	var i sql.NullInt64
  	if err := i.Scan(value); err != nil {
  		ni.Int64, ni.Valid = 0, false
  		return err
  	}
  	ni.Int64, ni.Valid = i.Int64, i.Valid
  	return nil
  }
b80ee4b2b   Marko Tikvić   new stuff
123
124
125
126
127
128
129
  // ScanPtr ...
  func (ni *NullInt64) ScanPtr(v interface{}) error {
  	if ip, ok := v.(*int64); ok && ip != nil {
  		return ni.Scan(*ip)
  	}
  	return nil
  }
cacf57bd4   Marko Tikvić   merging with /uti...
130
131
132
133
134
135
136
  // Value ...
  func (ni *NullInt64) Value() (driver.Value, error) {
  	if !ni.Valid {
  		return nil, nil
  	}
  	return ni.Int64, nil
  }
1b51eed04   Marko Tikvić   pdf helper
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  func (ni *NullInt64) Val() int64 {
  	return ni.Int64
  }
  
  // Add
  func (ni *NullInt64) Add(i NullInt64) {
  	ni.Valid = true
  	ni.Int64 += i.Int64
  }
  
  func (ni *NullInt64) Set(i int64) {
  	ni.Valid = true
  	ni.Int64 = i
  }
cacf57bd4   Marko Tikvić   merging with /uti...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  // MarshalJSON ...
  func (ni NullInt64) MarshalJSON() ([]byte, error) {
  	if ni.Valid {
  		return json.Marshal(ni.Int64)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (ni *NullInt64) UnmarshalJSON(b []byte) error {
  	var temp *int64
  	if err := json.Unmarshal(b, &temp); err != nil {
  		return err
  	}
  	if temp != nil {
  		ni.Valid = true
  		ni.Int64 = *temp
  	} else {
  		ni.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
173
174
  // CastToSQL ...
  func (ni *NullInt64) CastToSQL() sql.NullInt64 {
cacf57bd4   Marko Tikvić   merging with /uti...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  	return sql.NullInt64(*ni)
  }
  
  // NullFloat64 is a wrapper for sql.NullFloat64 with added JSON (un)marshalling
  type NullFloat64 sql.NullFloat64
  
  // Scan ...
  func (nf *NullFloat64) Scan(value interface{}) error {
  	var f sql.NullFloat64
  	if err := f.Scan(value); err != nil {
  		nf.Float64, nf.Valid = 0.0, false
  		return err
  	}
  	nf.Float64, nf.Valid = f.Float64, f.Valid
  	return nil
  }
b80ee4b2b   Marko Tikvić   new stuff
191
192
193
194
195
196
197
  // ScanPtr ...
  func (nf *NullFloat64) ScanPtr(v interface{}) error {
  	if fp, ok := v.(*float64); ok && fp != nil {
  		return nf.Scan(*fp)
  	}
  	return nil
  }
cacf57bd4   Marko Tikvić   merging with /uti...
198
199
200
201
202
203
204
  // Value ...
  func (nf *NullFloat64) Value() (driver.Value, error) {
  	if !nf.Valid {
  		return nil, nil
  	}
  	return nf.Float64, nil
  }
1b51eed04   Marko Tikvić   pdf helper
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  // Val ...
  func (nf *NullFloat64) Val() float64 {
  	return nf.Float64
  }
  
  // Add ...
  func (nf *NullFloat64) Add(f NullFloat64) {
  	nf.Valid = true
  	nf.Float64 += f.Float64
  }
  
  func (nf *NullFloat64) Set(f float64) {
  	nf.Valid = true
  	nf.Float64 = f
  }
cacf57bd4   Marko Tikvić   merging with /uti...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  // MarshalJSON ...
  func (nf NullFloat64) MarshalJSON() ([]byte, error) {
  	if nf.Valid {
  		return json.Marshal(nf.Float64)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (nf *NullFloat64) UnmarshalJSON(b []byte) error {
  	var temp *float64
  	if err := json.Unmarshal(b, &temp); err != nil {
  		return err
  	}
  	if temp != nil {
  		nf.Valid = true
  		nf.Float64 = *temp
  	} else {
  		nf.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
242
243
  // CastToSQL ...
  func (nf *NullFloat64) CastToSQL() sql.NullFloat64 {
cacf57bd4   Marko Tikvić   merging with /uti...
244
245
  	return sql.NullFloat64(*nf)
  }
07daaf4e5   Marko Tikvić   separate nullable...
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
  // NullDateTime ...
  type NullDateTime struct {
  	Time  time.Time
  	Valid bool // Valid is true if Time is not NULL
  }
  
  // Scan ...
  func (nt *NullDateTime) Scan(value interface{}) (err error) {
  	if value == nil {
  		nt.Time, nt.Valid = time.Time{}, false
  		return
  	}
  
  	switch v := value.(type) {
  	case time.Time:
  		nt.Time, nt.Valid = v, true
  		return
  	case []byte:
  		nt.Time, err = parseSQLDateTime(string(v), time.UTC)
  		nt.Valid = (err == nil)
  		return
  	case string:
  		nt.Time, err = parseSQLDateTime(v, time.UTC)
  		nt.Valid = (err == nil)
  		return
  	}
  
  	nt.Valid = false
  	return fmt.Errorf("Can't convert %T to time.Time", value)
  }
  
  // Value implements the driver Valuer interface.
  func (nt NullDateTime) Value() (driver.Value, error) {
  	if !nt.Valid {
  		return nil, nil
  	}
  	return nt.Time, nil
  }
  
  // MarshalJSON ...
  func (nt NullDateTime) MarshalJSON() ([]byte, error) {
  	if nt.Valid {
  		format := nt.Time.Format("2006-01-02 15:04:05")
  		return json.Marshal(format)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (nt *NullDateTime) UnmarshalJSON(b []byte) error {
  	var temp *time.Time
  	var t1 time.Time
  	var err error
  
  	s1 := string(b)
  	s2 := s1[1 : len(s1)-1]
  	if s1 == "null" {
  		temp = nil
  	} else {
  		t1, err = time.Parse("2006-01-02 15:04:05", s2)
  		if err != nil {
  			return err
  		}
  		temp = &t1
  	}
  
  	if temp != nil {
  		nt.Valid = true
  		nt.Time = *temp
  	} else {
  		nt.Valid = false
  	}
  	return nil
  }
  
  func (nt *NullDateTime) CastToSQL() NullDateTime {
  	return *nt
  }
5d0856f6f   Marko Tikvić   when parsing time...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  func parseSQLDateTime(str string, loc *time.Location) (t time.Time, err error) {
  	base := "0000-00-00 00:00:00.0000000"
  	timeFormat := "2006-01-02 15:04:05.999999"
  	switch len(str) {
  	case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  		if str == base[:len(str)] {
  			return
  		}
  		t, err = time.Parse(timeFormat[:len(str)], str)
  	default:
  		err = fmt.Errorf("invalid time string: %s", str)
  		return
  	}
  
  	// Adjust location
  	if err == nil && loc != time.UTC {
  		y, mo, d := t.Date()
  		h, mi, s := t.Clock()
  		t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  	}
  
  	return
  }
07daaf4e5   Marko Tikvić   separate nullable...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
  // NullDate ...
  type NullDate struct {
  	Time  time.Time
  	Valid bool // Valid is true if Time is not NULL
  }
  
  // Scan ...
  func (nt *NullDate) Scan(value interface{}) (err error) {
  	if value == nil {
  		nt.Time, nt.Valid = time.Time{}, false
  		return
  	}
  
  	switch v := value.(type) {
  	case time.Time:
  		nt.Time, nt.Valid = v, true
  		return
  	case []byte:
  		nt.Time, err = parseSQLDate(string(v), time.UTC)
  		nt.Valid = (err == nil)
  		return
  	case string:
  		nt.Time, err = parseSQLDate(v, time.UTC)
  		nt.Valid = (err == nil)
  		return
  	}
  
  	nt.Valid = false
  	return fmt.Errorf("Can't convert %T to time.Time", value)
  }
  
  // Value implements the driver Valuer interface.
  func (nt NullDate) Value() (driver.Value, error) {
  	if !nt.Valid {
  		return nil, nil
  	}
  	return nt.Time, nil
  }
  
  // MarshalJSON ...
  func (nt NullDate) MarshalJSON() ([]byte, error) {
  	if nt.Valid {
  		format := nt.Time.Format("2006-01-02")
  		return json.Marshal(format)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (nt *NullDate) UnmarshalJSON(b []byte) error {
  	var temp *time.Time
  	var t1 time.Time
  	var err error
  
  	s1 := string(b)
  	s2 := s1[1 : len(s1)-1]
  	if s1 == "null" {
  		temp = nil
  	} else {
  		t1, err = time.Parse("2006-01-02", s2)
  		if err != nil {
  			return err
  		}
  		temp = &t1
  	}
  
  	if temp != nil {
5d0856f6f   Marko Tikvić   when parsing time...
414
  		nt.Scan(t1)
07daaf4e5   Marko Tikvić   separate nullable...
415
416
417
418
419
420
421
422
423
  	} else {
  		nt.Valid = false
  	}
  	return nil
  }
  
  func (nt *NullDate) CastToSQL() NullDate {
  	return *nt
  }
5d0856f6f   Marko Tikvić   when parsing time...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) {
  	base := "0000-00-00"
  	timeFormat := "2006-01-02"
  	switch len(str) {
  	case 10:
  		if str == base[:len(str)] {
  			return
  		}
  		t, err = time.Parse(timeFormat[:len(str)], str)
  	default:
  		err = fmt.Errorf("invalid time string: %s", str)
  		return
  	}
  
  	// Adjust location
  	if err == nil && loc != time.UTC {
  		y, mo, d := t.Date()
  		h, mi, s := t.Clock()
  		t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  	}
  
  	return
  }
cacf57bd4   Marko Tikvić   merging with /uti...
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  // NullTime ...
  type NullTime struct {
  	Time  time.Time
  	Valid bool // Valid is true if Time is not NULL
  }
  
  // Scan ...
  func (nt *NullTime) Scan(value interface{}) (err error) {
  	if value == nil {
  		nt.Time, nt.Valid = time.Time{}, false
  		return
  	}
  
  	switch v := value.(type) {
  	case time.Time:
  		nt.Time, nt.Valid = v, true
  		return
  	case []byte:
07daaf4e5   Marko Tikvić   separate nullable...
465
  		nt.Time, err = parseSQLTime(string(v), time.UTC)
cacf57bd4   Marko Tikvić   merging with /uti...
466
467
468
  		nt.Valid = (err == nil)
  		return
  	case string:
07daaf4e5   Marko Tikvić   separate nullable...
469
  		nt.Time, err = parseSQLTime(v, time.UTC)
cacf57bd4   Marko Tikvić   merging with /uti...
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  		nt.Valid = (err == nil)
  		return
  	}
  
  	nt.Valid = false
  	return fmt.Errorf("Can't convert %T to time.Time", value)
  }
  
  // Value implements the driver Valuer interface.
  func (nt NullTime) Value() (driver.Value, error) {
  	if !nt.Valid {
  		return nil, nil
  	}
  	return nt.Time, nil
  }
  
  // MarshalJSON ...
  func (nt NullTime) MarshalJSON() ([]byte, error) {
  	if nt.Valid {
07daaf4e5   Marko Tikvić   separate nullable...
489
  		format := nt.Time.Format("15:04:05")
cacf57bd4   Marko Tikvić   merging with /uti...
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  		return json.Marshal(format)
  	}
  	return json.Marshal(nil)
  }
  
  // UnmarshalJSON ...
  func (nt *NullTime) UnmarshalJSON(b []byte) error {
  	var temp *time.Time
  	var t1 time.Time
  	var err error
  
  	s1 := string(b)
  	s2 := s1[1 : len(s1)-1]
  	if s1 == "null" {
  		temp = nil
  	} else {
5d0856f6f   Marko Tikvić   when parsing time...
506
  		t1, err = time.Parse("2006-05-04 15:04:05", "1970-01-01 "+s2)
cacf57bd4   Marko Tikvić   merging with /uti...
507
508
509
510
511
512
513
  		if err != nil {
  			return err
  		}
  		temp = &t1
  	}
  
  	if temp != nil {
5d0856f6f   Marko Tikvić   when parsing time...
514
  		nt.Scan(t1)
cacf57bd4   Marko Tikvić   merging with /uti...
515
516
517
518
519
  	} else {
  		nt.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
520
521
522
  func (nt *NullTime) CastToSQL() NullTime {
  	return *nt
  }
5d0856f6f   Marko Tikvić   when parsing time...
523
  // NOTE(marko): Date must be included because database can't convert it to TIME otherwise.
07daaf4e5   Marko Tikvić   separate nullable...
524
  func parseSQLTime(str string, loc *time.Location) (t time.Time, err error) {
5d0856f6f   Marko Tikvić   when parsing time...
525
526
  	base := "00:00:00"
  	timeFormat := "15:04:05"
07daaf4e5   Marko Tikvić   separate nullable...
527
  	switch len(str) {
5d0856f6f   Marko Tikvić   when parsing time...
528
  	case 8:
07daaf4e5   Marko Tikvić   separate nullable...
529
530
531
  		if str == base[:len(str)] {
  			return
  		}
5d0856f6f   Marko Tikvić   when parsing time...
532
  		t, err = time.Parse("2006-05-04 "+timeFormat[:len(str)], "1970-01-01 "+str)
07daaf4e5   Marko Tikvić   separate nullable...
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  	default:
  		err = fmt.Errorf("invalid time string: %s", str)
  		return
  	}
  
  	// Adjust location
  	if err == nil && loc != time.UTC {
  		y, mo, d := t.Date()
  		h, mi, s := t.Clock()
  		t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  	}
  
  	return
  }