This is DataSource.m in view mode; [Download] [Up]
/*-------------------------------------------------------------------------- * * 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 "Motorcycle.h" #import <foundation/NSUtilities.h> #import <appkit/Application.h> #define print_trace [[NXApp delegate] perform: @selector (console:) with:\ [NSString stringWithFormat: @"DATASOURCE %s\n", sel_getName(_cmd)]] @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. print_trace; 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. print_trace; return [[Motorcycle alloc] init]; } - (BOOL) insertObject: object { // Inserts object into the data source. Returns YES on success, NO on // failure for any reason. print_trace; if ([object isKindOf: [Motorcycle 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. print_trace; if ([object isKindOf: [Motorcycle 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. print_trace; return YES; } - (NSArray *) fetchObjects { // Returns an array of the data-bearing objects in the data source. print_trace; return persistentStore; } - (BOOL) saveObjects { // Saves objects to persistent storage, if needed. Returns YES on // success, NO on failure for any reason. print_trace; 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. print_trace; 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". print_trace; 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. print_trace; 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. print_trace; [workingStore removeAllObjects]; [workingStore addObjectsFromArray: persistentStore]; } @end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.