forked from tetrafolium/luci-gae
/
finalized_query.go
360 lines (322 loc) · 9.72 KB
/
finalized_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2015 The LUCI Authors.
//
// 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 datastore
import (
"bytes"
"fmt"
"sort"
"strings"
)
// FinalizedQuery is the representation of a Query which has been normalized.
//
// It contains only fully-specified, non-redundant, non-conflicting information
// pertaining to the Query to run. It can only represent a valid query.
type FinalizedQuery struct {
original *Query
kind string
eventuallyConsistent bool
distinct bool
keysOnly bool
limit *int32
offset *int32
start Cursor
end Cursor
project []string
orders []IndexColumn
eqFilts map[string]PropertySlice
ineqFiltProp string
ineqFiltLow Property
ineqFiltLowIncl bool
ineqFiltLowSet bool
ineqFiltHigh Property
ineqFiltHighIncl bool
ineqFiltHighSet bool
}
// Original returns the original Query object from which this FinalizedQuery was
// derived.
func (q *FinalizedQuery) Original() *Query {
return q.original
}
// Kind returns the datastore 'Kind' over which this query operates. It may be
// empty for a kindless query.
func (q *FinalizedQuery) Kind() string {
return q.kind
}
// EventuallyConsistent returns true iff this query will be eventually
// consistent. This is true when the query is a non-ancestor query, or when it's
// an ancestory query with the 'EventualConsistency(true)' option set.
func (q *FinalizedQuery) EventuallyConsistent() bool {
return q.eventuallyConsistent
}
// Project is the list of fields that this query projects on, or empty if this
// is not a projection query.
func (q *FinalizedQuery) Project() []string {
if len(q.project) == 0 {
return nil
}
ret := make([]string, len(q.project))
copy(ret, q.project)
return ret
}
// Distinct returnst true iff this is a distinct projection query. It will never
// be true for non-projection queries.
func (q *FinalizedQuery) Distinct() bool {
return q.distinct
}
// KeysOnly returns true iff this query will only return keys (as opposed to a
// normal or projection query).
func (q *FinalizedQuery) KeysOnly() bool {
return q.keysOnly
}
// Limit returns the maximum number of responses this query will retrieve, and a
// boolean indicating if the limit is set.
func (q *FinalizedQuery) Limit() (int32, bool) {
if q.limit != nil {
return *q.limit, true
}
return 0, false
}
// Offset returns the number of responses this query will skip before returning
// data, and a boolean indicating if the offset is set.
func (q *FinalizedQuery) Offset() (int32, bool) {
if q.offset != nil {
return *q.offset, true
}
return 0, false
}
// Orders returns the sort orders that this query will use, including all orders
// implied by the projections, and the implicit __key__ order at the end.
func (q *FinalizedQuery) Orders() []IndexColumn {
ret := make([]IndexColumn, len(q.orders))
copy(ret, q.orders)
return ret
}
// Bounds returns the start and end Cursors. One or both may be nil. The Cursors
// returned are implementation-specific depending on the actual RawInterface
// implementation and the filters installed (if the filters interfere with
// Cursor production).
func (q *FinalizedQuery) Bounds() (start, end Cursor) {
return q.start, q.end
}
// Ancestor returns the ancestor filter key, if any. This is a convenience
// function for getting the value from EqFilters()["__ancestor__"].
func (q *FinalizedQuery) Ancestor() *Key {
if anc, ok := q.eqFilts["__ancestor__"]; ok {
return anc[0].Value().(*Key)
}
return nil
}
// EqFilters returns all the equality filters. The map key is the field name
// and the PropertySlice is the values that field should equal.
//
// This includes a special equality filter on "__ancestor__". If "__ancestor__"
// is present in the result, it's guaranteed to have 1 value in the
// PropertySlice which is of type *Key.
func (q *FinalizedQuery) EqFilters() map[string]PropertySlice {
ret := make(map[string]PropertySlice, len(q.eqFilts))
for k, v := range q.eqFilts {
newV := make(PropertySlice, len(v))
copy(newV, v)
ret[k] = newV
}
return ret
}
// IneqFilterProp returns the inequality filter property name, if one is used
// for this filter. An empty return value means that this query does not
// contain any inequality filters.
func (q *FinalizedQuery) IneqFilterProp() string {
return q.ineqFiltProp
}
// IneqFilterLow returns the field name, operator and value for the low-side
// inequality filter. If the returned field name is "", it means that there's
// no lower inequality bound on this query.
//
// If field is non-empty, op may have the values ">" or ">=".
func (q *FinalizedQuery) IneqFilterLow() (field, op string, val Property) {
if q.ineqFiltLowSet {
field = q.ineqFiltProp
val = q.ineqFiltLow
op = ">"
if q.ineqFiltLowIncl {
op = ">="
}
}
return
}
// IneqFilterHigh returns the field name, operator and value for the high-side
// inequality filter. If the returned field name is "", it means that there's
// no upper inequality bound on this query.
//
// If field is non-empty, op may have the values "<" or "<=".
func (q *FinalizedQuery) IneqFilterHigh() (field, op string, val Property) {
if q.ineqFiltHighSet {
field = q.ineqFiltProp
val = q.ineqFiltHigh
op = "<"
if q.ineqFiltHighIncl {
op = "<="
}
}
return
}
var escaper = strings.NewReplacer(
"\\%", `\%`,
"\\_", `\_`,
"\\", `\\`,
"\x00", `\0`,
"\b", `\b`,
"\n", `\n`,
"\r", `\r`,
"\t", `\t`,
"\x1A", `\Z`,
"'", `\'`,
"\"", `\"`,
"`", "\\`",
)
func gqlQuoteName(s string) string {
return fmt.Sprintf("`%s`", escaper.Replace(s))
}
func gqlQuoteString(s string) string {
return fmt.Sprintf(`"%s"`, escaper.Replace(s))
}
// GQL returns a correctly formatted Cloud Datastore GQL expression which
// is equivalent to this query.
//
// The flavor of GQL that this emits is defined here:
// https://cloud.google.com/datastore/docs/apis/gql/gql_reference
//
// NOTE: Cursors are omitted because currently there's currently no syntax for
// literal cursors.
//
// NOTE: GeoPoint values are emitted with speculated future syntax. There is
// currently no syntax for literal GeoPoint values.
func (q *FinalizedQuery) GQL() string {
ret := bytes.Buffer{}
ws := func(s string) {
_, err := ret.WriteString(s)
if err != nil {
panic(err)
}
}
ws("SELECT")
if len(q.project) != 0 {
if q.distinct {
ws(" DISTINCT")
}
proj := make([]string, len(q.project))
for i, p := range q.project {
proj[i] = gqlQuoteName(p)
}
ws(" ")
ws(strings.Join(proj, ", "))
} else if q.keysOnly {
ws(" __key__")
} else {
ws(" *")
}
if q.kind != "" {
fmt.Fprintf(&ret, " FROM %s", gqlQuoteName(q.kind))
}
filts := []string(nil)
anc := Property{}
if len(q.eqFilts) > 0 {
eqProps := make([]string, 0, len(q.eqFilts))
for k, v := range q.eqFilts {
if k == "__ancestor__" {
anc = v[0]
continue
}
eqProps = append(eqProps, k)
}
sort.Strings(eqProps)
for _, k := range eqProps {
vals := q.eqFilts[k]
k = gqlQuoteName(k)
for _, v := range vals {
if v.Type() == PTNull {
filts = append(filts, fmt.Sprintf("%s IS NULL", k))
} else {
filts = append(filts, fmt.Sprintf("%s = %s", k, v.GQL()))
}
}
}
}
if q.ineqFiltProp != "" {
for _, f := range [](func() (p, op string, v Property)){q.IneqFilterLow, q.IneqFilterHigh} {
prop, op, v := f()
if prop != "" {
filts = append(filts, fmt.Sprintf("%s %s %s", gqlQuoteName(prop), op, v.GQL()))
}
}
}
if anc.propType != PTNull {
filts = append(filts, fmt.Sprintf("__key__ HAS ANCESTOR %s", anc.GQL()))
}
if len(filts) > 0 {
fmt.Fprintf(&ret, " WHERE %s", strings.Join(filts, " AND "))
}
if len(q.orders) > 0 {
orders := make([]string, len(q.orders))
for i, col := range q.orders {
orders[i] = col.GQL()
}
fmt.Fprintf(&ret, " ORDER BY %s", strings.Join(orders, ", "))
}
if q.limit != nil {
fmt.Fprintf(&ret, " LIMIT %d", *q.limit)
}
if q.offset != nil {
fmt.Fprintf(&ret, " OFFSET %d", *q.offset)
}
return ret.String()
}
func (q *FinalizedQuery) String() string {
// TODO(riannucci): make a more compact go-like representation here.
return q.GQL()
}
// Valid returns true iff this FinalizedQuery is valid in the provided
// KeyContext's App ID and Namespace.
//
// This checks the ancestor filter (if any), as well as the inequality filters
// if they filter on '__key__'.
//
// In particular, it does NOT validate equality filters which happen to have
// values of type PTKey, nor does it validate inequality filters that happen to
// have values of type PTKey (but don't filter on the magic '__key__' field).
func (q *FinalizedQuery) Valid(kc KeyContext) error {
anc := q.Ancestor()
if anc != nil {
switch {
case !anc.Valid(false, kc):
return MakeErrInvalidKey("ancestor [%s] is not valid in context %s", anc, kc).Err()
case anc.IsIncomplete():
return MakeErrInvalidKey("ancestor [%s] is incomplete", anc).Err()
}
}
if q.ineqFiltProp == "__key__" {
if q.ineqFiltLowSet {
if k := q.ineqFiltLow.Value().(*Key); !k.Valid(false, kc) {
return MakeErrInvalidKey(
"low inequality filter key [%s] is not valid in context %s", k, kc).Err()
}
}
if q.ineqFiltHighSet {
if k := q.ineqFiltHigh.Value().(*Key); !k.Valid(false, kc) {
return MakeErrInvalidKey(
"high inequality filter key [%s] is not valid in context %s", k, kc).Err()
}
}
}
return nil
}