-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
368 lines (317 loc) · 8.01 KB
/
rows.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
242
243
244
245
246
247
248
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package mysqlx
import (
"context"
"database/sql/driver"
"encoding/binary"
"fmt"
"io"
"math"
"reflect"
"github.com/renthraysk/mysqlx/proto"
"github.com/renthraysk/mysqlx/protobuf/mysqlx"
"github.com/renthraysk/mysqlx/protobuf/mysqlx_resultset"
)
const (
tagRowField = 1
)
type queryState int
const (
queryStart queryState = iota
queryFetchColumns
queryFetchedFirstRow
queryFetchRows
queryFetchDone
queryFetchDoneMoreResultSets
queryFetchDoneMoreOutParams
queryError
queryClosed
)
type rows struct {
conn *conn
state queryState
names []string
columns []*ColumnType
columnBuf [16]ColumnType
firstRow []byte
}
func (r *rows) readColumns(ctx context.Context) error {
r.state = queryFetchColumns
r.columns = r.columns[:0]
r.names = nil
buf := r.columnBuf[:]
n := len(buf)
t, b, err := r.conn.readMessage(ctx)
for err == nil && t == mysqlx.ServerMessages_RESULTSET_COLUMN_META_DATA {
if n == 0 {
n = 16
buf = make([]ColumnType, n)
}
n--
ct := &buf[n]
if err := ct.Unmarshal(b); err != nil {
return fmt.Errorf("failed to unmarshal column metadata: %w", err)
}
r.columns = append(r.columns, ct)
t, b, err = r.conn.readMessage(ctx)
}
if err != nil {
return err
}
switch t {
case mysqlx.ServerMessages_RESULTSET_ROW:
r.state, r.firstRow = queryFetchedFirstRow, b
case mysqlx.ServerMessages_ERROR:
r.state = queryError
return r.conn.handleError(b)
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE:
r.state = queryFetchDone
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE_MORE_RESULTSETS:
r.state = queryFetchDoneMoreResultSets
}
return nil
}
func (r *rows) Columns() []string {
if r.names == nil {
r.names = make([]string, len(r.columns))
for index, column := range r.columns {
r.names[index] = column.name
}
}
return r.names
}
func (r *rows) Close() error {
switch r.state {
case queryClosed, queryError:
default:
// We don't know if still holding any values in the buffer, so replace it for closing.
r.conn.replaceBuffer()
t, _, err := r.conn.readMessage(context.Background())
for err == nil && t != mysqlx.ServerMessages_SQL_STMT_EXECUTE_OK {
t, _, err = r.conn.readMessage(context.Background())
}
if err != nil {
r.state = queryError
return err
}
r.state = queryClosed
}
return nil
}
func (r *rows) Next(values []driver.Value) error {
switch r.state {
case queryFetchRows:
t, b, err := r.conn.readMessage(context.Background())
if err != nil {
return err
}
switch t {
case mysqlx.ServerMessages_RESULTSET_ROW:
return r.unmarshalRow(b, values)
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE:
r.state = queryFetchDone
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE_MORE_RESULTSETS:
r.state = queryFetchDoneMoreResultSets
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE_MORE_OUT_PARAMS:
r.state = queryFetchDoneMoreOutParams
}
case queryFetchedFirstRow:
err := r.unmarshalRow(r.firstRow, values)
r.state = queryFetchRows
r.firstRow = nil
return err
}
return io.EOF
}
func (r *rows) HasNextResultSet() bool {
return r.state == queryFetchDoneMoreResultSets || r.state == queryFetchDoneMoreOutParams
}
func (r *rows) NextResultSet() error {
ctx := context.Background()
switch r.state {
case queryFetchDoneMoreResultSets, queryFetchDoneMoreOutParams:
return r.readColumns(ctx)
case queryFetchedFirstRow:
r.state = queryFetchRows
r.firstRow = nil
fallthrough
case queryFetchRows:
for {
t, _, err := r.conn.readMessage(ctx)
if err != nil {
return err
}
switch t {
case mysqlx.ServerMessages_RESULTSET_ROW:
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE:
r.state = queryFetchDone
return io.EOF
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE_MORE_RESULTSETS:
r.state = queryFetchDoneMoreResultSets
return r.readColumns(ctx)
case mysqlx.ServerMessages_RESULTSET_FETCH_DONE_MORE_OUT_PARAMS:
r.state = queryFetchDoneMoreOutParams
return r.readColumns(ctx)
}
}
}
return io.EOF
}
// unmarshalRow parses mysqlx_resultset Row protobuf
func (r *rows) unmarshalRow(b []byte, values []driver.Value) error {
var j uint64
var nn int
i := uint64(0)
n := uint64(len(b))
// Column index
index := 0
// Breaks as soon as parsed a value per column even if hasn't parsed entire protobuf
for i < n && index < len(r.columns) {
tag := uint64(b[i])
i++
if i >= n {
return io.ErrUnexpectedEOF
}
switch tag {
case tagRowField<<3 | proto.WireBytes:
// Length...
j = uint64(b[i])
i++
if j > 0x7F {
i--
j, nn = binary.Uvarint(b[i:])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
i += uint64(nn)
}
// Length == 0 means nil
if j == 0 {
values[index] = nil
index++
continue
}
j += i
if j > n {
return io.ErrUnexpectedEOF
}
// Value
switch column := r.columns[index]; column.fieldType {
case mysqlx_resultset.ColumnMetaData_UINT:
v, nn := binary.Uvarint(b[i:j])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
values[index] = v
case mysqlx_resultset.ColumnMetaData_SINT:
v, nn := binary.Varint(b[i:j])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
values[index] = v
case mysqlx_resultset.ColumnMetaData_BYTES:
values[index] = b[i : j-1 : j-1]
case mysqlx_resultset.ColumnMetaData_DOUBLE:
if j-i != 8 {
return io.ErrUnexpectedEOF
}
values[index] = math.Float64frombits(binary.LittleEndian.Uint64(b[i:j]))
case mysqlx_resultset.ColumnMetaData_FLOAT:
if j-i != 4 {
return io.ErrUnexpectedEOF
}
values[index] = math.Float32frombits(binary.LittleEndian.Uint32(b[i:j]))
case mysqlx_resultset.ColumnMetaData_DATETIME:
if column.hasContentType && mysqlx_resultset.ContentType_DATETIME(column.contentType) == mysqlx_resultset.ContentType_DATETIME_DATE {
var d Date
if err := d.Unmarshal(b[i:j]); err != nil {
return err
}
values[index] = d
break
}
var dt DateTime
if err := dt.Unmarshal(b[i:j]); err != nil {
return err
}
values[index] = dt
case mysqlx_resultset.ColumnMetaData_DECIMAL:
values[index] = decimal(b[i:j:j])
case mysqlx_resultset.ColumnMetaData_ENUM:
values[index] = b[i : j-1 : j-1]
case mysqlx_resultset.ColumnMetaData_SET:
values[index] = b[i : j-1 : j-1]
case mysqlx_resultset.ColumnMetaData_TIME:
d, err := parseDuration(b[i:j])
if err != nil {
return err
}
values[index] = d
case mysqlx_resultset.ColumnMetaData_BIT:
bit, nn := binary.Uvarint(b[i:j])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
values[index] = bit
default:
return fmt.Errorf("unknown mysqlx column type %s", column.fieldType.String())
}
i = j
// Next column
index++
default:
switch tag >> 3 {
case tagRowField:
return fmt.Errorf("wrong wire type: expected BYTES, got %d", tag&7)
}
// Skip over tags & values not familar with
if tag > 0x7F {
i--
tag, nn = binary.Uvarint(b[i:])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
i += uint64(nn)
}
switch tag & 7 {
case proto.WireVarint:
_, nn = binary.Uvarint(b[i:])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
i += uint64(nn)
case proto.WireFixed64:
i += 8
case proto.WireBytes:
j, nn = binary.Uvarint(b[i:])
if nn <= 0 {
return io.ErrUnexpectedEOF
}
i += uint64(nn)
i += j
case proto.WireFixed32:
i += 4
default:
return fmt.Errorf("unknown wire type (%d)", tag&7)
}
}
}
if index < len(r.columns) {
return io.ErrUnexpectedEOF
}
return nil
}
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
return r.columns[index].DatabaseTypeName()
}
func (r *rows) ColumnTypeLength(index int) (int64, bool) {
return r.columns[index].Length()
}
func (r *rows) ColumnTypeNullable(index int) (bool, bool) {
return r.columns[index].Nullable()
}
func (r *rows) ColumnTypePrecisionScale(index int) (int64, int64, bool) {
return r.columns[index].PrecisionScale()
}
func (r *rows) ColumnTypeScanType(index int) reflect.Type {
return r.columns[index].ScanType()
}