28
28
#import " FirebaseDatabase/Sources/FValueIndex.h"
29
29
#import " FirebaseDatabase/Sources/Snapshot/FLeafNode.h"
30
30
#import " FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
31
+ #import " FirebaseDatabase/Sources/Utilities/FNextPushId.h"
31
32
#import " FirebaseDatabase/Sources/Utilities/FValidation.h"
32
33
33
34
@implementation FIRDatabaseQuery
@@ -91,7 +92,8 @@ - (FQuerySpec *)querySpec {
91
92
- (void )validateQueryEndpointsForParams : (FQueryParams *)params {
92
93
if ([params.index isEqual: [FKeyIndex keyIndex ]]) {
93
94
if ([params hasStart ]) {
94
- if (params.indexStartKey != [FUtilities minName ]) {
95
+ if (params.indexStartKey != [FUtilities minName ] &&
96
+ params.indexStartKey != [FUtilities maxName ]) {
95
97
[NSException raise: INVALID_QUERY_PARAM_ERROR
96
98
format: @" Can't use queryStartingAtValue:childKey: "
97
99
@" or queryEqualTo:andChildKey: in "
@@ -106,7 +108,8 @@ - (void)validateQueryEndpointsForParams:(FQueryParams *)params {
106
108
}
107
109
}
108
110
if ([params hasEnd ]) {
109
- if (params.indexEndKey != [FUtilities maxName ]) {
111
+ if (params.indexEndKey != [FUtilities maxName ] &&
112
+ params.indexEndKey != [FUtilities minName ]) {
110
113
[NSException raise: INVALID_QUERY_PARAM_ERROR
111
114
format: @" Can't use queryEndingAtValue:childKey: or "
112
115
@" queryEqualToValue:childKey: in "
@@ -183,9 +186,44 @@ - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue
183
186
@" queryOrderedByKey:"
184
187
userInfo: nil ];
185
188
}
189
+ NSString *methodName = @" queryStartingAtValue:childKey:" ;
190
+ if (childKey != nil ) {
191
+ [FValidation validateFrom: methodName validKey: childKey];
192
+ }
186
193
return [self queryStartingAtInternal: startValue
187
194
childKey: childKey
188
- from: @" queryStartingAtValue:childKey:"
195
+ from: methodName
196
+ priorityMethod: NO ];
197
+ }
198
+
199
+ - (FIRDatabaseQuery *)queryStartingAfterValue : (id )startAfterValue {
200
+ return [self queryStartingAfterValue: startAfterValue childKey: nil ];
201
+ }
202
+
203
+ - (FIRDatabaseQuery *)queryStartingAfterValue : (id )startAfterValue
204
+ childKey : (NSString *)childKey {
205
+ if ([self .queryParams.index isEqual: [FKeyIndex keyIndex ]] &&
206
+ childKey != nil ) {
207
+ @throw [[NSException alloc ]
208
+ initWithName: INVALID_QUERY_PARAM_ERROR
209
+ reason: @" You must use queryStartingAfterValue: instead of "
210
+ @" queryStartingAfterValue:childKey: when using "
211
+ @" queryOrderedByKey:"
212
+ userInfo: nil ];
213
+ }
214
+ if (childKey == nil ) {
215
+ childKey = [FUtilities maxName ];
216
+ } else {
217
+ childKey = [FNextPushId successor: childKey];
218
+ NSLog (@" successor of child key %@ " , childKey);
219
+ }
220
+ NSString *methodName = @" queryStartingAfterValue:childKey:" ;
221
+ if (childKey != nil && ![childKey isEqual: [FUtilities maxName ]]) {
222
+ [FValidation validateFrom: methodName validKey: childKey];
223
+ }
224
+ return [self queryStartingAtInternal: startAfterValue
225
+ childKey: childKey
226
+ from: methodName
189
227
priorityMethod: NO ];
190
228
}
191
229
@@ -194,9 +232,6 @@ - (FIRDatabaseQuery *)queryStartingAtInternal:(id<FNode>)startValue
194
232
from : (NSString *)methodName
195
233
priorityMethod : (BOOL )priorityMethod {
196
234
[self validateIndexValueType: startValue fromMethod: methodName];
197
- if (childKey != nil ) {
198
- [FValidation validateFrom: methodName validKey: childKey];
199
- }
200
235
if ([self .queryParams hasStart ]) {
201
236
[NSException raise: INVALID_QUERY_PARAM_ERROR
202
237
format: @" Can't call %@ after queryStartingAtValue or "
@@ -232,10 +267,44 @@ - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue
232
267
@" queryOrderedByKey:"
233
268
userInfo: nil ];
234
269
}
270
+ NSString *methodName = @" queryEndingAtValue:childKey:" ;
271
+ if (childKey != nil ) {
272
+ [FValidation validateFrom: methodName validKey: childKey];
273
+ }
274
+ return [self queryEndingAtInternal: endValue
275
+ childKey: childKey
276
+ from: methodName
277
+ priorityMethod: NO ];
278
+ }
235
279
280
+ - (FIRDatabaseQuery *)queryEndingBeforeValue : (id )endValue {
281
+ return [self queryEndingBeforeValue: endValue childKey: nil ];
282
+ }
283
+
284
+ - (FIRDatabaseQuery *)queryEndingBeforeValue : (id )endValue
285
+ childKey : (NSString *)childKey {
286
+ if ([self .queryParams.index isEqual: [FKeyIndex keyIndex ]] &&
287
+ childKey != nil ) {
288
+ @throw [[NSException alloc ]
289
+ initWithName: INVALID_QUERY_PARAM_ERROR
290
+ reason: @" You must use queryEndingBeforeValue: instead of "
291
+ @" queryEndingBeforeValue:childKey: when using "
292
+ @" queryOrderedByKey:"
293
+ userInfo: nil ];
294
+ }
295
+
296
+ if (childKey == nil ) {
297
+ childKey = [FUtilities minName ];
298
+ } else {
299
+ childKey = [FNextPushId predecessor: childKey];
300
+ }
301
+ NSString *methodName = @" queryEndingBeforeValue:childKey:" ;
302
+ if (childKey != nil && ![childKey isEqual: [FUtilities minName ]]) {
303
+ [FValidation validateFrom: methodName validKey: childKey];
304
+ }
236
305
return [self queryEndingAtInternal: endValue
237
306
childKey: childKey
238
- from: @" queryEndingAtValue:childKey: "
307
+ from: methodName
239
308
priorityMethod: NO ];
240
309
}
241
310
@@ -244,9 +313,6 @@ - (FIRDatabaseQuery *)queryEndingAtInternal:(id)endValue
244
313
from : (NSString *)methodName
245
314
priorityMethod : (BOOL )priorityMethod {
246
315
[self validateIndexValueType: endValue fromMethod: methodName];
247
- if (childKey != nil ) {
248
- [FValidation validateFrom: methodName validKey: childKey];
249
- }
250
316
if ([self .queryParams hasEnd ]) {
251
317
[NSException raise: INVALID_QUERY_PARAM_ERROR
252
318
format: @" Can't call %@ after queryEndingAtValue or "
0 commit comments