-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrows.go
More file actions
246 lines (219 loc) · 7.35 KB
/
rows.go
File metadata and controls
246 lines (219 loc) · 7.35 KB
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
// Copyright 2026 Dolthub, Inc.
//
// 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 embedded
import (
"database/sql/driver"
"errors"
"fmt"
"io"
gms "github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)
// doltMultiRows implements driver.RowsNextResultSet by aggregating a
// set of individual doltRows instances. Each doltRows is a lazy
// producer to get the row iter, and they are run on demand. The
// current result set is in currentRowSet, with the index of its
// producer in currentIdx.
type doltMultiRows struct {
rowSets []func() (*doltRows, error)
currentIdx int
currentRowSet *doltRows
}
var _ driver.RowsNextResultSet = (*doltMultiRows)(nil)
var _ driver.RowsColumnTypeDatabaseTypeName = (*doltMultiRows)(nil)
func (d *doltMultiRows) Columns() []string {
if d.currentRowSet == nil {
return nil
}
return d.currentRowSet.Columns()
}
func (d *doltMultiRows) ColumnTypeDatabaseTypeName(i int) string {
if d.currentRowSet == nil {
return ""
}
return d.currentRowSet.ColumnTypeDatabaseTypeName(i)
}
// Close implements the driver.Rows interface. When Close is called on
// a doltMultiRows instance, it will close the open-and-unclosed
// doltRows instance that it contains, if there is one. If any errors
// are encountered while closing the row set, that error will be
// returned.
func (d *doltMultiRows) Close() error {
rowSet := d.currentRowSet
d.currentRowSet = nil
d.rowSets = nil
if rowSet != nil {
return rowSet.Close()
}
return nil
}
// Return the next row from the current result set.
func (d *doltMultiRows) Next(dest []driver.Value) error {
if d.currentRowSet == nil {
return io.EOF
}
return d.currentRowSet.Next(dest)
}
// Called by the database/sql implementation at the end of a result
// set. Simply returns |true| if there are more queries we need to
// run. It is OK if this returns |true| but |NextResultSet| ends up
// returning |io.EOF| after we run all the remaining queries.
func (d *doltMultiRows) HasNextResultSet() bool {
return (d.currentIdx + 1) < len(d.rowSets)
}
// Called anytime we are going on to the next result set, including at
// the end of an existing result set or in the middle of one at the
// request of the caller.
//
// This is responsible for closing the existing result set and for
// running the remaining queries until we get to an iterator that has
// a result set.
//
// Returns io.EOF if we have run all the queries and there are no
// result sets to iterate.
func (d *doltMultiRows) NextResultSet() error {
if d.currentRowSet == nil {
return io.EOF
}
err := d.currentRowSet.Close()
if err != nil {
return err
}
d.currentRowSet = nil
for d.currentIdx = d.currentIdx + 1; d.currentIdx < len(d.rowSets); d.currentIdx++ {
iter, err := d.rowSets[d.currentIdx]()
if err != nil {
return err
} else if iter.isQueryResultSet() {
d.currentRowSet = iter
return nil
} else {
err = iter.Close()
if err != nil {
return err
}
}
}
return io.EOF
}
type doltRows struct {
sch gms.Schema
rowIter gms.RowIter
conn *DoltConn
gmsCtx *gms.Context // per-query context; used for Next and Close calls on the iter.
columns []string
// True once rowIter has seen io.EOF.
drained bool
}
var _ driver.Rows = (*doltRows)(nil)
var _ driver.RowsColumnTypeDatabaseTypeName = (*doltRows)(nil)
// ColumnTypeDatabaseTypeName returns the database system type name for column i.
func (rows *doltRows) ColumnTypeDatabaseTypeName(i int) string {
if i >= 0 && i < len(rows.sch) {
return rows.sch[i].Type.String()
}
return ""
}
// Columns returns the names of the columns. The number of columns of the result is inferred from the length of the
// slice. If a particular column name isn't known, an empty string should be returned for that entry.
func (rows *doltRows) Columns() []string {
if rows.columns == nil {
rows.columns = make([]string, len(rows.sch))
for i, col := range rows.sch {
rows.columns[i] = col.Name
}
}
return rows.columns
}
// isQueryResultSet indicates if this result set was generated by a statement that doesn't produce a result set. For
// example, an INSERT or DML statement doesn't return a result set. This is used to skip over this doltRows
// instance if it does not have a result set when calling NextResultSet() on a doltMultiRows instance.
func (rows *doltRows) isQueryResultSet() bool {
return isQueryResultSet(rows.sch)
}
// Close closes the rows iterator.
func (rows *doltRows) Close() error {
if !rows.isQueryResultSet() {
// DML and DDL want Next() to be called on their iters in order to run.
err := rows.drain()
if err != nil {
_ = rows.rowIter.Close(rows.gmsCtx)
return err
}
}
err := translateError(rows.rowIter.Close(rows.gmsCtx))
return err
}
// drain consumes all remaining rows in the iterator without converting them.
// This is used by NextResultSet to mirror MySQL client/server behavior, where
// the client must consume all pending rows before advancing to the next result set.
func (rows *doltRows) drain() error {
if !rows.drained {
rows.drained = true
for {
_, err := rows.rowIter.Next(rows.gmsCtx)
if err != nil {
if err == io.EOF {
return nil
}
return translateError(err)
}
}
}
return nil
}
// Next is called to populate the next row of data into the provided slice. The provided slice will be the same size as
// the Columns() are wide. Next returns io.EOF when there are no more rows.
func (rows *doltRows) Next(dest []driver.Value) error {
nextRow, err := rows.rowIter.Next(rows.gmsCtx)
if err != nil {
if err == io.EOF {
rows.drained = true
return io.EOF
}
return translateError(err)
}
if len(dest) != len(nextRow) {
return errors.New("mismatch between expected column count and actual column count")
}
for i := range nextRow {
if v, ok := nextRow[i].(driver.Valuer); ok {
dest[i], err = v.Value()
if err != nil {
return fmt.Errorf("error processing column %d: %w", i, err)
}
} else if geomValue, ok := nextRow[i].(types.GeometryValue); ok {
dest[i] = geomValue.Serialize()
} else if enumType, ok := rows.sch[i].Type.(gms.EnumType); ok {
if v, _, err := enumType.Convert(rows.gmsCtx, nextRow[i]); err != nil {
return fmt.Errorf("could not convert to expected enum type for column %d: %w", i, err)
} else if enumStr, ok := enumType.At(int(v.(uint16))); !ok {
return fmt.Errorf("not a valid enum index for column %d: %v", i, v)
} else {
dest[i] = enumStr
}
} else if setType, ok := rows.sch[i].Type.(gms.SetType); ok {
if v, _, err := setType.Convert(rows.gmsCtx, nextRow[i]); err != nil {
return fmt.Errorf("could not convert to expected set type for column %d: %w", i, err)
} else if setStr, err := setType.BitsToString(v.(uint64)); err != nil {
return fmt.Errorf("could not convert value to set string for column %d: %w", i, err)
} else {
dest[i] = setStr
}
} else {
dest[i] = nextRow[i]
}
}
return nil
}