-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
196 lines (182 loc) · 4.44 KB
/
query.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
package binlog
import (
"errors"
"fmt"
"io"
)
// queryResponse holds one of the following values:
// okPacket, *resultSet.
//
// https://dev.mysql.com/doc/internals/en/com-query-response.html
type queryResponse interface{}
func (bl *Remote) queryRows(q string) ([][]interface{}, error) {
resp, err := bl.query(q)
if err != nil {
return nil, err
}
rs := resp.(*resultSet)
return rs.rows()
}
func (bl *Remote) query(q string) (queryResponse, error) {
bl.seq = 0
w := newWriter(bl.conn, &bl.seq)
if err := w.query(q); err != nil {
return nil, err
}
r := newReader(bl.conn, &bl.seq)
b, err := r.peek()
if err != nil {
return nil, err
}
switch b {
case okMarker:
ok := okPacket{}
if err := ok.decode(r, bl.hs.capabilityFlags); err != nil {
return nil, err
}
return ok, nil
case errMarker:
ep := errPacket{}
if err := ep.decode(r, bl.hs.capabilityFlags); err != nil {
return nil, err
}
return nil, errors.New(ep.errorMessage)
default:
rs := resultSet{}
if err := rs.decode(r, bl.hs.capabilityFlags); err != nil {
return nil, err
}
return &rs, nil
}
}
// columnDef is column definition for resultSet.
//
// https://dev.mysql.com/doc/internals/en/com-query-response.html#column-definition
type columnDef struct {
schema string
table string // virtual table-name
orgTable string // physical table-name
name string // virtual column name
orgName string // physical column name
charset uint16
columnLength uint32 // maximum length of the field
typ uint8
flags uint16
decimals uint8
}
func (cd *columnDef) decode(r *reader, capabilities uint32) error {
if capabilities&capProtocol41 != 0 {
_ = r.stringN() // catalog (always "def")
cd.schema = r.stringN()
cd.table = r.stringN()
cd.orgTable = r.stringN()
cd.name = r.stringN()
cd.orgName = r.stringN()
_ = r.intN() // next_length -- length of the following fields (always 0x0c)
cd.charset = r.int2()
cd.columnLength = r.int4()
cd.typ = r.int1()
cd.flags = r.int2()
cd.decimals = r.int1()
_ = r.skip(2) // filler
return r.err
}
return fmt.Errorf("binlog: Protocol::ColumnDefinition320 not implemented yet")
}
// resultSet made up of two parts.
// 1. column definitions
// - starts with a packet containing the column-count
// - followed by as many columnDef packets as there are columns
// - terminated by eofPacket, if the capDeprecateEOF is not set
// 2. rows
// - each row is a packet
// - terminated by eofPacket or errPacket
//
// https://dev.mysql.com/doc/internals/en/com-query-response.html#text-resultset
type resultSet struct {
r *reader
capabilities uint32
columnDefs []columnDef
}
func (rs *resultSet) decode(r *reader, capabilities uint32) error {
rs.r, rs.capabilities = r, capabilities
ncol := r.intN()
if r.err != nil {
return r.err
}
if r.more() {
return ErrMalformedPacket
}
// Parse Column Definitions.
for i := uint64(0); i < ncol; i++ {
r.rd.(*packetReader).reset()
cd := columnDef{}
if err := cd.decode(r, capabilities); err != nil {
return err
}
if r.more() {
return ErrMalformedPacket
}
rs.columnDefs = append(rs.columnDefs, cd)
}
// Parse EOF Packet.
r.rd.(*packetReader).reset()
eof := eofPacket{}
return eof.decode(r, capabilities)
}
// null represents null in resultSet.
type null struct{}
// nextRow returns data of next row. Returns io.EOF
// if there are no more rows.
func (rs *resultSet) nextRow() ([]interface{}, error) {
r := rs.r
r.rd.(*packetReader).reset()
b, err := r.peek()
if err != nil {
return nil, err
}
switch b {
case eofMarker:
eof := eofPacket{}
if err := eof.decode(r, rs.capabilities); err != nil {
return nil, err
}
return nil, io.EOF
case errMarker:
ep := errPacket{}
if err := ep.decode(r, rs.capabilities); err != nil {
return nil, err
}
return nil, errors.New(ep.errorMessage)
default:
row := make([]interface{}, len(rs.columnDefs))
for i := range row {
b, err := r.peek()
if err != nil {
return nil, err
}
if b == 0xfb {
row[i] = null{}
} else {
row[i] = r.stringN()
if r.err != nil {
return nil, r.err
}
}
}
return row, nil
}
}
// rows is helper method to collect rows into [][]interface{}.
func (rs *resultSet) rows() ([][]interface{}, error) {
var rows [][]interface{}
for {
row, err := rs.nextRow()
if err == io.EOF {
return rows, nil
} else if err != nil {
return nil, err
}
rows = append(rows, row)
}
}