/
session.go
176 lines (140 loc) · 4.49 KB
/
session.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
package db
import (
"context"
"errors"
"github.com/gocql/gocql"
)
type QueryOptions struct {
UserOrRole string
Consistency gocql.Consistency
SerialConsistency gocql.SerialConsistency
PageState []byte
PageSize int
Context context.Context
}
func NewQueryOptions() *QueryOptions {
return &QueryOptions{
// Set defaults for queries that are not affected by consistency
// But still need the parameters, i.e, DDL queries.
Consistency: gocql.LocalOne,
SerialConsistency: gocql.LocalSerial,
}
}
func (q *QueryOptions) WithUserOrRole(userOrRole string) *QueryOptions {
q.UserOrRole = userOrRole
return q
}
func (q *QueryOptions) WithConsistency(consistency gocql.Consistency) *QueryOptions {
q.Consistency = consistency
return q
}
func (q *QueryOptions) WithSerialConsistency(serialConsistency gocql.SerialConsistency) *QueryOptions {
q.SerialConsistency = serialConsistency
return q
}
func (q *QueryOptions) WithPageSize(pageSize int) *QueryOptions {
q.PageSize = pageSize
return q
}
func (q *QueryOptions) WithPageState(pageState []byte) *QueryOptions {
q.PageState = pageState
return q
}
func (q *QueryOptions) WithContext(ctx context.Context) *QueryOptions {
q.Context = ctx
return q
}
type Session interface {
// Execute executes a statement without returning row results
Execute(query string, options *QueryOptions, values ...interface{}) error
// ExecuteIterSimple executes a statement and returns iterator to the result set
ExecuteIter(query string, options *QueryOptions, values ...interface{}) (ResultSet, error)
// ChangeSchema executes a schema change query and waits for schema agreement
ChangeSchema(query string, options *QueryOptions) error
//TODO: Extract metadata methods from interface into another interface
KeyspaceMetadata(keyspaceName string) (*gocql.KeyspaceMetadata, error)
}
type ResultSet interface {
PageState() []byte
Values() []map[string]interface{}
}
func (r *goCqlResultIterator) PageState() []byte {
return r.pageState
}
func (r *goCqlResultIterator) Values() []map[string]interface{} {
return r.values
}
type goCqlResultIterator struct {
pageState []byte
values []map[string]interface{}
}
func newResultIterator(iter *gocql.Iter) (*goCqlResultIterator, error) {
columns := iter.Columns()
scanner := iter.Scanner()
items := make([]map[string]interface{}, 0)
for scanner.Next() {
row, err := mapScan(scanner, columns)
if err != nil {
return nil, err
}
items = append(items, row)
}
if err := iter.Close(); err != nil {
return nil, err
}
return &goCqlResultIterator{
pageState: iter.PageState(),
values: items,
}, nil
}
type GoCqlSession struct {
ref *gocql.Session
}
func (db *Db) Execute(query string, options *QueryOptions, values ...interface{}) (ResultSet, error) {
return db.session.ExecuteIter(query, options, values...)
}
func (db *Db) ExecuteNoResult(query string, options *QueryOptions, values ...interface{}) error {
return db.session.Execute(query, options, values)
}
func (session *GoCqlSession) Execute(query string, options *QueryOptions, values ...interface{}) error {
_, err := session.ExecuteIter(query, options, values...)
return err
}
func (session *GoCqlSession) ChangeSchema(query string, options *QueryOptions) error {
err := session.Execute(query, options)
if err != nil {
return err
}
if options.Context != nil {
return session.ref.AwaitSchemaAgreement(options.Context)
}
return nil
}
func (session *GoCqlSession) ExecuteIter(query string, options *QueryOptions, values ...interface{}) (ResultSet, error) {
q := session.ref.Query(query, values...)
// Avoid reusing metadata from the prepared statement
// Otherwise, we will not get the [applied] column (https://github.com/gocql/gocql/issues/612)
// Or new columns for SELECT *
q.NoSkipMetadata()
if options != nil {
q.Consistency(options.Consistency)
if options.SerialConsistency != gocql.Serial && options.SerialConsistency != gocql.LocalSerial {
return nil, errors.New("Invalid serial consistency")
}
q.SerialConsistency(options.SerialConsistency)
if options.PageSize > 0 {
// We don't allow disabling paging
q.PageSize(options.PageSize)
}
q.PageState(options.PageState)
if options.UserOrRole != "" {
q.CustomPayload(map[string][]byte{
"ProxyExecute": []byte(options.UserOrRole),
})
}
}
return newResultIterator(q.Iter())
}
func (session *GoCqlSession) KeyspaceMetadata(keyspaceName string) (*gocql.KeyspaceMetadata, error) {
return session.ref.KeyspaceMetadata(keyspaceName)
}