-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrows.go
158 lines (147 loc) · 3.68 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
// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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 spannerdriver
import (
"database/sql/driver"
"encoding/base64"
"io"
"log"
"sync"
"time"
"cloud.google.com/go/spanner"
"google.golang.org/api/iterator"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
)
type rows struct {
it *spanner.RowIterator
colsOnce sync.Once
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 {
log.Println(err)
return
}
r.dirtyRow = row
r.cols = row.ColumnNames()
})
}
// 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 {
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
}
dest[i] = v.Int64
case sppb.TypeCode_FLOAT64:
var v spanner.NullFloat64
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v.Float64
case sppb.TypeCode_STRING:
var v spanner.NullString
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v.StringVal
case sppb.TypeCode_BYTES:
// The column value is a base64 encoded string.
var v spanner.NullString
if err := col.Decode(&v); err != nil {
return err
}
if v.IsNull() {
dest[i] = []byte(nil)
} else {
b, err := base64.StdEncoding.DecodeString(v.StringVal)
if err != nil {
return err
}
dest[i] = b
}
case sppb.TypeCode_BOOL:
var v spanner.NullBool
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v.Bool
case sppb.TypeCode_DATE:
var v spanner.NullDate
if err := col.Decode(&v); err != nil {
return err
}
if v.IsNull() {
dest[i] = v.Date // typed nil
} else {
dest[i] = v.Date.In(time.Local) // TODO(jbd): Add note about this.
}
case sppb.TypeCode_TIMESTAMP:
var v spanner.NullTime
if err := col.Decode(&v); err != nil {
return err
}
dest[i] = v.Time
}
// TODO(jbd): Implement other types.
// How to handle array and struct?
}
return nil
}