Blame view

nullables.go 10.8 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
  	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
  }
0c71c5487   Marko Tikvić   minor changes
82
83
84
85
  // Val ...
  func (ns *NullString) Val() string {
  	return ns.String
  }
cacf57bd4   Marko Tikvić   merging with /uti...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  // 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...
108
109
  // CastToSQL ...
  func (ns *NullString) CastToSQL() sql.NullString {
cacf57bd4   Marko Tikvić   merging with /uti...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  	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
126
127
128
129
130
131
132
  // 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...
133
134
135
136
137
138
139
  // Value ...
  func (ni *NullInt64) Value() (driver.Value, error) {
  	if !ni.Valid {
  		return nil, nil
  	}
  	return ni.Int64, nil
  }
1b51eed04   Marko Tikvić   pdf helper
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  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...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  // 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...
176
177
  // CastToSQL ...
  func (ni *NullInt64) CastToSQL() sql.NullInt64 {
cacf57bd4   Marko Tikvić   merging with /uti...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  	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
194
195
196
197
198
199
200
  // 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...
201
202
203
204
205
206
207
  // Value ...
  func (nf *NullFloat64) Value() (driver.Value, error) {
  	if !nf.Valid {
  		return nil, nil
  	}
  	return nf.Float64, nil
  }
1b51eed04   Marko Tikvić   pdf helper
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  // 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...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  // 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...
245
246
  // CastToSQL ...
  func (nf *NullFloat64) CastToSQL() sql.NullFloat64 {
cacf57bd4   Marko Tikvić   merging with /uti...
247
248
  	return sql.NullFloat64(*nf)
  }
07daaf4e5   Marko Tikvić   separate nullable...
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
  // 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...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  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...
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
414
415
416
  // 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...
417
  		nt.Scan(t1)
07daaf4e5   Marko Tikvić   separate nullable...
418
419
420
421
422
423
424
425
426
  	} else {
  		nt.Valid = false
  	}
  	return nil
  }
  
  func (nt *NullDate) CastToSQL() NullDate {
  	return *nt
  }
0c71c5487   Marko Tikvić   minor changes
427
428
429
430
431
432
433
  func (nd *NullDate) Format(f string) string {
  	if !nd.Valid {
  		return ""
  	}
  
  	return EpochToDate(nd.Time.Unix(), f)
  }
5d0856f6f   Marko Tikvić   when parsing time...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  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...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  // 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...
475
  		nt.Time, err = parseSQLTime(string(v), time.UTC)
cacf57bd4   Marko Tikvić   merging with /uti...
476
477
478
  		nt.Valid = (err == nil)
  		return
  	case string:
07daaf4e5   Marko Tikvić   separate nullable...
479
  		nt.Time, err = parseSQLTime(v, time.UTC)
cacf57bd4   Marko Tikvić   merging with /uti...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  		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...
499
  		format := nt.Time.Format("15:04:05")
cacf57bd4   Marko Tikvić   merging with /uti...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  		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...
516
  		t1, err = time.Parse("2006-05-04 15:04:05", "1970-01-01 "+s2)
cacf57bd4   Marko Tikvić   merging with /uti...
517
518
519
520
521
522
523
  		if err != nil {
  			return err
  		}
  		temp = &t1
  	}
  
  	if temp != nil {
5d0856f6f   Marko Tikvić   when parsing time...
524
  		nt.Scan(t1)
cacf57bd4   Marko Tikvić   merging with /uti...
525
526
527
528
529
  	} else {
  		nt.Valid = false
  	}
  	return nil
  }
07daaf4e5   Marko Tikvić   separate nullable...
530
531
532
  func (nt *NullTime) CastToSQL() NullTime {
  	return *nt
  }
5d0856f6f   Marko Tikvić   when parsing time...
533
  // NOTE(marko): Date must be included because database can't convert it to TIME otherwise.
07daaf4e5   Marko Tikvić   separate nullable...
534
  func parseSQLTime(str string, loc *time.Location) (t time.Time, err error) {
5d0856f6f   Marko Tikvić   when parsing time...
535
536
  	base := "00:00:00"
  	timeFormat := "15:04:05"
07daaf4e5   Marko Tikvić   separate nullable...
537
  	switch len(str) {
5d0856f6f   Marko Tikvić   when parsing time...
538
  	case 8:
07daaf4e5   Marko Tikvić   separate nullable...
539
540
541
  		if str == base[:len(str)] {
  			return
  		}
5d0856f6f   Marko Tikvić   when parsing time...
542
  		t, err = time.Parse("2006-05-04 "+timeFormat[:len(str)], "1970-01-01 "+str)
07daaf4e5   Marko Tikvić   separate nullable...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  	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
  }