|
|
Sample Programs from NeXSTEP 3.3
/*--------------------------------------------------------------------------
*
* You may freely copy, distribute, and reuse the code in this example.
* SHL Systemhouse disclaims any warranty of any kind, expressed or
* implied, as to its fitness for any particular use.
*
*
* DataSource
*
* Inherits From: NSObject
*
* Conforms To: EOQualifiedDataSources, EORollbackDataSources
*
* Declared In: DataSource.h
*
*
*------------------------------------------------------------------------*/
#import "DataSource.h"
#import "Person.h"
#import <foundation/NSUtilities.h>
#import <appkit/Application.h>
@implementation DataSource
/*--------------------------------------------------------------------------
* EODataSources Protocol
*
* The EODataSources protocol defines the interface for a source of
* data-bearing objects retrieved from some external store, such as an RDBMS.
* EODataSources uses a simple insert/delete/update/fetch model.
*
* Changes to the objects provided by a data source are made in two phases.
* First, you can modify an object independently of the data source. These
* changes don't affect the external store until you send an -insertObject:,
* -deleteObject:, or -updateObject: message. For example, if you release an
* object you received from the data source, it isn't deleted from the
* external store. Invoking one of the messages listed sends the changes
* associated with the object to the external store. You must invoke
* -saveObjects to make your changes permanent. If an external store supports
* rolling back of changes you can invoke -rollback (declared in the
* EORollbackDataSources protocol) to undo the changes made since the last
* -saveObjects message.
*
*------------------------------------------------------------------------*/
- (NSArray *) keys
{
// Returns the names of the keys that describe the data-bearing objects.
return [NSArray arrayWithObject: @"Model"];
}
- createObject
{
// Returns a new data bearing object with no values set, or nil if the
// data source won't allow object insertion. You're responsible for
// assigning a proper primary key.
return [[Person alloc] init];
}
- coerceValue: value forKey: (NSString *)key
{
return value;
}
- (BOOL) insertObject: object
{
// Inserts object into the data source. Returns YES on success, NO on
// failure for any reason.
if ([object isKindOf: [Person class]] == NO) return NO;
if (workingStore == nil)
workingStore = [[NSMutableArray allocWithZone: [self zone]]
initWithCapacity: 1];
[workingStore addObject: object];
return YES;
}
- (BOOL) deleteObject: object
{
// Deletes object from the data source. Returns YES on success, NO on
// failure for any reason.
if ([object isKindOf: [Person class]] == NO) return NO;
if (workingStore == nil) return NO;
[workingStore removeObject: object];
return YES;
}
- (BOOL) updateObject: object
{
// Saves changes to object to the data source. Returns YES on success,
// NO on failure for any reason.
return YES;
}
- (NSArray *) fetchObjects
{
// Returns an array of the data-bearing objects in the data source.
return persistentStore;
}
- (BOOL) saveObjects
{
// Saves objects to persistent storage, if needed. Returns YES on
// success, NO on failure for any reason.
if (persistentStore == nil)
persistentStore = [[NSMutableArray allocWithZone: [self zone]]
initWithCapacity: [workingStore count]];
[persistentStore removeAllObjects];
[persistentStore addObjectsFromArray: workingStore];
return YES;
}
- (BOOL) canDelete
{
// Returns YES if the data source allows objects to be deleted, NO if it
// doesn't.
return YES;
}
/*--------------------------------------------------------------------------
* EOQualifiedDataSources Protocol
*
* The EOQualifiedDataSources protocol decalres methods that must be
* implemented by data sources that provide sub data sources rooted in the
* super data source.
*
*------------------------------------------------------------------------*/
- (NSArray *) keysForPath: (NSString *)aPath
{
// The path supplied is a concatenation of all the keys used to get to
// a detail datasource with each key separated by a '.'; for example,
// "toAuthor.toPublisher.address".
return nil;
}
- (id <EODataSources>) dataSourceQualifiedByKey:(NSString *)key ofObject:object
{
// Returns a data source that supplies objects associated with object's
// key. This can be used for creating master-detail data sources.
return nil;
}
/*--------------------------------------------------------------------------
* EORollbackDataSources Protocol
*------------------------------------------------------------------------*/
- (void) rollback
{
// Reverses any changes made by -insertObject:, -deleteObject:, or
// -updateObject: since the data source was last sent a -saveObjects
// message.
[workingStore removeAllObjects];
[workingStore addObjectsFromArray: persistentStore];
}
@end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.