Skip to content

Commit 4239c91

Browse files
committed
Added support (including test cases) for NSBetweenPredicateOperatorType using the SQL BETWEEN expression
1 parent 74626aa commit 4239c91

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

Incremental Store/EncryptedStore.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,6 @@ - (NSDictionary *)recursiveWhereClauseWithFetchRequest:(NSFetchRequest *)request
21912191

21922192
// enum {
21932193
// NSCustomSelectorPredicateOperatorType,
2194-
// NSBetweenPredicateOperatorType
21952194
// };
21962195
// typedef NSUInteger NSPredicateOperatorType;
21972196

@@ -2210,7 +2209,8 @@ - (NSDictionary *)recursiveWhereClauseWithFetchRequest:(NSFetchRequest *)request
22102209
@(NSLessThanPredicateOperatorType) : @{ @"operator" : @"<", @"format" : @"%@" },
22112210
@(NSLessThanOrEqualToPredicateOperatorType) : @{ @"operator" : @"<=", @"format" : @"%@" },
22122211
@(NSGreaterThanPredicateOperatorType) : @{ @"operator" : @">", @"format" : @"%@" },
2213-
@(NSGreaterThanOrEqualToPredicateOperatorType) : @{ @"operator" : @">=", @"format" : @"%@" }
2212+
@(NSGreaterThanOrEqualToPredicateOperatorType) : @{ @"operator" : @">=", @"format" : @"%@" },
2213+
@(NSBetweenPredicateOperatorType) : @{ @"operator" : @"BETWEEN", @"format" : @"%@ AND %@" }
22142214
};
22152215
});
22162216

@@ -2583,12 +2583,17 @@ - (void)parseExpression:(NSExpression *)expression
25832583
*operand = @"?";
25842584
}
25852585
else if ([value isKindOfClass:[NSArray class]]) {
2586-
NSUInteger count = [value count];
2587-
NSArray *parameters = [NSArray cmdArrayWithObject:@"?" times:count];
2588-
*bindings = value;
2589-
*operand = [NSString stringWithFormat:
2590-
[operator objectForKey:@"format"],
2591-
[parameters componentsJoinedByString:@", "]];
2586+
if (predicate.predicateOperatorType == NSBetweenPredicateOperatorType) {
2587+
*bindings = value;
2588+
*operand = [NSString stringWithFormat:[operator objectForKey:@"format"], @"?", @"?"];
2589+
} else {
2590+
NSUInteger count = [value count];
2591+
NSArray *parameters = [NSArray cmdArrayWithObject:@"?" times:count];
2592+
*bindings = value;
2593+
*operand = [NSString stringWithFormat:
2594+
[operator objectForKey:@"format"],
2595+
[parameters componentsJoinedByString:@", "]];
2596+
}
25922597
}
25932598
else if ([value isKindOfClass:[NSString class]]) {
25942599
if ([predicate options] & NSCaseInsensitivePredicateOption) {

exampleProjects/IncrementalStore/Model.xcdatamodeld/Model.xcdatamodel/contents

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<entity name="Post" syncable="YES">
77
<attribute name="body" optional="YES" attributeType="String" syncable="YES"/>
88
<attribute name="tags" optional="YES" attributeType="Transformable" syncable="YES"/>
9+
<attribute name="timestamp" optional="YES" attributeType="Date" syncable="YES"/>
910
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
1011
<relationship name="comments" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Comment" inverseName="parent" inverseEntity="Comment" syncable="YES"/>
1112
<relationship name="user" optional="YES" minCount="1" maxCount="1" deletionRule="Cascade" destinationEntity="User" inverseName="posts" inverseEntity="User" syncable="YES"/>
@@ -24,7 +25,7 @@
2425
</entity>
2526
<elements>
2627
<element name="Comment" positionX="97" positionY="-13" width="128" height="58"/>
27-
<element name="Post" positionX="-144" positionY="-45" width="128" height="118"/>
28+
<element name="Post" positionX="-144" positionY="-45" width="128" height="133"/>
2829
<element name="RootTag" positionX="-99" positionY="117" width="128" height="43"/>
2930
<element name="Tag" positionX="-99" positionY="197" width="128" height="73"/>
3031
<element name="User" positionX="-360" positionY="54" width="128" height="118"/>

exampleProjects/IncrementalStore/Tests/IncrementalStoreTests.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ - (void)createPosts:(NSUInteger)count forUser:(NSManagedObject *)user {
140140
[object setValue:@"adventures" forKey:@"title"];
141141
[object setValue:@"fundamental" forKey:@"body"];
142142
[object setValue:user forKey:@"user"];
143+
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3600 * i];
144+
[object setValue:date forKey:@"timestamp"];
143145
}
144146
error = nil;
145147
BOOL save = [context save:&error];
@@ -757,4 +759,34 @@ -(void)test_predicateEqualityComparison {
757759

758760
}
759761

762+
-(void)test_predicateEqualityComparisonUsingDates
763+
{
764+
const NSUInteger count = 30;
765+
766+
__block NSError *error = nil;
767+
NSManagedObject *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
768+
[context save:&error];
769+
XCTAssertNil(error, @"Error saving database.");
770+
771+
NSDate *now = [NSDate date];
772+
773+
[self createPosts:count forUser:user];
774+
775+
[@{ [NSPredicate predicateWithFormat:@"timestamp < %@", now] : @(0),
776+
[NSPredicate predicateWithFormat:@"timestamp > %@", now] : @(count),
777+
[NSPredicate predicateWithFormat:@"timestamp BETWEEN %@", @[[now dateByAddingTimeInterval:100], [now dateByAddingTimeInterval:4000]]] : @(1),
778+
[NSPredicate predicateWithFormat:@"timestamp BETWEEN %@", @[now, [now dateByAddingTimeInterval:30 * 3601]]] : @(count),
779+
[NSPredicate predicateWithFormat:@"timestamp BETWEEN %@", @[[now dateByAddingTimeInterval:100], [now dateByAddingTimeInterval:10 * 3602]]] : @(10)
780+
} enumerateKeysAndObjectsUsingBlock:^(NSPredicate *predicate, NSNumber *expectedCount, BOOL *stop) {
781+
782+
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"Post"];
783+
req.predicate = predicate;
784+
785+
NSUInteger count = [context countForFetchRequest:req error:&error];
786+
787+
XCTAssertFalse(count == NSNotFound, @"Error with fetch: %@", error);
788+
XCTAssertEqual(count, [expectedCount unsignedIntegerValue], @"Incorrect fetch count");
789+
}];
790+
}
791+
760792
@end

0 commit comments

Comments
 (0)