-
Notifications
You must be signed in to change notification settings - Fork 4k
/
query.dart
425 lines (396 loc) · 17.3 KB
/
query.dart
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of cloud_firestore;
/// Represents a query over the data at a particular location.
class Query {
Query._(
{@required this.firestore,
@required List<String> pathComponents,
bool isCollectionGroup = false,
Map<String, dynamic> parameters})
: _pathComponents = pathComponents,
_isCollectionGroup = isCollectionGroup,
_parameters = parameters ??
Map<String, dynamic>.unmodifiable(<String, dynamic>{
'where': List<List<dynamic>>.unmodifiable(<List<dynamic>>[]),
'orderBy': List<List<dynamic>>.unmodifiable(<List<dynamic>>[]),
}),
assert(firestore != null),
assert(pathComponents != null);
/// The Firestore instance associated with this query
final Firestore firestore;
final List<String> _pathComponents;
final Map<String, dynamic> _parameters;
final bool _isCollectionGroup;
String get _path => _pathComponents.join('/');
Query _copyWithParameters(Map<String, dynamic> parameters) {
return Query._(
firestore: firestore,
isCollectionGroup: _isCollectionGroup,
pathComponents: _pathComponents,
parameters: Map<String, dynamic>.unmodifiable(
Map<String, dynamic>.from(_parameters)..addAll(parameters),
),
);
}
Map<String, dynamic> buildArguments() {
return Map<String, dynamic>.from(_parameters)
..addAll(<String, dynamic>{
'path': _path,
});
}
/// Notifies of query results at this location
// TODO(jackson): Reduce code duplication with [DocumentReference]
Stream<QuerySnapshot> snapshots({bool includeMetadataChanges = false}) {
assert(includeMetadataChanges != null);
Future<int> _handle;
// It's fine to let the StreamController be garbage collected once all the
// subscribers have cancelled; this analyzer warning is safe to ignore.
StreamController<QuerySnapshot> controller; // ignore: close_sinks
controller = StreamController<QuerySnapshot>.broadcast(
onListen: () {
_handle = Firestore.channel.invokeMethod<int>(
'Query#addSnapshotListener',
<String, dynamic>{
'app': firestore.app.name,
'path': _path,
'isCollectionGroup': _isCollectionGroup,
'parameters': _parameters,
'includeMetadataChanges': includeMetadataChanges,
},
).then<int>((dynamic result) => result);
_handle.then((int handle) {
Firestore._queryObservers[handle] = controller;
});
},
onCancel: () {
_handle.then((int handle) async {
await Firestore.channel.invokeMethod<void>(
'removeListener',
<String, dynamic>{'handle': handle},
);
Firestore._queryObservers.remove(handle);
});
},
);
return controller.stream;
}
/// Fetch the documents for this query
Future<QuerySnapshot> getDocuments(
{Source source = Source.serverAndCache}) async {
assert(source != null);
final Map<dynamic, dynamic> data =
await Firestore.channel.invokeMapMethod<String, dynamic>(
'Query#getDocuments',
<String, dynamic>{
'app': firestore.app.name,
'path': _path,
'isCollectionGroup': _isCollectionGroup,
'parameters': _parameters,
'source': _getSourceString(source),
},
);
return QuerySnapshot._(data, firestore);
}
/// Obtains a CollectionReference corresponding to this query's location.
CollectionReference reference() =>
CollectionReference._(firestore, _pathComponents);
/// Creates and returns a new [Query] with additional filter on specified
/// [field]. [field] refers to a field in a document.
///
/// The [field] may be a [String] consisting of a single field name
/// (referring to a top level field in the document),
/// or a series of field names separated by dots '.'
/// (referring to a nested field in the document).
/// Alternatively, the [field] can also be a [FieldPath].
///
/// Only documents satisfying provided condition are included in the result
/// set.
Query where(
dynamic field, {
dynamic isEqualTo,
dynamic isLessThan,
dynamic isLessThanOrEqualTo,
dynamic isGreaterThan,
dynamic isGreaterThanOrEqualTo,
dynamic arrayContains,
List<dynamic> arrayContainsAny,
List<dynamic> whereIn,
bool isNull,
}) {
assert(field is String || field is FieldPath,
'Supported [field] types are [String] and [FieldPath].');
final ListEquality<dynamic> equality = const ListEquality<dynamic>();
final List<List<dynamic>> conditions =
List<List<dynamic>>.from(_parameters['where']);
void addCondition(dynamic field, String operator, dynamic value) {
final List<dynamic> condition = <dynamic>[field, operator, value];
assert(
conditions
.where((List<dynamic> item) => equality.equals(condition, item))
.isEmpty,
'Condition $condition already exists in this query.');
conditions.add(condition);
}
if (isEqualTo != null) addCondition(field, '==', isEqualTo);
if (isLessThan != null) addCondition(field, '<', isLessThan);
if (isLessThanOrEqualTo != null)
addCondition(field, '<=', isLessThanOrEqualTo);
if (isGreaterThan != null) addCondition(field, '>', isGreaterThan);
if (isGreaterThanOrEqualTo != null)
addCondition(field, '>=', isGreaterThanOrEqualTo);
if (arrayContains != null)
addCondition(field, 'array-contains', arrayContains);
if (arrayContainsAny != null)
addCondition(field, 'array-contains-any', arrayContainsAny);
if (whereIn != null) addCondition(field, 'in', whereIn);
if (isNull != null) {
assert(
isNull,
'isNull can only be set to true. '
'Use isEqualTo to filter on non-null values.');
addCondition(field, '==', null);
}
return _copyWithParameters(<String, dynamic>{'where': conditions});
}
/// Creates and returns a new [Query] that's additionally sorted by the specified
/// [field].
/// The field may be a [String] representing a single field name or a [FieldPath].
///
/// After a [FieldPath.documentId] order by call, you cannot add any more [orderBy]
/// calls.
/// Furthermore, you may not use [orderBy] on the [FieldPath.documentId] [field] when
/// using [startAfterDocument], [startAtDocument], [endAfterDocument],
/// or [endAtDocument] because the order by clause on the document id
/// is added by these methods implicitly.
Query orderBy(dynamic field, {bool descending = false}) {
assert(field != null && descending != null);
assert(field is String || field is FieldPath,
'Supported [field] types are [String] and [FieldPath].');
final List<List<dynamic>> orders =
List<List<dynamic>>.from(_parameters['orderBy']);
final List<dynamic> order = <dynamic>[field, descending];
assert(orders.where((List<dynamic> item) => field == item[0]).isEmpty,
'OrderBy $field already exists in this query');
assert(() {
if (field == FieldPath.documentId) {
return !(_parameters.containsKey('startAfterDocument') ||
_parameters.containsKey('startAtDocument') ||
_parameters.containsKey('endAfterDocument') ||
_parameters.containsKey('endAtDocument'));
}
return true;
}(),
'{start/end}{At/After/Before}Document order by document id themselves. '
'Hence, you may not use an order by [FieldPath.documentId] when using any of these methods for a query.');
orders.add(order);
return _copyWithParameters(<String, dynamic>{'orderBy': orders});
}
/// Creates and returns a new [Query] that starts after the provided document
/// (exclusive). The starting position is relative to the order of the query.
/// The document must contain all of the fields provided in the orderBy of
/// this query.
///
/// Cannot be used in combination with [startAtDocument], [startAt], or
/// [startAfter], but can be used in combination with [endAt],
/// [endBefore], [endAtDocument] and [endBeforeDocument].
///
/// See also:
///
/// * [endAfterDocument] for a query that ends after a document.
/// * [startAtDocument] for a query that starts at a document.
/// * [endAtDocument] for a query that ends at a document.
Query startAfterDocument(DocumentSnapshot documentSnapshot) {
assert(documentSnapshot != null);
assert(!_parameters.containsKey('startAfter'));
assert(!_parameters.containsKey('startAt'));
assert(!_parameters.containsKey('startAfterDocument'));
assert(!_parameters.containsKey('startAtDocument'));
assert(
List<List<dynamic>>.from(_parameters['orderBy'])
.where((List<dynamic> item) => item[0] == FieldPath.documentId)
.isEmpty,
'[startAfterDocument] orders by document id itself. '
'Hence, you may not use an order by [FieldPath.documentId] when using [startAfterDocument].');
return _copyWithParameters(<String, dynamic>{
'startAfterDocument': <String, dynamic>{
'id': documentSnapshot.documentID,
'path': documentSnapshot.reference.path,
'data': documentSnapshot.data
}
});
}
/// Creates and returns a new [Query] that starts at the provided document
/// (inclusive). The starting position is relative to the order of the query.
/// The document must contain all of the fields provided in the orderBy of
/// this query.
///
/// Cannot be used in combination with [startAfterDocument], [startAfter], or
/// [startAt], but can be used in combination with [endAt],
/// [endBefore], [endAtDocument] and [endBeforeDocument].
///
/// See also:
///
/// * [startAfterDocument] for a query that starts after a document.
/// * [endAtDocument] for a query that ends at a document.
/// * [endBeforeDocument] for a query that ends before a document.
Query startAtDocument(DocumentSnapshot documentSnapshot) {
assert(documentSnapshot != null);
assert(!_parameters.containsKey('startAfter'));
assert(!_parameters.containsKey('startAt'));
assert(!_parameters.containsKey('startAfterDocument'));
assert(!_parameters.containsKey('startAtDocument'));
assert(
List<List<dynamic>>.from(_parameters['orderBy'])
.where((List<dynamic> item) => item[0] == FieldPath.documentId)
.isEmpty,
'[startAtDocument] orders by document id itself. '
'Hence, you may not use an order by [FieldPath.documentId] when using [startAtDocument].');
return _copyWithParameters(<String, dynamic>{
'startAtDocument': <String, dynamic>{
'id': documentSnapshot.documentID,
'path': documentSnapshot.reference.path,
'data': documentSnapshot.data
},
});
}
/// Takes a list of [values], creates and returns a new [Query] that starts
/// after the provided fields relative to the order of the query.
///
/// The [values] must be in order of [orderBy] filters.
///
/// Cannot be used in combination with [startAt], [startAfterDocument], or
/// [startAtDocument], but can be used in combination with [endAt],
/// [endBefore], [endAtDocument] and [endBeforeDocument].
Query startAfter(List<dynamic> values) {
assert(values != null);
assert(!_parameters.containsKey('startAfter'));
assert(!_parameters.containsKey('startAt'));
assert(!_parameters.containsKey('startAfterDocument'));
assert(!_parameters.containsKey('startAtDocument'));
return _copyWithParameters(<String, dynamic>{'startAfter': values});
}
/// Takes a list of [values], creates and returns a new [Query] that starts at
/// the provided fields relative to the order of the query.
///
/// The [values] must be in order of [orderBy] filters.
///
/// Cannot be used in combination with [startAfter], [startAfterDocument],
/// or [startAtDocument], but can be used in combination with [endAt],
/// [endBefore], [endAtDocument] and [endBeforeDocument].
Query startAt(List<dynamic> values) {
assert(values != null);
assert(!_parameters.containsKey('startAfter'));
assert(!_parameters.containsKey('startAt'));
assert(!_parameters.containsKey('startAfterDocument'));
assert(!_parameters.containsKey('startAtDocument'));
return _copyWithParameters(<String, dynamic>{'startAt': values});
}
/// Creates and returns a new [Query] that ends at the provided document
/// (inclusive). The end position is relative to the order of the query.
/// The document must contain all of the fields provided in the orderBy of
/// this query.
///
/// Cannot be used in combination with [endBefore], [endBeforeDocument], or
/// [endAt], but can be used in combination with [startAt],
/// [startAfter], [startAtDocument] and [startAfterDocument].
///
/// See also:
///
/// * [startAfterDocument] for a query that starts after a document.
/// * [startAtDocument] for a query that starts at a document.
/// * [endBeforeDocument] for a query that ends before a document.
Query endAtDocument(DocumentSnapshot documentSnapshot) {
assert(documentSnapshot != null);
assert(!_parameters.containsKey('endBefore'));
assert(!_parameters.containsKey('endAt'));
assert(!_parameters.containsKey('endBeforeDocument'));
assert(!_parameters.containsKey('endAtDocument'));
assert(
List<List<dynamic>>.from(_parameters['orderBy'])
.where((List<dynamic> item) => item[0] == FieldPath.documentId)
.isEmpty,
'[endAtDocument] orders by document id itself. '
'Hence, you may not use an order by [FieldPath.documentId] when using [endAtDocument].');
return _copyWithParameters(<String, dynamic>{
'endAtDocument': <String, dynamic>{
'id': documentSnapshot.documentID,
'path': documentSnapshot.reference.path,
'data': documentSnapshot.data
},
});
}
/// Takes a list of [values], creates and returns a new [Query] that ends at the
/// provided fields relative to the order of the query.
///
/// The [values] must be in order of [orderBy] filters.
///
/// Cannot be used in combination with [endBefore], [endBeforeDocument], or
/// [endAtDocument], but can be used in combination with [startAt],
/// [startAfter], [startAtDocument] and [startAfterDocument].
Query endAt(List<dynamic> values) {
assert(values != null);
assert(!_parameters.containsKey('endBefore'));
assert(!_parameters.containsKey('endAt'));
assert(!_parameters.containsKey('endBeforeDocument'));
assert(!_parameters.containsKey('endAtDocument'));
return _copyWithParameters(<String, dynamic>{'endAt': values});
}
/// Creates and returns a new [Query] that ends before the provided document
/// (exclusive). The end position is relative to the order of the query.
/// The document must contain all of the fields provided in the orderBy of
/// this query.
///
/// Cannot be used in combination with [endAt], [endBefore], or
/// [endAtDocument], but can be used in combination with [startAt],
/// [startAfter], [startAtDocument] and [startAfterDocument].
///
/// See also:
///
/// * [startAfterDocument] for a query that starts after document.
/// * [startAtDocument] for a query that starts at a document.
/// * [endAtDocument] for a query that ends at a document.
Query endBeforeDocument(DocumentSnapshot documentSnapshot) {
assert(documentSnapshot != null);
assert(!_parameters.containsKey('endBefore'));
assert(!_parameters.containsKey('endAt'));
assert(!_parameters.containsKey('endBeforeDocument'));
assert(!_parameters.containsKey('endAtDocument'));
assert(
List<List<dynamic>>.from(_parameters['orderBy'])
.where((List<dynamic> item) => item[0] == FieldPath.documentId)
.isEmpty,
'[endBeforeDocument] orders by document id itself. '
'Hence, you may not use an order by [FieldPath.documentId] when using [endBeforeDocument].');
return _copyWithParameters(<String, dynamic>{
'endBeforeDocument': <String, dynamic>{
'id': documentSnapshot.documentID,
'path': documentSnapshot.reference.path,
'data': documentSnapshot.data,
},
});
}
/// Takes a list of [values], creates and returns a new [Query] that ends before
/// the provided fields relative to the order of the query.
///
/// The [values] must be in order of [orderBy] filters.
///
/// Cannot be used in combination with [endAt], [endBeforeDocument], or
/// [endBeforeDocument], but can be used in combination with [startAt],
/// [startAfter], [startAtDocument] and [startAfterDocument].
Query endBefore(List<dynamic> values) {
assert(values != null);
assert(!_parameters.containsKey('endBefore'));
assert(!_parameters.containsKey('endAt'));
assert(!_parameters.containsKey('endBeforeDocument'));
assert(!_parameters.containsKey('endAtDocument'));
return _copyWithParameters(<String, dynamic>{'endBefore': values});
}
/// Creates and returns a new Query that's additionally limited to only return up
/// to the specified number of documents.
Query limit(int length) {
assert(!_parameters.containsKey('limit'));
return _copyWithParameters(<String, dynamic>{'limit': length});
}
}