-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow_result.go
173 lines (144 loc) · 4.18 KB
/
row_result.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
package kin
import (
"database/sql"
"encoding/json"
"fmt"
"strconv"
"time"
)
// RowResult contains the results of a single row in a query result set.
type RowResult struct {
Columns []string
Data map[string]interface{}
err error
}
// ExtractJSON gets a JSON value from the dataset and unmarshals it into an
// interface passed by the caller.
func (rr *RowResult) ExtractJSON(column string, out interface{}) {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return
}
if err := json.Unmarshal(rawCol, out); err != nil {
rr.err = fmt.Errorf("column %s could not be unmarshalled with err %v", column, err)
return
}
}
// ExtractBool gets the value in the dataset and returns a boolean.
// If the value can't be extracted, it stores an error on the result and
// prevents further extraction from occurring.
func (rr *RowResult) ExtractBool(column string) bool {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return false
}
boolVal, err := strconv.ParseBool(string(rawCol))
if err != nil {
rr.err = fmt.Errorf("column %s (%+v) could not be extracted as a bool with error %v", column, rawCol, err)
return false
}
return boolVal
}
// ExtractDecimal gets the value in the dataset and returns an integer.
// If the value can't be extracted, it stores an error on the result and
// prevents further extraction from occurring.
func (rr *RowResult) ExtractDecimal(column string) float64 {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return 0
}
num, err := strconv.ParseFloat(string(rawCol), 64)
if err != nil {
rr.err = fmt.Errorf("column %s (%+v) could not be extracted as a float with error %v", column, rawCol, err)
return 0
}
return num
}
// ExtractInt gets the value in the dataset and returns an integer.
// If the value can't be extracted, it stores an error on the result and
// prevents further extraction from occurring.
func (rr *RowResult) ExtractInt(column string) int {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return 0
}
num, err := strconv.ParseInt(string(rawCol), 10, 64)
if err != nil {
rr.err = fmt.Errorf("column %s (%+v) could not be extracted as an int with error %v", column, rawCol, err)
return 0
}
return int(num)
}
// ExtractString gets the value in the dataset and returns a string.
// If the value can't be extracted, it stores an error on the result and
// prevents further extraction from occurring.
func (rr *RowResult) ExtractString(column string) string {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return ""
}
return string(rawCol)
}
// Err returns any aggregrated errors.
func (rr *RowResult) Err() error {
return rr.err
}
func newRowResult(row *sql.Rows) (*RowResult, error) {
columns, err := row.Columns()
if err != nil {
return nil, err
}
data := map[string]interface{}{}
scanDest := make([]interface{}, len(columns))
for i := 0; i < len(columns); i++ {
columnValue := &[]byte{}
columnName := columns[i]
data[columnName] = columnValue
scanDest[i] = columnValue
}
if err := row.Scan(scanDest...); err != nil {
return nil, err
}
return &RowResult{
Columns: columns,
Data: data,
err: nil,
}, nil
}
// ExtractTime gets the value in the dataset and returns a time.Time.
// If the value can't be extracted, it stores an error on the result and
// prevents further extraction from occurring.
func (rr *RowResult) ExtractTime(column string) time.Time {
rawCol, err := rr.extractColumn(column)
if err != nil {
rr.err = err
return time.Now()
}
dateFormat := "2006-01-02T15:04:05.999999Z"
dateString := string(rawCol)
t, err := time.Parse(dateFormat, dateString)
if err != nil {
rr.err = fmt.Errorf("column %s (%s) could not be extracted as a timestamp with error %v", column, dateString, err)
return time.Now()
}
return t
}
func (rr *RowResult) extractColumn(column string) ([]byte, error) {
if rr.err != nil {
return nil, rr.err
}
col, ok := rr.Data[column]
if !ok {
return nil, fmt.Errorf("column %s not found in result set", column)
}
b, ok := col.(*[]byte)
if !ok {
return nil, fmt.Errorf("column %s (%v) could not be extracted", column, col)
}
return *b, nil
}