Browse files

Fixed #27: NSFNanoSearch should filter objects of a specific class

  • Loading branch information...
1 parent 9c72853 commit 9ea7a1af47a79ba5b56eab65a90568d8b9ab4dd0 @tciuro committed May 26, 2012
View
2 Classes/Public/NSFNanoSearch.h
@@ -174,6 +174,8 @@
@property (nonatomic, copy, readonly) NSString *sql;
/** * The sort holds an array of one or more sort descriptors of type \link NSFNanoSortDescriptor NSFNanoSortDescriptor \endlink. */
@property (nonatomic, strong, readwrite) NSArray *sort;
+/** * The filterClass allows to filter the results based on a specific object class. */
+@property (nonatomic, strong, readwrite) NSString *filterClass;
/** @name Creating and Initializing a Search
*/
View
16 Classes/Public/NSFNanoSearch.m
@@ -37,7 +37,7 @@ @implementation NSFNanoSearch
}
-@synthesize nanoStore, attributesToBeReturned, key, attribute, value, match, expressions, groupValues, sql, sort;
+@synthesize nanoStore, attributesToBeReturned, key, attribute, value, match, expressions, groupValues, sql, sort, filterClass;
// ----------------------------------------------
// Initialization / Cleanup
@@ -83,7 +83,7 @@ - (NSString *)sql
return sql;
}
-- (NSString*)description
+- (NSString *)description
{
NSMutableString *description = [NSMutableString string];
@@ -96,9 +96,10 @@ - (NSString*)description
[description appendString:[NSString stringWithFormat:@"Value : %@\n", value]];
[description appendString:[NSString stringWithFormat:@"Match : %@\n", NSFStringFromMatchType(match)]];
[description appendString:[NSString stringWithFormat:@"Expressions : %@\n", expressions]];
- [description appendString:[NSString stringWithFormat:@"Group Values? : %@\n", (groupValues ? @"YES" : @"NO")]];
+ [description appendString:[NSString stringWithFormat:@"Group values? : %@\n", (groupValues ? @"YES" : @"NO")]];
[description appendString:[NSString stringWithFormat:@"Sort : %@\n", sort]];
-
+ [description appendString:[NSString stringWithFormat:@"Filter class : %@\n", filterClass]];
+
return description;
}
@@ -576,10 +577,11 @@ - (NSString *)_prepareSQLQueryStringWithKey:(NSString *)aKey attribute:(NSString
}
if (NSFReturnObjects == returnType) {
- if (nil != attributes)
- theSQLStatement = [NSString stringWithFormat:@"SELECT DISTINCT (NSFKey),NSFPlist,NSFObjectClass FROM NSFKeys WHERE NSFKey IN (%@)", theSQLStatement];
- else
+ if (self.filterClass.length > 0) {
+ theSQLStatement = [NSString stringWithFormat:@"SELECT DISTINCT (NSFKey),NSFPlist,NSFObjectClass FROM NSFKeys WHERE (NSFObjectClass = '%@') AND NSFKey IN (%@)", self.filterClass, theSQLStatement];
+ } else {
theSQLStatement = [NSString stringWithFormat:@"SELECT DISTINCT (NSFKey),NSFPlist,NSFObjectClass FROM NSFKeys WHERE NSFKey IN (%@)", theSQLStatement];
+ }
}
return theSQLStatement;
View
24 NanoStore.xcodeproj/project.pbxproj
@@ -26,6 +26,10 @@
7400C28B122450C60066D2B5 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D69BFE84028FC02AAC07 /* Foundation.framework */; };
74116B0D1538E9C600AEAD62 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 74116B091538E9C600AEAD62 /* InfoPlist.strings */; };
74116B141538E9CE00AEAD62 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 74116B101538E9CE00AEAD62 /* InfoPlist.strings */; };
+ 7412893A15718D90002D5138 /* NanoCarTestClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 7412893915718D90002D5138 /* NanoCarTestClass.m */; };
+ 7412893B15718D90002D5138 /* NanoCarTestClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 7412893915718D90002D5138 /* NanoCarTestClass.m */; };
+ 7412893E15718F2C002D5138 /* NanoPersonTestClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 7412893D15718F2C002D5138 /* NanoPersonTestClass.m */; };
+ 7412893F15718F2C002D5138 /* NanoPersonTestClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 7412893D15718F2C002D5138 /* NanoPersonTestClass.m */; };
748D3454139772B600FD5565 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 748D3453139772B600FD5565 /* Foundation.framework */; };
748D34701397730900FD5565 /* NSFNanoEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 74B63DD1129F25C400B3B2A7 /* NSFNanoEngine.m */; };
748D34721397730900FD5565 /* NSFNanoResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 74B63DD3129F25C400B3B2A7 /* NSFNanoResult.m */; };
@@ -170,6 +174,10 @@
74116B111538E9CE00AEAD62 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
74116B121538E9CE00AEAD62 /* UnitTestMac-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "UnitTestMac-Info.plist"; sourceTree = "<group>"; };
74116B131538E9CE00AEAD62 /* UnitTestMac-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UnitTestMac-Prefix.pch"; sourceTree = "<group>"; };
+ 7412893815718D90002D5138 /* NanoCarTestClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NanoCarTestClass.h; sourceTree = "<group>"; };
+ 7412893915718D90002D5138 /* NanoCarTestClass.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NanoCarTestClass.m; sourceTree = "<group>"; };
+ 7412893C15718F2C002D5138 /* NanoPersonTestClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NanoPersonTestClass.h; sourceTree = "<group>"; };
+ 7412893D15718F2C002D5138 /* NanoPersonTestClass.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NanoPersonTestClass.m; sourceTree = "<group>"; };
744510DA128A2EA8008A39B5 /* NanoEngineTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NanoEngineTests.h; sourceTree = "<group>"; };
744510DB128A2EA8008A39B5 /* NanoEngineTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NanoEngineTests.m; sourceTree = "<group>"; };
745D1E92128B9DCA00D4653D /* NanoStoreResultTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NanoStoreResultTests.h; sourceTree = "<group>"; };
@@ -365,6 +373,17 @@
path = UnitTestMac;
sourceTree = SOURCE_ROOT;
};
+ 7412893615718D60002D5138 /* TestClasses */ = {
+ isa = PBXGroup;
+ children = (
+ 7412893815718D90002D5138 /* NanoCarTestClass.h */,
+ 7412893915718D90002D5138 /* NanoCarTestClass.m */,
+ 7412893C15718F2C002D5138 /* NanoPersonTestClass.h */,
+ 7412893D15718F2C002D5138 /* NanoPersonTestClass.m */,
+ );
+ path = TestClasses;
+ sourceTree = "<group>";
+ };
748D3452139772B600FD5565 /* Frameworks */ = {
isa = PBXGroup;
children = (
@@ -464,6 +483,7 @@
74A4EF19138E47B100FBC46C /* NanoStoreSortTests.m */,
74B2098C122B280C0079E2FF /* NanoStoreTests.h */,
74B2098D122B280C0079E2FF /* NanoStoreTests.m */,
+ 7412893615718D60002D5138 /* TestClasses */,
);
path = NanoStore;
sourceTree = "<group>";
@@ -791,6 +811,8 @@
74C1FAB41538DDF20077DAD1 /* NanoStoreSortTests.m in Sources */,
74C1FAB51538DDF20077DAD1 /* NanoStoreTests.m in Sources */,
748EB00A153F09A600F5C7F2 /* NanoStoreGlobalsTests.m in Sources */,
+ 7412893A15718D90002D5138 /* NanoCarTestClass.m in Sources */,
+ 7412893E15718F2C002D5138 /* NanoPersonTestClass.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -818,6 +840,8 @@
74C1FAE21538E1740077DAD1 /* NanoStoreTests.m in Sources */,
748EB00B153F09A600F5C7F2 /* NanoStoreGlobalsTests.m in Sources */,
74FA5DE6155795CC00217E09 /* fopenCompatibilityFix.c in Sources */,
+ 7412893B15718D90002D5138 /* NanoCarTestClass.m in Sources */,
+ 7412893F15718F2C002D5138 /* NanoPersonTestClass.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
31 Tests/UnitTests/NanoStore/NanoStoreSearchTests.m
@@ -12,6 +12,8 @@
#import "NSFNanoStore_Private.h"
#import "NSFNanoObject_Private.h"
#import "NSFNanoSortDescriptor.h"
+#import "NanoCarTestClass.h"
+#import "NanoPersonTestClass.h"
@implementation NanoStoreSearchTests
@@ -637,6 +639,35 @@ - (void)testSearchReturningObjectsWithCalendarDateOfClassNSFNanoObject
STAssertTrue (([searchResults count] == 2) && (YES == isClassCorrect), @"Expected to find two objects of type NSFNanoObject.");
}
+- (void)testSearchFilteringResultsByClass
+{
+ NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFPersistentStoreType path:[@"~/Desktop/test.db" stringByExpandingTildeInPath] error:nil];
+ [nanoStore removeAllObjectsFromStoreAndReturnError:nil];
+
+ NanoCarTestClass *car = [NanoCarTestClass new];
+ car.name = @"Mercedes";
+ car.key = [NSFNanoEngine stringWithUUID];
+
+ NanoPersonTestClass *person = [NanoPersonTestClass new];
+ person.name = @"Mercedes";
+ person.last = @"Doe";
+ person.key = [NSFNanoEngine stringWithUUID];
+
+ [nanoStore addObjectsFromArray:[NSArray arrayWithObjects:car, person, nil] error:nil];
+
+ NSFNanoSearch *search = [NSFNanoSearch searchWithStore:nanoStore];
+ search.attribute = @"kName";
+ search.match = NSFEqualTo;
+ search.value = @"Mercedes";
+ search.filterClass = NSStringFromClass([NanoCarTestClass class]);
+
+ NSDictionary *searchResults = [search searchObjectsWithReturnType:NSFReturnObjects error:nil];
+ BOOL isClassCorrect = [[searchResults objectForKey:car.key]isKindOfClass:[NanoCarTestClass class]];
+ [nanoStore closeWithError:nil];
+
+ STAssertTrue (([searchResults count] == 1) && (YES == isClassCorrect), @"Expected to find one object of type NanoCarTestClass.");
+}
+
#pragma mark -
- (void)testSearchObjectKnownInThisProcess
View
18 Tests/UnitTests/NanoStore/TestClasses/NanoCarTestClass.h
@@ -0,0 +1,18 @@
+//
+// NanoCarTestClass.h
+// NanoStore
+//
+// Created by Tito Ciuro on 5/26/12.
+// Copyright (c) 2012 Webbo, LLC. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "NSFNanoObjectProtocol.h"
+
+@interface NanoCarTestClass : NSObject <NSFNanoObjectProtocol>
+
+@property (nonatomic, strong) NSString *name;
+@property (nonatomic, strong) NSString *key;
+
+@end
View
43 Tests/UnitTests/NanoStore/TestClasses/NanoCarTestClass.m
@@ -0,0 +1,43 @@
+//
+// NanoCarTestClass.m
+// NanoStore
+//
+// Created by Tito Ciuro on 5/26/12.
+// Copyright (c) 2012 Webbo, LLC. All rights reserved.
+//
+
+#import "NanoCarTestClass.h"
+
+#define kName @"kName"
+
+@implementation NanoCarTestClass
+
+@synthesize name;
+@synthesize key;
+
+- (id)initNanoObjectFromDictionaryRepresentation:(NSDictionary *)theDictionary forKey:(NSString *)aKey store:(NSFNanoStore *)theStore
+{
+ if (self = [self init]) {
+ self.name = [theDictionary objectForKey:kName];
+ self.key = aKey;
+ }
+
+ return self;
+}
+
+- (NSDictionary *)nanoObjectDictionaryRepresentation
+{
+ return [NSDictionary dictionaryWithObject:self.name forKey:kName];
+}
+
+- (NSString *)nanoObjectKey
+{
+ return self.key;
+}
+
+- (id)rootObject
+{
+ return self;
+}
+
+@end
View
19 Tests/UnitTests/NanoStore/TestClasses/NanoPersonTestClass.h
@@ -0,0 +1,19 @@
+//
+// NanoPersonTestClass.h
+// NanoStore
+//
+// Created by Tito Ciuro on 5/26/12.
+// Copyright (c) 2012 Webbo, LLC. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "NSFNanoObjectProtocol.h"
+
+@interface NanoPersonTestClass : NSObject <NSFNanoObjectProtocol>
+
+@property (nonatomic, strong) NSString *name;
+@property (nonatomic, strong) NSString *last;
+@property (nonatomic, strong) NSString *key;
+
+@end
View
48 Tests/UnitTests/NanoStore/TestClasses/NanoPersonTestClass.m
@@ -0,0 +1,48 @@
+//
+// NanoPersonTestClass.m
+// NanoStore
+//
+// Created by Tito Ciuro on 5/26/12.
+// Copyright (c) 2012 Webbo, LLC. All rights reserved.
+//
+
+#import "NanoPersonTestClass.h"
+
+#define kName @"kName"
+#define kLast @"kLast"
+
+@implementation NanoPersonTestClass
+
+@synthesize name;
+@synthesize last;
+@synthesize key;
+
+- (id)initNanoObjectFromDictionaryRepresentation:(NSDictionary *)theDictionary forKey:(NSString *)aKey store:(NSFNanoStore *)theStore
+{
+ if (self = [self init]) {
+ self.name = [theDictionary objectForKey:kName];
+ self.last = [theDictionary objectForKey:kLast];
+ self.key = aKey;
+ }
+
+ return self;
+}
+
+- (NSDictionary *)nanoObjectDictionaryRepresentation
+{
+ return [NSDictionary dictionaryWithObjectsAndKeys:self.name, kName,
+ self.last, kLast,
+ nil];
+}
+
+- (NSString *)nanoObjectKey
+{
+ return self.key;
+}
+
+- (id)rootObject
+{
+ return self;
+}
+
+@end

0 comments on commit 9ea7a1a

Please sign in to comment.