-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcqlr.go
219 lines (178 loc) · 3.87 KB
/
cqlr.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
package cqlr
import (
"errors"
"github.com/gocql/gocql"
"reflect"
"strings"
)
type Binding struct {
err error
qry *gocql.Query
iter *gocql.Iter
stmt string
arg interface{}
isCompiled bool
strict bool
strategy map[string]reflect.Value
fun func(gocql.ColumnInfo) (reflect.StructField, bool)
typeMap map[string]string
fieldMap map[string][]int
}
func BindQuery(q *gocql.Query) *Binding {
return &Binding{
qry: q,
strategy: make(map[string]reflect.Value),
fieldMap: make(map[string][]int),
}
}
func Bind(s string, v interface{}) *Binding {
return &Binding{
stmt: s,
arg: v,
strategy: make(map[string]reflect.Value),
fieldMap: make(map[string][]int),
}
}
func (b *Binding) Bind(v interface{}) *Binding {
if b.qry == nil {
b.arg = v
} else {
b.qry.Bind(v)
}
return b
}
func (b *Binding) Exec(s *gocql.Session) error {
return s.Bind(b.stmt, b.bind).Exec()
}
func (b *Binding) Query(s *gocql.Session) *gocql.Query {
return s.Bind(b.stmt, b.bind)
}
func (b *Binding) Use(f func(gocql.ColumnInfo) (reflect.StructField, bool)) *Binding {
b.fun = f
return b
}
func (b *Binding) Map(m map[string]string) *Binding {
b.typeMap = m
return b
}
func (b *Binding) Strict() *Binding {
b.strict = true
return b
}
func (b *Binding) Close() error {
if b.err != nil {
return b.err
}
if err := b.iter.Close(); err != nil {
return err
}
return nil
}
func (b *Binding) Scan(dest interface{}) bool {
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr || v.IsNil() {
return false
}
if b.iter == nil {
b.iter = b.qry.Iter()
}
cols := b.iter.Columns()
if !b.isCompiled {
if err := b.compile(v, cols); err != nil {
b.err = err
return false
}
}
values := make([]interface{}, len(cols))
for i, col := range cols {
f, ok := b.strategy[col.Name]
if ok {
values[i] = f.Addr().Interface()
}
}
return b.iter.Scan(values...)
}
func (b *Binding) bind(q *gocql.QueryInfo) ([]interface{}, error) {
values := make([]interface{}, len(q.Args))
value := reflect.ValueOf(b.arg)
if !b.isCompiled {
if err := b.compile(value, q.Args); err != nil {
return nil, err
}
}
for i, col := range q.Args {
f, ok := b.strategy[col.Name]
if b.strict && !ok {
return nil, ErrMissingStrategy
}
if ok {
if f.CanInterface() {
values[i] = f.Interface()
} else if f.CanAddr() {
values[i] = f.Addr().Interface()
}
}
// TODO Going forwards, passing a nil value to the gocql driver
// might be a valid approach, but for now, we're going to try
// avoid confusing people with reflect panics
if values[i] == nil {
return nil, ErrMissingStrategy
}
}
return values, nil
}
func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
indirect := reflect.Indirect(v)
s := indirect.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
tag := f.Tag.Get("cql")
if tag != "" {
b.strategy[tag] = indirect.Field(i)
} else {
b.fieldMap[strings.ToLower(f.Name)] = f.Index
}
}
if b.fun != nil {
for _, col := range cols {
staticField, ok := b.fun(col)
if ok {
b.strategy[col.Name] = indirect.FieldByIndex(staticField.Index)
}
}
}
if b.typeMap != nil && len(b.typeMap) > 0 {
for _, col := range cols {
fieldName, ok := b.typeMap[col.Name]
if ok {
f := indirect.FieldByName(fieldName)
b.strategy[col.Name] = f
}
}
}
for _, col := range cols {
_, ok := b.strategy[col.Name]
if !ok {
index, ok := b.fieldMap[col.Name]
if !ok {
index, ok = b.fieldMap[strings.ToLower(col.Name)]
}
if ok {
f := indirect.FieldByIndex(index)
if f.IsValid() {
b.strategy[col.Name] = f
}
}
}
}
if b.strict {
if len(b.strategy) != len(cols) {
return ErrMissingStrategy
}
}
b.isCompiled = true
return nil
}
var (
ErrMissingStrategy = errors.New("insufficient column mapping")
)