File: eoexample.m

package info (click to toggle)
gnustep-dl2 0.12.0-9%2Bnmu1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,232 kB
  • sloc: objc: 61,721; makefile: 82; sh: 22
file content (62 lines) | stat: -rw-r--r-- 1,603 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <Foundation/Foundation.h>
#include <EOAccess/EOAccess.h>
#include <EOControl/EOControl.h>

int
main(int arcg, char *argv[], char **envp)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  EOModelGroup *group = [EOModelGroup defaultGroup];
  EOModel *model;
  EOAdaptor *adaptor;
  EOAdaptorContext *context;
  EOAdaptorChannel *channel; 
  EOEditingContext *ec;
  EODatabaseDataSource *authorsDS;
  NSArray *authors;
  id author;
  
  model = [group modelNamed:@"library"];
  
  /* Tools don't have resources so we have to add the model manually */ 
  if (!model)
    {
      NSString *path = @"./library.eomodel";
      model = [[EOModel alloc] initWithContentsOfFile: path];
      [group addModel:model];
      [model release];
    }

  adaptor = [EOAdaptor adaptorWithModel:model];
  context = [adaptor createAdaptorContext];
  channel = [context createAdaptorChannel];
  ec = [[EOEditingContext alloc] init];
  authorsDS 
    = [[EODatabaseDataSource alloc] initWithEditingContext: ec
				    entityName:@"authors"];

  [channel openChannel];

  /* Create a new author object */
  author = [authorsDS createObject];
  [author takeValue:@"Anonymous" forKey:@"name"];
  [authorsDS insertObject:author];
  [ec saveChanges];

  
  /* Fetch the newly inserted object from the database */
  authors = [authorsDS fetchObjects];
  NSLog(@"%@", authors);

  /* Update the authors name */
  [[authors objectAtIndex:0] 
  	takeValue:@"John Doe" forKey:@"name"];
  [ec saveChanges];
 
  NSLog(@"%@", [authorsDS fetchObjects]);

  [channel closeChannel];
  [pool release];
  return 0;
}