Skip to content

Commit

Permalink
Between statement added
Browse files Browse the repository at this point in the history
AlexDenisov committed May 29, 2012
1 parent 6f5401b commit 1df68c6
Showing 6 changed files with 89 additions and 2 deletions.
29 changes: 29 additions & 0 deletions UnitTests/ARLazyFetcherSpecs.h
Original file line number Diff line number Diff line change
@@ -15,6 +15,9 @@
#import "ARFactory.h"
#import "ARWhereStatement.h"

#define DAY (24*60*60)
#define MONTH (30*DAY)

SPEC_BEGIN(ARLazyFetcherSpecs)

beforeEach(^{
@@ -31,6 +34,32 @@ describe(@"LazyFetcher", ^{
NSArray *records = [[User lazyFetcher] fetchRecords];
expect([records count]).toEqual(10);
});

describe(@"where between", ^{
it(@"should fetch only records between two dates", ^{
[ActiveRecord clearDatabase];
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-MONTH];
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:DAY];
User *john = [User newRecord];
john.name = @"John";
john.createdAt = [NSDate dateWithTimeIntervalSinceNow:-MONTH * 2];
expect([john save]).toEqual(YES);
User *alex = [User newRecord];
alex.name = @"Alex";
alex.createdAt = [NSDate dateWithTimeIntervalSinceNow:-DAY];
expect([alex save]).toEqual(YES);
ARLazyFetcher *fetcher = [User lazyFetcher];
[fetcher whereField:@"createdAt"
between:startDate
and:endDate];
NSArray *users = [fetcher fetchRecords];
expect(users.count).toEqual(1);
[alex release];
[john release];

});
});

describe(@"Limit/Offset", ^{
it(@"LIMIT should return limited count of records", ^{
NSInteger limit = 5;
12 changes: 10 additions & 2 deletions iActiveRecord.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -566,6 +566,7 @@
25998EDC14BC19E200CA2490 /* Classes */ = {
isa = PBXGroup;
children = (
CAE051701574BFE100010AAE /* ARLazyFetcher */,
CA7DC75615639DF800B22EBF /* ARWhereStatement */,
CA1AF758155FEA4400D0B17E /* ARSchemaManager */,
CA1AF74F155FEA1D00D0B17E /* ARColumn */,
@@ -677,8 +678,6 @@
children = (
CAAE33911528C41B0080447D /* ARFactory.h */,
CAAE33921528C41B0080447D /* ARFactory.m */,
CA3D18041519D5FA00FA3F3C /* ARLazyFetcher.h */,
CA3D18051519D5FA00FA3F3C /* ARLazyFetcher.m */,
CAF24809151F298B00120198 /* ARError.h */,
CAF2480A151F298B00120198 /* ARError.m */,
CAAE33451528819C0080447D /* ARException.h */,
@@ -850,6 +849,15 @@
name = NSMutableDictionary;
sourceTree = "<group>";
};
CAE051701574BFE100010AAE /* ARLazyFetcher */ = {
isa = PBXGroup;
children = (
CA3D18041519D5FA00FA3F3C /* ARLazyFetcher.h */,
CA3D18051519D5FA00FA3F3C /* ARLazyFetcher.m */,
);
name = ARLazyFetcher;
sourceTree = "<group>";
};
CAF2480F151F375000120198 /* NSData */ = {
isa = PBXGroup;
children = (
6 changes: 6 additions & 0 deletions iActiveRecord/ARLazyFetcher.h
Original file line number Diff line number Diff line change
@@ -30,6 +30,12 @@ typedef enum {
- (ARLazyFetcher *)whereField:(NSString *)aField notIn:(NSArray *)aValues;
- (ARLazyFetcher *)whereField:(NSString *)aField like:(NSString *)aPattern;
- (ARLazyFetcher *)whereField:(NSString *)aField notLike:(NSString *)aPattern;
- (ARLazyFetcher *)whereField:(NSString *)aField between:(id)startValue and:(id)endValue;

- (ARLazyFetcher *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
between:(id)startValue
and:(id)endValue;

- (ARLazyFetcher *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
25 changes: 25 additions & 0 deletions iActiveRecord/ARLazyFetcher.m
Original file line number Diff line number Diff line change
@@ -274,6 +274,18 @@ - (ARLazyFetcher *)setWhereStatement:(ARWhereStatement *)aStatement {
return self;
}

- (ARLazyFetcher *)whereField:(NSString *)aField
between:(id)startValue
and:(id)endValue
{
ARWhereStatement *where = [ARWhereStatement whereField:aField
ofRecord:recordClass
between:startValue
and:endValue];
[self setWhereStatement:where];
return self;
}

- (ARLazyFetcher *)whereField:(NSString *)aField like:(NSString *)aPattern {
ARWhereStatement *where = [ARWhereStatement whereField:aField
ofRecord:recordClass
@@ -387,6 +399,19 @@ - (ARLazyFetcher *)whereField:(NSString *)aField
return self;
}

- (ARLazyFetcher *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
between:(id)startValue
and:(id)endValue
{
ARWhereStatement *where = [ARWhereStatement whereField:aField
ofRecord:aRecord
between:startValue
and:endValue];
[self setWhereStatement:where];
return self;
}

#pragma mark - OrderBy

- (ARLazyFetcher *)orderBy:(NSString *)aField
5 changes: 5 additions & 0 deletions iActiveRecord/ARWhereStatement.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,11 @@ typedef enum {
+ (ARWhereStatement *)whereField:(NSString *)aField ofRecord:(Class)aRecord like:(NSString *)aPattern;
+ (ARWhereStatement *)whereField:(NSString *)aField ofRecord:(Class)aRecord notLike:(NSString *)aPattern;

+ (ARWhereStatement *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
between:(id)startValue
and:(id)endValue;

+ (ARWhereStatement *)concatenateStatement:(ARWhereStatement *)aFirstStatement
withStatement:(ARWhereStatement *)aSecondStatement
useLogicalOperation:(ARLogicalOperation)logicalOperation;
14 changes: 14 additions & 0 deletions iActiveRecord/ARWhereStatement.m
Original file line number Diff line number Diff line change
@@ -62,6 +62,20 @@ + (ARWhereStatement *)whereField:(NSString *)aField equalToValue:(id)aValue {
return [ARWhereStatement statement:stmt];
}

+ (ARWhereStatement *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
between:(id)startValue
and:(id)endValue
{
NSString *stmt = [NSString stringWithFormat:
@" %@.%@ BETWEEN %@ AND %@",
[[aRecord performSelector:@selector(recordName)] quotedString],
[aField quotedString],
[[startValue performSelector:@selector(toSql)] quotedString],
[[endValue performSelector:@selector(toSql)] quotedString]];
return [ARWhereStatement statement:stmt];
}

+ (ARWhereStatement *)whereField:(NSString *)aField
ofRecord:(Class)aRecord
like:(NSString *)aPattern

0 comments on commit 1df68c6

Please sign in to comment.