-
Notifications
You must be signed in to change notification settings - Fork 329
/
types.go
104 lines (81 loc) · 2.18 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
package sqlite
import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/spf13/cast"
"time"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers"
)
type (
TypeDate struct{ *dal.TypeDate }
TypeTime struct{ *dal.TypeTime }
TypeTimestamp struct{ *dal.TypeTimestamp }
)
func (*TypeDate) MakeScanBuffer() any { return new(sql.NullString) }
func (t *TypeDate) 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 := cast.StringToDate(dec.String)
if err != nil {
return time.Time{}, false, err
}
return parsed.Format(drivers.DateLayout), true, nil
}
func (t *TypeDate) Encode(val any) (driver.Value, error) {
if val == nil {
return nil, nil
}
return val, nil
}
func (*TypeTime) MakeScanBuffer() any { return new(sql.NullTime) }
func (t *TypeTime) Decode(raw any) (any, bool, error) {
dec, is := raw.(*sql.NullTime)
if !is {
return nil, false, fmt.Errorf("unexpected raw type %T for Time", raw)
}
if !dec.Valid {
return time.Time{}, false, nil
}
return dec.Time.Format(drivers.TimeLayout(false, t.Precision)), dec.Valid, nil
}
func (t *TypeTime) Encode(val any) (driver.Value, error) {
if val == nil {
return nil, nil
}
return val, nil
}
func (*TypeTimestamp) MakeScanBuffer() any { return new(sql.NullTime) }
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 {
val := dec.Time.Format(drivers.TimestampLayout(false, t.Precision))
return val, dec.Valid, nil
}
return nil, false, nil
}
func (t *TypeTimestamp) Encode(val any) (driver.Value, error) {
if val == nil {
return val, nil
}
switch c := val.(type) {
case string:
parsed, err := cast.StringToDate(c)
if err != nil {
return val, nil
}
val = parsed.Format(drivers.TimestampLayout(false, t.Precision))
case time.Time:
val = c.Format(drivers.TimestampLayout(false, t.Precision))
}
return val, nil
}