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
use crate::datastore::{Key, Value};
/// Represents Datastore query result orderings.
#[derive(Debug, Clone, PartialEq)]
pub enum Order {
/// Ascendent ordering.
Asc(String),
/// Descendent ordering.
Desc(String),
}
/// Represents Datastore query result orderings.
#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
/// Equality filter (==).
Equal(String, Value),
/// Greater-than filter (>).
GreaterThan(String, Value),
/// Lesser-than filter (<).
LesserThan(String, Value),
/// Greater-than-or-equal filter (>=).
GreaterThanOrEqual(String, Value),
/// Lesser-than-or-equal filter (<=).
LesserThanEqual(String, Value),
}
/// Represents a Datastore query.
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
pub(crate) kind: String,
pub(crate) eventual: bool,
pub(crate) keys_only: bool,
pub(crate) offset: i32,
pub(crate) limit: Option<i32>,
pub(crate) ancestor: Option<Key>,
pub(crate) namespace: Option<String>,
pub(crate) projections: Vec<String>,
pub(crate) distinct_on: Vec<String>,
pub(crate) ordering: Vec<Order>,
pub(crate) filters: Vec<Filter>,
}
impl Query {
/// Construct a new empty Query.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users");
/// ```
pub fn new(kind: impl Into<String>) -> Query {
Query {
kind: kind.into(),
eventual: false,
keys_only: false,
offset: 0,
limit: None,
ancestor: None,
namespace: None,
projections: Vec::new(),
distinct_on: Vec::new(),
ordering: Vec::new(),
filters: Vec::new(),
}
}
/// Ask to accept eventually consistent results.
/// It only has an effect on ancestor queries.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users")
/// .eventually_consistent();
/// ```
pub fn eventually_consistent(mut self) -> Query {
self.eventual = true;
self
}
/// Ask to yield only yield keys, without the entity values.
/// It has no effects on projected queries.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users").keys_only();
/// ```
pub fn keys_only(mut self) -> Query {
self.keys_only = true;
self
}
/// Skip any number of keys before returning results.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users").offset(14);
/// ```
pub fn offset(mut self, offset: i32) -> Query {
self.offset = offset;
self
}
/// Limit the number of results to send back.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users").limit(25);
/// ```
pub fn limit(mut self, limit: i32) -> Query {
self.limit = Some(limit);
self
}
/// Appends an ancestor filter to the query.
///
/// ```
/// # use google_cloud::datastore::Query;
/// use google_cloud::datastore::Key;
///
/// let key = Key::new("dev").id(10);
/// let query = Query::new("users").ancestor(key);
/// ```
pub fn ancestor(mut self, key: Key) -> Query {
self.ancestor = Some(key);
self
}
/// Associates the query with a namespace.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let query = Query::new("users").namespace("dev");
/// ```
pub fn namespace(mut self, namespace: impl Into<String>) -> Query {
self.namespace = Some(namespace.into());
self
}
/// Ask to only yield the given fields.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let fields: Vec<String> = vec![
/// "firstname".into(),
/// "lastname".into(),
/// "age".into()
/// ];
/// let query = Query::new("users").project(fields);
/// ```
pub fn project<T, I>(mut self, projections: I) -> Query
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
self.projections.clear();
self.projections
.extend(projections.into_iter().map(Into::into));
self
}
/// Ask to yield de-duplicated results.
///
/// ```
/// # use google_cloud::datastore::Query;
/// let fields: Vec<String> = vec!["email".into()];
/// let query = Query::new("users").distinct_on(fields);
/// ```
pub fn distinct_on<T, I>(mut self, fields: I) -> Query
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
self.distinct_on.clear();
self.distinct_on.extend(fields.into_iter().map(Into::into));
self
}
/// Filter results based on their fields.
/// Multiple filters are combined with an 'AND'.
///
/// ```
/// # use google_cloud::datastore::Query;
/// use google_cloud::datastore::{Filter, Value, IntoValue};
///
/// let query = Query::new("users")
/// .filter(Filter::GreaterThan("age".into(), 10.into_value()))
/// .filter(Filter::Equal("firstname".into(), "john".into_value()));
/// ```
pub fn filter(mut self, filter: Filter) -> Query {
self.filters.push(filter);
self
}
/// Order results based on some of their fields.
/// Multiple orderings are applied in the order they are added.
///
/// ```
/// # use google_cloud::datastore::Query;
/// use google_cloud::datastore::Order;
///
/// let query = Query::new("users")
/// .order(Order::Asc("age".into()))
/// .order(Order::Desc("firstname".into()));
/// ```
pub fn order(mut self, order: Order) -> Query {
self.ordering.push(order);
self
}
}