forked from googleapis/go-sql-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
278 lines (267 loc) · 6.22 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
// Copyright 2021 Google LLC
//
// 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
//
// https://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 spannerdriver
import (
"database/sql/driver"
"fmt"
"io"
"sync"
"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/api/iterator"
)
type rows struct {
it rowIterator
colsOnce sync.Once
dirtyErr error
cols []string
dirtyRow *spanner.Row
}
// 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 (r *rows) Columns() []string {
r.getColumns()
return r.cols
}
// Close closes the rows iterator.
func (r *rows) Close() error {
r.it.Stop()
return nil
}
func (r *rows) getColumns() {
r.colsOnce.Do(func() {
row, err := r.it.Next()
if err == nil {
r.dirtyRow = row
} else {
r.dirtyErr = err
if err != iterator.Done {
return
}
}
rowType := r.it.Metadata().RowType
r.cols = make([]string, len(rowType.Fields))
for i, c := range rowType.Fields {
r.cols[i] = c.Name
}
})
}
// 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 should return io.EOF when there are no more rows.
//
// The dest should not be written to outside of Next. Care
// should be taken when closing Rows not to modify
// a buffer held in dest.
func (r *rows) Next(dest []driver.Value) error {
r.getColumns()
var row *spanner.Row
if r.dirtyRow != nil {
row = r.dirtyRow
r.dirtyRow = nil
} else if r.dirtyErr != nil {
err := r.dirtyErr
r.dirtyErr = nil
if err == iterator.Done {
return io.EOF
}
return err
} else {
var err error
row, err = r.it.Next() // returns io.EOF when there is no next
if err == iterator.Done {
return io.EOF
}
if err != nil {
return err
}
}
for i := 0; i < row.Size(); i++ {
var col spanner.GenericColumnValue
if err := row.Column(i, &col); err != nil {
return err
}
switch col.Type.Code {
case sppb.TypeCode_INT64:
var v spanner.NullInt64
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Int64
} else {
dest[i] = nil
}
case sppb.TypeCode_FLOAT32:
var v spanner.NullFloat32
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Float32
} else {
dest[i] = nil
}
case sppb.TypeCode_FLOAT64:
var v spanner.NullFloat64
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Float64
} else {
dest[i] = nil
}
case sppb.TypeCode_NUMERIC:
var v spanner.NullNumeric
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Numeric
} else {
dest[i] = nil
}
case sppb.TypeCode_STRING:
var v spanner.NullString
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.StringVal
} else {
dest[i] = nil
}
case sppb.TypeCode_JSON:
var v spanner.NullJSON
if err := col.Decode(&v); err != nil {
return err
}
// We always assign `v` to dest[i] here because there is no native type
// for JSON in the Go sql package. That means that instead of returning
// nil we should return a NullJSON with valid=false.
dest[i] = v
case sppb.TypeCode_BYTES:
// The column value is a base64 encoded string.
var v []byte
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_BOOL:
var v spanner.NullBool
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Bool
} else {
dest[i] = nil
}
case sppb.TypeCode_DATE:
var v spanner.NullDate
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Date
} else {
dest[i] = nil
}
case sppb.TypeCode_TIMESTAMP:
var v spanner.NullTime
if err := col.Decode(&v); err != nil {
return err
}
if v.Valid {
dest[i] = v.Time
} else {
dest[i] = nil
}
case sppb.TypeCode_ARRAY:
switch col.Type.ArrayElementType.Code {
case sppb.TypeCode_INT64:
var v []spanner.NullInt64
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_FLOAT32:
var v []spanner.NullFloat32
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_FLOAT64:
var v []spanner.NullFloat64
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_NUMERIC:
var v []spanner.NullNumeric
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_STRING:
var v []spanner.NullString
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_JSON:
var v []spanner.NullJSON
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_BYTES:
var v [][]byte
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_BOOL:
var v []spanner.NullBool
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_DATE:
var v []spanner.NullDate
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
case sppb.TypeCode_TIMESTAMP:
var v []spanner.NullTime
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v
default:
return fmt.Errorf("unsupported element type ARRAY<%v>", col.Type.ArrayElementType.Code)
}
default:
return fmt.Errorf("unsupported element type %v", col.Type.Code)
}
// TODO: Implement struct
}
return nil
}