-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.go
241 lines (197 loc) · 4.06 KB
/
row.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
package mysqlkebab
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
// Row is A single tuple with some methods
type Row struct {
tuple map[string]interface{}
}
// Ready returns true if the tuple contains at least one filled column
func (r Row) Ready() bool {
return len(r.tuple) > 0
}
// Columns returns an string array filled with column names
func (r Row) Columns() []string {
if len(r.tuple) < 1 {
return []string{}
}
var a []string
for x := range r.tuple {
a = append(a, x)
}
return a
}
// has returns true if the tuple is initialized and contains the given key
func (r Row) has(key string) bool {
if len(r.tuple) > 0 {
if _, ok := r.tuple[key]; ok {
return true
}
}
return false
}
// String returns the specified field as string
func (r Row) String(key string) string {
if !r.has(key) {
return ""
}
switch x := r.tuple[key].(type) {
case string:
return x
case int64:
return strconv.Itoa(int(x))
case float64:
return strconv.FormatFloat(x, 'f', -1, 64)
case time.Time:
return x.Format(time.RFC3339)
case bool:
return fmt.Sprintf("%t", x)
case []byte:
return string(x)
}
return ""
}
// Int64 returns the specified field as Int64
func (r Row) Int64(key string) int64 {
if !r.has(key) {
return 0
}
switch x := r.tuple[key].(type) {
case int64:
return x
case string:
{
i, _ := strconv.ParseInt(x, 10, 64)
return i
}
case float64:
return int64(x)
case []byte:
{
if len(x) == 1 {
return int64(x[0])
}
return 0
}
}
return 0
}
// Float64 returns the specified field as float64
// Check for conversion details: https://stackoverflow.com/questions/31946344/why-does-go-treat-a-postgresql-numeric-decimal-columns-as-uint8
func (r Row) Float64(key string) float64 {
if !r.has(key) {
return 0
}
switch x := r.tuple[key].(type) {
case float64:
return x
case string:
{
f, _ := strconv.ParseFloat(x, 64)
return f
}
case []uint8:
{
f, _ := strconv.ParseFloat(string(x), 64)
return f
}
case int64:
return float64(x)
}
return 0
}
// Bool returns the specified field as boolean
func (r Row) Bool(key string) bool {
if !r.has(key) {
return false
}
switch x := r.tuple[key].(type) {
case bool:
return x
case string:
{
b, _ := strconv.ParseBool(x)
return b
}
}
return false
}
// Time returns the specified field as time.Time
func (r Row) Time(key string) time.Time {
if !r.has(key) {
return time.Time{}
}
switch x := r.tuple[key].(type) {
case time.Time:
return x
case string:
{
b, _ := time.Parse(time.RFC3339, x)
return b
}
}
return time.Time{}
}
// Timef returns the specified field as time.Time
// it tries to interpret the date/time value according the given format
func (r Row) Timef(key, format string) time.Time {
if !r.has(key) {
return time.Time{}
}
switch x := r.tuple[key].(type) {
case time.Time:
return x
case string:
{
b, _ := time.Parse(format, x)
return b
}
}
return time.Time{}
}
// JSON returns the column content serialized as JSON string
func (r Row) JSON() (string, error) {
if len(r.tuple) < 1 {
return "", fmt.Errorf("empty row")
}
bs, err := json.Marshal(r.tuple)
return string(bs), err
}
// JSONMap returns the column content desserialized and a bool indicating if the map has some content
func (r Row) JSONMap(key string) (map[string]interface{}, bool) {
if !r.has(key) {
return nil, false
}
m := make(map[string]interface{})
switch x := r.tuple[key].(type) {
case []uint8:
if err := json.Unmarshal(x, &m); err != nil {
return m, false
}
case string:
if err := json.Unmarshal([]byte(x), &m); err != nil {
return m, false
}
}
return m, len(m) > 0
}
// JSONStruct returns the column content desserialized to given target struct
func (r Row) JSONStruct(key string, target interface{}) error {
if !r.has(key) {
return fmt.Errorf("unknow column %s", key)
}
switch x := r.tuple[key].(type) {
case []uint8:
if err := json.Unmarshal(x, &target); err != nil {
return err
}
case string:
if err := json.Unmarshal([]byte(x), &target); err != nil {
return err
}
}
return nil
}