-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathclient_side_statement.go
More file actions
114 lines (99 loc) · 3.68 KB
/
client_side_statement.go
File metadata and controls
114 lines (99 loc) · 3.68 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
// Copyright 2021 Google LLC 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 (
"time"
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/api/iterator"
)
func createEmptyIterator() *clientSideIterator {
return &clientSideIterator{
metadata: &spannerpb.ResultSetMetadata{
RowType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{},
},
},
rows: []*spanner.Row{},
}
}
// createBooleanIterator creates a row iterator with a single BOOL column with
// one row. This is used for client side statements that return a result set
// containing a BOOL value.
func createBooleanIterator(column string, value bool) (*clientSideIterator, error) {
return createSingleValueIterator(column, value, spannerpb.TypeCode_BOOL)
}
// createInt64Iterator creates a row iterator with a single INT64 column with
// one row. This is used for client side statements that return a result set
// containing an INT64 value.
func createInt64Iterator(column string, value int64) (*clientSideIterator, error) {
return createSingleValueIterator(column, value, spannerpb.TypeCode_INT64)
}
// createStringIterator creates a row iterator with a single STRING column with
// one row. This is used for client side statements that return a result set
// containing a STRING value.
func createStringIterator(column string, value string) (*clientSideIterator, error) {
return createSingleValueIterator(column, value, spannerpb.TypeCode_STRING)
}
// createTimestampIterator creates a row iterator with a single TIMESTAMP column with
// one row. This is used for client side statements that return a result set
// containing a TIMESTAMP value.
func createTimestampIterator(column string, value *time.Time) (*clientSideIterator, error) {
return createSingleValueIterator(column, value, spannerpb.TypeCode_TIMESTAMP)
}
func createSingleValueIterator(column string, value interface{}, code spannerpb.TypeCode) (*clientSideIterator, error) {
row, err := spanner.NewRow([]string{column}, []interface{}{value})
if err != nil {
return nil, err
}
return &clientSideIterator{
metadata: &spannerpb.ResultSetMetadata{
RowType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{
{Name: column, Type: &spannerpb.Type{Code: code}},
},
},
},
rows: []*spanner.Row{row},
}, nil
}
var _ rowIterator = &clientSideIterator{}
// clientSideIterator implements the rowIterator interface for client side
// statements. All values are created and kept in memory, and this struct
// should only be used for small result sets.
type clientSideIterator struct {
metadata *spannerpb.ResultSetMetadata
rows []*spanner.Row
index int
stopped bool
}
func (t *clientSideIterator) Next() (*spanner.Row, error) {
if t.index == len(t.rows) {
return nil, iterator.Done
}
row := t.rows[t.index]
t.index++
return row, nil
}
func (t *clientSideIterator) Stop() {
t.stopped = true
t.rows = nil
t.metadata = nil
}
func (t *clientSideIterator) Metadata() (*spannerpb.ResultSetMetadata, error) {
return t.metadata, nil
}
func (t *clientSideIterator) ResultSetStats() *spannerpb.ResultSetStats {
return &spannerpb.ResultSetStats{}
}