-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
streaming_query.go
150 lines (128 loc) · 4.03 KB
/
streaming_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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// This file contains the methods needed to execute streaming queries.
// ExecuteStreamFetch starts a streaming query. Fields(), FetchNext() and
// CloseResult() can be called once this is successful.
// Returns a SQLError.
func (c *Conn) ExecuteStreamFetch(query string) (err error) {
defer func() {
if err != nil {
if sqlerr, ok := err.(*SQLError); ok {
sqlerr.Query = query
}
}
}()
// Sanity check.
if c.fields != nil {
return NewSQLError(CRCommandsOutOfSync, SSUnknownSQLState, "streaming query already in progress")
}
// Send the query as a COM_QUERY packet.
if err := c.WriteComQuery(query); err != nil {
return err
}
// Get the result.
colNumber, _, err := c.readComQueryResponse()
if err != nil {
return err
}
if colNumber == 0 {
// OK packet, means no results. Save an empty Fields array.
c.fields = make([]*querypb.Field, 0)
return nil
}
// Read the fields, save them.
fields := make([]querypb.Field, colNumber)
fieldsPointers := make([]*querypb.Field, colNumber)
// Read column headers. One packet per column.
// Build the fields.
for i := 0; i < colNumber; i++ {
fieldsPointers[i] = &fields[i]
if err := c.readColumnDefinition(fieldsPointers[i], i); err != nil {
return err
}
}
// Read the EOF after the fields if necessary.
if c.Capabilities&CapabilityClientDeprecateEOF == 0 {
// EOF is only present here if it's not deprecated.
data, err := c.readEphemeralPacket()
if err != nil {
return NewSQLError(CRServerLost, SSUnknownSQLState, "%v", err)
}
defer c.recycleReadPacket()
if isEOFPacket(data) {
// This is what we expect.
// Warnings and status flags are ignored.
// goto: end
} else if isErrorPacket(data) {
return ParseErrorPacket(data)
} else {
return NewSQLError(CRCommandsOutOfSync, SSUnknownSQLState, "unexpected packet after fields: %v", data)
}
}
c.fields = fieldsPointers
return nil
}
// Fields returns the fields for an ongoing streaming query.
func (c *Conn) Fields() ([]*querypb.Field, error) {
if c.fields == nil {
return nil, NewSQLError(CRCommandsOutOfSync, SSUnknownSQLState, "no streaming query in progress")
}
if len(c.fields) == 0 {
// The query returned an empty field list.
return nil, nil
}
return c.fields, nil
}
// FetchNext returns the next result for an ongoing streaming query.
// It returns (nil, nil) if there is nothing more to read.
func (c *Conn) FetchNext(in []sqltypes.Value) ([]sqltypes.Value, error) {
if c.fields == nil {
// We are already done, and the result was closed.
return nil, NewSQLError(CRCommandsOutOfSync, SSUnknownSQLState, "no streaming query in progress")
}
if len(c.fields) == 0 {
// We received no fields, so there is no data.
return nil, nil
}
data, err := c.ReadPacket()
if err != nil {
return nil, err
}
if isEOFPacket(data) {
// Warnings and status flags are ignored.
c.fields = nil
return nil, nil
} else if isErrorPacket(data) {
// Error packet.
return nil, ParseErrorPacket(data)
}
// Regular row.
return c.parseRow(data, c.fields, readLenEncStringAsBytes, in)
}
// CloseResult can be used to terminate a streaming query
// early. It just drains the remaining values.
func (c *Conn) CloseResult() {
row := make([]sqltypes.Value, 0, len(c.fields))
for c.fields != nil {
rows, err := c.FetchNext(row[:0])
if err != nil || rows == nil {
// We either got an error, or got the last result.
c.fields = nil
}
}
}