forked from leprosus/golang-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
239 lines (171 loc) · 5.35 KB
/
fetch.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
package clickhouse
import (
"fmt"
"strconv"
"time"
)
//TODO maybe need to add GetArray<Type> func(column string) (Type, error) where Type is all listen types above
// Columns returns columns list
func (result Result) Columns() (columns []string) {
for column := range result.data {
columns = append(columns, column)
}
return columns
}
// Exist returns true if field is exist or false
func (result Result) Exist(column string) bool {
cfg.logger.debug(fmt.Sprintf("Try to check if exist by `%s`", column))
_, ok := result.data[column]
return ok
}
// String returns value of string
func (result Result) String(column string) (value string, err error) {
cfg.logger.debug(fmt.Sprintf("Try to get value by `%s`", column))
value, ok := result.data[column]
if !ok {
err = fmt.Errorf("can't get value by `%s`", column)
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return
}
cfg.logger.debug(fmt.Sprintf("Success get `%s` = %s", column, value))
return
}
// Bytes returns value of bytes
func (result Result) Bytes(column string) (bytes []byte, err error) {
value, err := result.String(column)
if err != nil {
return
}
bytes = []byte(value)
return
}
func (result Result) getUInt(column string, bitSize int) (ui64 uint64, err error) {
value, err := result.String(column)
if err != nil {
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return
}
ui64, err = strconv.ParseUint(value, 10, bitSize)
if err != nil {
err = fmt.Errorf("can't convert value %s to uint%d: %s", value, bitSize, err.Error())
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return
}
return
}
// Bool returns value as bool
func (result Result) Bool(column string) (f bool, err error) {
i, err := result.getUInt(column, 8)
if err != nil {
return
}
return i == 1, nil
}
// UInt8 returns value as uint8
func (result Result) UInt8(column string) (ui8 uint8, err error) {
i, err := result.getUInt(column, 8)
return uint8(i), err
}
// UInt16 returns value as uint16
func (result Result) UInt16(column string) (ui16 uint16, err error) {
i, err := result.getUInt(column, 16)
return uint16(i), err
}
// UInt32 returns value as uint32
func (result Result) UInt32(column string) (ui32 uint32, err error) {
i, err := result.getUInt(column, 32)
return uint32(i), err
}
// UInt64 returns value as uint64
func (result Result) UInt64(column string) (ui64 uint64, err error) {
i, err := result.getUInt(column, 64)
return uint64(i), err
}
func (result Result) getInt(column string, bitSize int) (i64 int64, err error) {
value, err := result.String(column)
if err != nil {
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return 0, err
}
i64, err = strconv.ParseInt(value, 10, bitSize)
if err != nil {
err := fmt.Errorf("can't convert value %s to int%d: %s", value, bitSize, err.Error())
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return 0, err
}
return i64, nil
}
// Int8 returns value as int8
func (result Result) Int8(column string) (i8 int8, err error) {
i, err := result.getInt(column, 8)
return int8(i), err
}
// Int16 returns value as int16
func (result Result) Int16(column string) (i16 int16, err error) {
i, err := result.getInt(column, 16)
return int16(i), err
}
// Int32 returns value as int32
func (result Result) Int32(column string) (i32 int32, err error) {
i, err := result.getInt(column, 32)
return int32(i), err
}
// Int64 returns value as int64
func (result Result) Int64(column string) (i64 int64, err error) {
i, err := result.getInt(column, 64)
return int64(i), err
}
func (result Result) getFloat(column string, bitSize int) (f64 float64, err error) {
value, err := result.String(column)
if err != nil {
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return 0, err
}
f64, err = strconv.ParseFloat(value, bitSize)
if err != nil {
err := fmt.Errorf("can't convert value %s to float%d: %s", value, bitSize, err.Error())
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return 0, err
}
return f64, nil
}
// Float32 returns value as float32
func (result Result) Float32(column string) (f32 float32, err error) {
f, err := result.getFloat(column, 32)
return float32(f), err
}
// Float64 returns value as float64
func (result Result) Float64(column string) (f64 float64, err error) {
f, err := result.getFloat(column, 64)
return float64(f), err
}
// Date returns value as date
func (result Result) Date(column string) (t time.Time, err error) {
value, err := result.String(column)
if err != nil {
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return time.Time{}, err
}
t, err = time.Parse("2006-01-02", value)
if err != nil {
err := fmt.Errorf("can't convert value %s to date: %s", value, err.Error())
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return time.Time{}, err
}
return t, nil
}
// DateTime returns value as datetime
func (result Result) DateTime(column string) (t time.Time, err error) {
value, err := result.String(column)
if err != nil {
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return time.Time{}, err
}
t, err = time.Parse("2006-01-02 15:04:05", value)
if err != nil {
err := fmt.Errorf("can't convert value %s to datetime: %s", value, err.Error())
cfg.logger.error(fmt.Sprintf("Catch error %s", err.Error()))
return time.Time{}, err
}
return t, nil
}