forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
68 lines (59 loc) · 1.81 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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package client
import (
"database/sql/driver"
"fmt"
"io"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/sqltypes"
)
// rows creates a database/sql/driver compliant Row iterator
// for a non-streaming QueryResult.
type rows struct {
qr *mproto.QueryResult
index int
}
// newRows creates a new rows from qr.
func newRows(qr *mproto.QueryResult) driver.Rows {
return &rows{qr: qr}
}
func (ri *rows) Columns() []string {
cols := make([]string, 0, len(ri.qr.Fields))
for _, field := range ri.qr.Fields {
cols = append(cols, field.Name)
}
return cols
}
func (ri *rows) Close() error {
return nil
}
func (ri *rows) Next(dest []driver.Value) error {
if ri.index == len(ri.qr.Rows) {
return io.EOF
}
err := populateRow(dest, ri.qr.Fields, ri.qr.Rows[ri.index])
ri.index++
return err
}
// populateRow populates a row of data using the table's field descriptions.
// The returned types for "dest" include the list from the interface
// specification at https://golang.org/pkg/database/sql/driver/#Value
// and in addition the type "uint64" for unsigned BIGINT MySQL records.
func populateRow(dest []driver.Value, fields []mproto.Field, row []sqltypes.Value) error {
if len(dest) != len(fields) {
return fmt.Errorf("length mismatch: dest is %d, fields are %d", len(dest), len(fields))
}
if len(fields) != len(row) {
return fmt.Errorf("internal error: length mismatch: dest is %d, fields are %d", len(fields), len(row))
}
var err error
for i := range dest {
dest[i], err = mproto.Convert(fields[i], row[i])
if err != nil {
return fmt.Errorf("conversion error: field: %v, val: %v: %v", fields[i], row[i], err)
}
}
return nil
}