forked from fjolnir/DatabaseKit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an NSArray style interface to ARBase
- Loading branch information
Showing
5 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// ARBaseArrayInterface.h | ||
// An NSArray style interface to ARBase records | ||
// | ||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright (c) 2007, Fjölnir Ásgeirsson | ||
// | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, | ||
// are permitted provided that the following conditions are met: | ||
// | ||
// Redistributions of source code must retain the above copyright notice, this list of conditions | ||
// and the following disclaimer. | ||
// Redistributions in binary form must reproduce the above copyright notice, this list of | ||
// conditions and the following disclaimer in the documentation and/or other materials provided with | ||
// the distribution. | ||
// Neither the name of Fjölnir Ásgeirsson, ninja kitten nor the names of its contributors may be | ||
// used to endorse or promote products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
#ifndef _ARBASE_ARRAYINTERFACE_H_ | ||
#define _ARBASE_ARRAYINTERFACE_H_ | ||
#import <Cocoa/Cocoa.h> | ||
#import <ActiveRecord/ARBase.h> | ||
|
||
/*! | ||
* An NSArray style interface to ARBase records\n | ||
* \n | ||
* The interface is by no means compatible with NSMutableArray. It implements only what's required to | ||
* implement binding support for ActiveRecord\n | ||
* \n | ||
* To use ARBaseArrayInterface, subclass it with a class named <prefix>ModelNameArrayInterface | ||
* ARBaseArrayInterface will then determine the name of the model to use (<prefix>ModelNameArrayInterface -> modelname)\n | ||
* \n | ||
* <b>Do not</b> use ARBaseArrayInterface directly. Hic sunt dracones | ||
*/ | ||
@interface ARBaseArrayInterface : NSObject { | ||
NSMutableDictionary *queryInfo; | ||
} | ||
@property(readwrite, retain) NSMutableDictionary *queryInfo; | ||
|
||
/*! Finds records based on the find specification. | ||
* @param idOrSpecification The find specification | ||
*/ | ||
+ (id)find:(ARFindSpecification)idOrSpecification; | ||
/*! Finds records based on the find specification, filter and limit using the specified connection. | ||
* @param idOrSpecification The find specification | ||
* @param limit The maximum number of records to retrieve | ||
*/ | ||
+ (id)find:(ARFindSpecification)idOrSpecification connection:(id<ARConnection>)connection; | ||
/*! Finds records based on the find specification, filter and limit. | ||
* @param idOrSpecification The find specification | ||
* @param whereSQL A valid SQL WHERE statement (omitting the actual "WHERE") | ||
* @param orderSQL A valud SQL ORDER statement (omitting the actual "ORDER BY") | ||
* @param limit The maximum number of records to retrieve | ||
*/ | ||
+ (id)find:(ARFindSpecification)idOrSpecification | ||
filter:(NSString *)whereSQL | ||
join:(NSString *)joinSQL | ||
order:(NSString *)orderSQL | ||
limit:(NSUInteger)limit; | ||
|
||
/*! Finds records based on the find specification, filter and limit using the specified connection. | ||
* @param idOrSpecification The find specification | ||
* @param whereSQL A valid SQL WHERE statement (omitting the actual "WHERE") | ||
* @param orderSQL A valud SQL ORDER statement (omitting the actual "ORDER BY") | ||
* @param limit The maximum number of records to retrieve | ||
* @param connection The connection to use for the record. (Pass nil to use the default connection) | ||
*/ | ||
+ (id)find:(ARFindSpecification)idOrSpecification | ||
filter:(NSString *)whereSQL | ||
join:(NSString *)joinSQL | ||
order:(NSString *)orderSQL | ||
limit:(NSUInteger)limit | ||
connection:(id<ARConnection>)aConnection; | ||
|
||
/*! Returns the class name of the model to use */ | ||
+ (NSString *)modelName; | ||
/*! Returns the model class to use */ | ||
+ (Class)modelClass; | ||
|
||
/*! Returns an array of dictionaries containing the database ids matching records */ | ||
- (NSArray *)matchingIds; | ||
/*! Returns an array of records matching the find specification */ | ||
- (NSArray *)allObjects; | ||
|
||
/*! Returns the nth record matching the find specification\n | ||
* @param index The index to look up | ||
*/ | ||
- (id)objectAtIndex:(NSUInteger)index; | ||
/*! Returns the last record matching the find specification */ | ||
- (id)lastObject; | ||
|
||
/*! Adds a record to the database | ||
* @param object A NSDictionary containing attributes for the record. | ||
*/ | ||
- (void)addObject:(NSDictionary *)attributes; | ||
|
||
/*! Destroys the nth record matching the find specification | ||
* @param index The index of the record to destroy | ||
*/ | ||
- (BOOL)removeObjectAtIndex:(NSUInteger)index; | ||
|
||
/*! Returns the number of records matching the find specification */ | ||
- (NSUInteger)count; | ||
|
||
/*! Returns an array containing the results of invoking valueForKey: using key on each of the receiver's records. */ | ||
- (NSArray *)valueForKey:(NSString *)key; | ||
/*! Invokes setValue:forKey: on each of the receiver's records using the specified value and key. */ | ||
- (void)setValue:(id)value forKey:(NSString *)key; | ||
@end | ||
#endif _ARBASE_ARRAYINTERFACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// ARBaseArrayInterface.m | ||
// ActiveRecord | ||
// | ||
// Created by Fjölnir Ásgeirsson on 23.8.2008. | ||
// Copyright 2008 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "ARBaseArrayInterface.h" | ||
|
||
|
||
@implementation ARBaseArrayInterface | ||
@synthesize queryInfo; | ||
#pragma mark - | ||
#pragma mark Creation | ||
+ (id)find:(ARFindSpecification)idOrSpecification | ||
{ | ||
return [self find:idOrSpecification | ||
connection:[ARBase defaultConnection]]; | ||
} | ||
+ (id)find:(ARFindSpecification)idOrSpecification connection:(id<ARConnection>)connection | ||
{ | ||
return [self find:idOrSpecification | ||
filter:nil | ||
join:nil | ||
order:nil | ||
limit:0 | ||
connection:connection]; | ||
} | ||
+ (id)find:(ARFindSpecification)idOrSpecification | ||
filter:(NSString *)whereSQL | ||
join:(NSString *)joinSQL | ||
order:(NSString *)orderSQL | ||
limit:(NSUInteger)limit | ||
{ | ||
return [self find:idOrSpecification | ||
filter:whereSQL | ||
join:joinSQL | ||
order:orderSQL | ||
limit:limit | ||
connection:[ARBase defaultConnection]]; | ||
} | ||
|
||
+ (id)find:(ARFindSpecification)idOrSpecification | ||
filter:(NSString *)whereSQL | ||
join:(NSString *)joinSQL | ||
order:(NSString *)orderSQL | ||
limit:(NSUInteger)limit | ||
connection:(id<ARConnection>)aConnection | ||
{ | ||
ARBaseArrayInterface *ret = [[self alloc] init]; | ||
[ret.queryInfo setObject:[NSNumber numberWithInt:idOrSpecification] forKey:@"findSpecification"]; | ||
if(whereSQL) | ||
[ret.queryInfo setObject:whereSQL forKey:@"whereSQL"]; | ||
if(joinSQL) | ||
[ret.queryInfo setObject:joinSQL forKey:@"joinSQL"]; | ||
if(orderSQL) | ||
[ret.queryInfo setObject:orderSQL forKey:@"orderSQL"]; | ||
[ret.queryInfo setObject:[NSNumber numberWithInt:limit] forKey:@"limit"]; | ||
if(aConnection) | ||
[ret.queryInfo setObject:aConnection forKey:@"connection"]; | ||
|
||
return [ret autorelease]; | ||
} | ||
- (id)init | ||
{ | ||
if(![super init]) | ||
return nil; | ||
self.queryInfo = [NSMutableDictionary dictionary]; | ||
|
||
return self; | ||
} | ||
|
||
#pragma mark - | ||
+ (NSString *)modelName | ||
{ | ||
NSMutableString *ret = [[[self className] mutableCopy] autorelease]; | ||
[ret replaceOccurrencesOfString:@"ArrayInterface" | ||
withString:@"" | ||
options:0 | ||
range:NSMakeRange(0, [ret length])]; | ||
return ret; | ||
} | ||
+ (Class)modelClass | ||
{ | ||
return NSClassFromString([self modelName]); | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark Database access | ||
- (NSArray *)matchingIds | ||
{ | ||
Class modelClass = [[self class] modelClass]; | ||
return [modelClass findIds:[[queryInfo objectForKey:@"findSpecification"] intValue] | ||
filter:[queryInfo objectForKey:@"whereSQL"] | ||
join:[queryInfo objectForKey:@"joinSQL"] | ||
order:[queryInfo objectForKey:@"orderSQL"] | ||
limit:[queryInfo objectForKey:@"limit"] | ||
connection:[queryInfo objectForKey:@"connection"]]; | ||
} | ||
- (NSArray *)allObjects | ||
{ | ||
Class modelClass = [[self class] modelClass]; | ||
return (NSArray *)[modelClass find:[[queryInfo objectForKey:@"findSpecification"] intValue] | ||
filter:[queryInfo objectForKey:@"whereSQL"] | ||
join:[queryInfo objectForKey:@"joinSQL"] | ||
order:[queryInfo objectForKey:@"orderSQL"] | ||
limit:[[queryInfo objectForKey:@"limit"] intValue] | ||
connection:[queryInfo objectForKey:@"connection"]]; | ||
} | ||
- (id)objectAtIndex:(NSUInteger)index | ||
{ | ||
// Fetch the id | ||
Class modelClass = [[self class] modelClass]; | ||
NSArray *ids = [self matchingIds]; | ||
if([ids count] > index) | ||
return [[[modelClass alloc] initWithId:[[[ids objectAtIndex:index] objectForKey:@"id"] unsignedIntValue]] autorelease]; | ||
return nil; | ||
} | ||
- (id)lastObject | ||
{ | ||
NSUInteger count = [self count]; | ||
if(count > 0) | ||
return [self objectAtIndex:count - 1]; | ||
return nil; | ||
} | ||
- (void)addObject:(NSDictionary *)attributes | ||
{ | ||
[[[self class] modelClass] createWithAttributes:attributes connection:[queryInfo objectForKey:@"connection"]]; | ||
} | ||
|
||
- (BOOL)removeObjectAtIndex:(NSUInteger)index | ||
{ | ||
return [[self objectAtIndex:index] destroy]; | ||
} | ||
|
||
- (NSUInteger)count | ||
{ | ||
return [[self matchingIds] count]; | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark KVC | ||
- (NSArray *)valueForKey:(NSString *)key | ||
{ | ||
NSMutableArray *ret = [NSMutableArray array]; | ||
for(ARBase *record in [self allObjects]) | ||
{ | ||
[ret addObject:[record valueForKey:key]]; | ||
} | ||
return ret; | ||
} | ||
- (void)setValue:(id)value forKey:(NSString *)key | ||
{ | ||
for(ARBase *record in [self allObjects]) | ||
{ | ||
[record setValue:value forKey:key]; | ||
} | ||
} | ||
@end |
Oops, something went wrong.