-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
269 lines (244 loc) · 5.67 KB
/
query.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
package mapper
import (
"bytes"
"errors"
"fmt"
"strings"
"encoding/gob"
"encoding/json"
"google.golang.org/appengine/datastore"
)
type (
// Query is a gob encodable specification of a datastore query
// with only the mapper supported query features provided and
// the addition ability to specify namespaces
Query struct {
selection selection
namespaces []string
kind string
filter []filter
keysOnly bool
err error
}
// filter is a conditional filter on query results.
filter struct {
FieldName string
Op operator
Value interface{}
}
operator int
selection int
)
const (
lessThan operator = iota
lessEq
equal
greaterEq
greaterThan
)
const (
all selection = iota
empty
named
selected
)
func init() {
gob.Register(&datastore.Key{})
gob.Register(&Query{})
}
func (op operator) String() string {
switch op {
case lessThan:
return "<"
case lessEq:
return "<="
case equal:
return "="
case greaterEq:
return ">="
case greaterThan:
return ">"
default:
return "unknown"
}
}
func (s selection) String() string {
switch s {
case all:
return "all"
case empty:
return "empty"
case named:
return "named"
case selected:
return "selected"
default:
return "unknown"
}
}
// NewQuery created a new Query
func NewQuery(kind string) *Query {
return &Query{
kind: kind,
}
}
// NamespaceAll returns a derivative query specifying all namespaces
func (q *Query) NamespaceAll() *Query {
q = q.clone()
q.selection = all
q.namespaces = []string{}
return q
}
// NamespaceEmpty returns a derivative query specifying the empty namespace
func (q *Query) NamespaceEmpty() *Query {
q = q.clone()
q.selection = empty
q.namespaces = []string{}
return q
}
// NamespaceNamed returns a derivative query specifying the none-empty namespaces
func (q *Query) NamespaceNamed() *Query {
q = q.clone()
q.selection = named
q.namespaces = []string{}
return q
}
// Namespace returns a derivative query with a selection of namespaces
func (q *Query) Namespace(namespaces ...string) *Query {
q = q.clone()
q.selection = selected
q.namespaces = append(q.namespaces, namespaces...)
return q
}
// KeysOnly returns a derivative query that yields only keys, not keys and
// entities. It cannot be used with projection queries.
func (q *Query) KeysOnly() *Query {
q = q.clone()
q.keysOnly = true
return q
}
// Filter returns a derivative query with a field-based filter.
// The filterStr argument must be a field name followed by optional space,
// followed by an operator, one of ">", "<", ">=", "<=", or "=".
// Fields are compared against the provided value using the operator.
// Multiple filters are AND'ed together.
func (q *Query) Filter(filterStr string, value interface{}) *Query {
q = q.clone()
filterStr = strings.TrimSpace(filterStr)
if len(filterStr) < 1 {
q.err = errors.New("datastore: invalid filter: " + filterStr)
return q
}
f := filter{
FieldName: strings.TrimRight(filterStr, " ><=!"),
Value: value,
}
switch op := strings.TrimSpace(filterStr[len(f.FieldName):]); op {
case "<=":
f.Op = lessEq
case ">=":
f.Op = greaterEq
case "<":
f.Op = lessThan
case ">":
f.Op = greaterThan
case "=":
f.Op = equal
default:
q.err = fmt.Errorf("datastore: invalid operator %q in filter %q", op, filterStr)
return q
}
q.filter = append(q.filter, f)
return q
}
func (q *Query) clone() *Query {
x := *q
x.selection = q.selection
// Copy the contents of the slice-typed fields to a new backing store.
if len(q.namespaces) > 0 {
x.namespaces = make([]string, len(q.namespaces))
copy(x.namespaces, q.namespaces)
}
if len(q.filter) > 0 {
x.filter = make([]filter, len(q.filter))
copy(x.filter, q.filter)
}
return &x
}
// String returns a string representation of the query for display purposes only
func (q *Query) String() string {
str := fmt.Sprintf("kind:%s namespace(s):%s %s (%d)", q.kind, q.selection, strings.Join(q.namespaces, ","), len(q.namespaces))
for _, f := range q.filter {
str += fmt.Sprintf(" filter:%s", f.String())
}
return str
}
func (f *filter) String() string {
return fmt.Sprintf("%s %s %v", f.FieldName, f.Op, f.Value)
}
func (q *Query) GobEncode() ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
if err := enc.Encode(q.selection); err != nil {
return nil, err
}
if err := enc.Encode(q.namespaces); err != nil {
return nil, err
}
if err := enc.Encode(q.kind); err != nil {
return nil, err
}
if err := enc.Encode(q.filter); err != nil {
return nil, err
}
if err := enc.Encode(q.keysOnly); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (q *Query) GobDecode(b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&q.selection); err != nil {
return err
}
if err := dec.Decode(&q.namespaces); err != nil {
return err
}
if err := dec.Decode(&q.kind); err != nil {
return err
}
if err := dec.Decode(&q.filter); err != nil {
return err
}
if err := dec.Decode(&q.keysOnly); err != nil {
return err
}
return nil
}
func (q *Query) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Selection string `json:"selection"`
Namespaces []string `json:"namespaces"`
Kind string `json:"kind"`
Filter []filter `json:"filter"`
KeysOnly bool `json:"keys_only"`
}{
Selection: q.selection.String(),
Namespaces: q.namespaces,
Kind: q.kind,
Filter: q.filter,
KeysOnly: q.keysOnly,
})
}
func (f *filter) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Field string `json:"field"`
Op string `json:"operator"`
Value interface{} `json:"value"`
}{
Field: f.FieldName,
Op: f.Op.String(),
Value: f.Value,
})
}