forked from mercari/datastore
/
query.go
186 lines (155 loc) · 3.3 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
package aedatastore
import (
"context"
w "go.mercari.io/datastore"
"google.golang.org/appengine/datastore"
)
var _ w.Query = (*queryImpl)(nil)
var _ w.Iterator = (*iteratorImpl)(nil)
var _ w.Cursor = (*cursorImpl)(nil)
type queryImpl struct {
ctx context.Context
q *datastore.Query
keysOnly bool
namespace string
transaction *transactionImpl
firstError error
}
type iteratorImpl struct {
client *datastoreImpl
q *queryImpl
t *datastore.Iterator
firstError error
}
type cursorImpl struct {
cursor datastore.Cursor
}
func (q *queryImpl) clone() *queryImpl {
x := *q
return &x
}
func (q *queryImpl) Ancestor(ancestor w.Key) w.Query {
q = q.clone()
q.q = q.q.Ancestor(toOriginalKey(ancestor))
return q
}
func (q *queryImpl) EventualConsistency() w.Query {
q = q.clone()
q.q = q.q.EventualConsistency()
return q
}
func (q *queryImpl) Namespace(ns string) w.Query {
q = q.clone()
q.namespace = ns
return q
}
func (q *queryImpl) Transaction(t w.Transaction) w.Query {
q = q.clone()
q.transaction = t.(*transactionImpl)
return q
}
func (q *queryImpl) Filter(filterStr string, value interface{}) w.Query {
q = q.clone()
var err error
if pt, ok := value.(w.PropertyTranslator); ok {
value, err = pt.ToPropertyValue(q.ctx)
if err != nil {
if q.firstError == nil {
q.firstError = err
}
return q
}
}
origV, err := toOriginalValue(value)
if err != nil {
if q.firstError == nil {
q.firstError = err
}
return q
}
q.q = q.q.Filter(filterStr, origV)
return q
}
func (q *queryImpl) Order(fieldName string) w.Query {
q = q.clone()
q.q = q.q.Order(fieldName)
return q
}
func (q *queryImpl) Project(fieldNames ...string) w.Query {
q = q.clone()
q.q = q.q.Project(fieldNames...)
return q
}
func (q *queryImpl) Distinct() w.Query {
q = q.clone()
q.q = q.q.Distinct()
return q
}
func (q *queryImpl) KeysOnly() w.Query {
q = q.clone()
q.q = q.q.KeysOnly()
q.keysOnly = true
return q
}
func (q *queryImpl) Limit(limit int) w.Query {
q = q.clone()
q.q = q.q.Limit(limit)
return q
}
func (q *queryImpl) Offset(offset int) w.Query {
q = q.clone()
q.q = q.q.Offset(offset)
return q
}
func (q *queryImpl) Start(c w.Cursor) w.Query {
q = q.clone()
curImpl := c.(*cursorImpl)
q.q = q.q.Start(curImpl.cursor)
return q
}
func (q *queryImpl) End(c w.Cursor) w.Query {
q = q.clone()
curImpl := c.(*cursorImpl)
q.q = q.q.End(curImpl.cursor)
return q
}
func (t *iteratorImpl) Next(dst interface{}) (w.Key, error) {
if t.firstError != nil {
return nil, t.firstError
}
var key *datastore.Key
var origPs datastore.PropertyList
var err error
if !t.q.keysOnly {
key, err = t.t.Next(&origPs)
} else {
key, err = t.t.Next(nil)
}
if err != nil {
return nil, toWrapperError(err)
}
wKey := toWrapperKey(t.client.ctx, key)
if !t.q.keysOnly {
ps := toWrapperPropertyList(t.client.ctx, origPs)
if err = w.LoadEntity(t.client.ctx, dst, &w.Entity{Key: wKey, Properties: ps}); err != nil {
return wKey, err
}
}
return wKey, nil
}
func (t *iteratorImpl) Cursor() (w.Cursor, error) {
if t.firstError != nil {
return nil, t.firstError
}
cur, err := t.t.Cursor()
if err != nil {
return nil, toWrapperError(err)
}
return &cursorImpl{cursor: cur}, nil
}
func (cur *cursorImpl) String() string {
if cur == nil {
return ""
}
return cur.cursor.String()
}