2007-01-06 17:21:18 +00:00
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#include <EOAccess/EOAccess.h>
|
|
|
|
#include <EOControl/EOControl.h>
|
|
|
|
|
2007-05-01 20:51:18 +00:00
|
|
|
int
|
|
|
|
main(int arcg, char *argv[], char **envp)
|
2007-01-06 17:21:18 +00:00
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
EOModelGroup *group = [EOModelGroup defaultGroup];
|
|
|
|
EOModel *model;
|
|
|
|
EOAdaptor *adaptor;
|
|
|
|
EOAdaptorContext *context;
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
EOEditingContext *ec;
|
|
|
|
EODatabaseDataSource *authorsDS;
|
|
|
|
EODataSource *booksDS;
|
|
|
|
id author;
|
|
|
|
id book;
|
|
|
|
|
|
|
|
model = [group modelNamed:@"library"];
|
|
|
|
|
2007-05-01 20:51:18 +00:00
|
|
|
/* Tools do not have resources so we add the model manually. */
|
2007-01-06 17:21:18 +00:00
|
|
|
if (!model)
|
|
|
|
{
|
2007-05-01 20:51:18 +00:00
|
|
|
NSString *path = @"./library.eomodel";
|
|
|
|
model = [[EOModel alloc] initWithContentsOfFile: path];
|
2007-01-06 17:21:18 +00:00
|
|
|
[group addModel:model];
|
2007-05-01 20:51:18 +00:00
|
|
|
[model release];
|
2007-01-06 17:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
adaptor = [EOAdaptor adaptorWithModel:model];
|
|
|
|
context = [adaptor createAdaptorContext];
|
|
|
|
channel = [context createAdaptorChannel];
|
|
|
|
ec = [[EOEditingContext alloc] init];
|
2007-05-01 20:51:18 +00:00
|
|
|
authorsDS
|
|
|
|
= [[EODatabaseDataSource alloc] initWithEditingContext: ec
|
|
|
|
entityName:@"authors"];
|
2007-01-06 17:21:18 +00:00
|
|
|
|
|
|
|
[channel openChannel];
|
|
|
|
|
|
|
|
author = [authorsDS createObject];
|
|
|
|
[author takeValue:@"Richard Brautigan" forKey:@"name"];
|
|
|
|
[authorsDS insertObject:author];
|
|
|
|
|
|
|
|
booksDS = [authorsDS dataSourceQualifiedByKey:@"toBooks"];
|
|
|
|
[booksDS qualifyWithRelationshipKey:@"toBooks" ofObject:author];
|
|
|
|
|
|
|
|
book = [booksDS createObject];
|
|
|
|
[book takeValue:@"The Hawkline Monster" forKey:@"title"];
|
|
|
|
[booksDS insertObject:book];
|
|
|
|
|
|
|
|
book = [booksDS createObject];
|
|
|
|
[book takeValue:@"Trout Fishing in America" forKey:@"title"];
|
|
|
|
[booksDS insertObject:book];
|
|
|
|
|
|
|
|
[ec saveChanges];
|
|
|
|
|
|
|
|
/* log the to many relationship from author to books */
|
2007-05-01 20:51:18 +00:00
|
|
|
NSLog(@"%@ %@",
|
|
|
|
[author valueForKey:@"name"],
|
|
|
|
[author valueForKeyPath:@"toBooks.title"]);
|
2007-01-06 17:21:18 +00:00
|
|
|
|
|
|
|
/* log the to one relationship from book to author */
|
|
|
|
NSLog(@"%@", [book valueForKeyPath:@"toAuthor.name"]);
|
|
|
|
|
|
|
|
/* traverse to one through the to many through key paths
|
|
|
|
logging the author once for each book. */
|
|
|
|
NSLog(@"%@", [author valueForKeyPath:@"toBooks.toAuthor.name"]);
|
|
|
|
|
|
|
|
[channel closeChannel];
|
|
|
|
[pool release];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|