]> err.no Git - sope/blob - sope-gdl1/PostgreSQL/gdltest.m
renamed PostgreSQL72 to PostgreSQL, install in Library/GDLAdaptors-1.1
[sope] / sope-gdl1 / PostgreSQL / gdltest.m
1 // $Id: gdltest.m 1 2004-08-20 10:38:46Z znek $
2
3 #import <Foundation/Foundation.h>
4 #import <EOAccess/EOAccess.h>
5 #include <NGExtensions/NGExtensions.h>
6
7 int main(int argc, char **argv, char **env) {
8   EOModel          *m = nil;
9   EOAdaptor        *a;
10   EOAdaptorContext *ctx;
11   EOAdaptorChannel *ch;
12   NSDictionary     *conDict;
13   NSString         *expr;
14   
15   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
16
17   NS_DURING {
18   
19   conDict = [NSDictionary dictionaryWithContentsOfFile:@"condict.plist"];
20   NSLog(@"condict is %@", conDict);
21   
22   if ((a = [EOAdaptor adaptorWithName:@"PostgreSQL"]) == nil) {
23     NSLog(@"found no PostgreSQL adaptor ..");
24     exit(1);
25   }
26   
27   NSLog(@"got adaptor %@", a);
28   [a setConnectionDictionary:conDict];
29   NSLog(@"got adaptor with condict %@", a);
30   
31   ctx = [a   createAdaptorContext];
32   ch  = [ctx createAdaptorChannel];
33
34 #if 1
35   m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]);
36   if (m) {
37     [a setModel:m];
38     [a setConnectionDictionary:conDict];
39   }
40 #endif
41   
42   expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"];
43
44   NSLog(@"opening channel ..");
45
46   [ch setDebugEnabled:YES];
47   
48   if ([ch openChannel]) {
49     NSLog(@"channel is open");
50     
51     if ([ctx beginTransaction]) {
52       NSLog(@"began tx ..");
53
54       /* do something */
55       {
56         NSAutoreleasePool *pool = [NSAutoreleasePool new];
57         EOEntity *e;
58         EOSQLQualifier *q;
59         NSArray *attrs;
60
61 #if 1
62         /* fetch some expr */
63
64         if (expr) {
65           if ([ch evaluateExpression:expr]) {
66             NSDictionary *record;
67
68             attrs = [ch describeResults];
69             NSLog(@"results: %@", attrs);
70             
71             while ((record = [ch fetchAttributes:attrs withZone:nil]))
72               NSLog(@"fetched %@", record);
73           }
74         }
75 #endif
76         /* fetch some doof records */
77
78         e = [m entityNamed:@"Doof"];
79         NSLog(@"entity: %@", e);
80         if (e == nil)
81           exit(1);
82         
83         q = [e qualifier];
84         attrs = [e attributes];
85
86         if ([ch selectAttributes:attrs
87                 describedByQualifier:q
88                 fetchOrder:nil
89                 lock:NO]) {
90           NSDictionary *record;
91
92           while ((record = [ch fetchAttributes:attrs withZone:nil])) {
93             NSLog(@"fetched %@ birthday %@",
94                   [record valueForKey:@"pkey"],
95                   [record valueForKey:@"companyId"]);
96           }
97         }
98         else
99           NSLog(@"Could not select ..");
100
101         /* fetch some team records */
102
103         if ((e = [m entityNamed:@"Team"])) {
104           q = [e qualifier];
105           attrs = [e attributes];
106
107           if ([ch selectAttributes:attrs
108                   describedByQualifier:q
109                   fetchOrder:nil
110                   lock:NO]) {
111             NSDictionary *record;
112
113             while ((record = [ch fetchAttributes:attrs withZone:nil])) {
114               NSLog(@"fetched %@ birthday %@",
115                     [record valueForKey:@"description"],
116                     [record valueForKey:@"companyId"]);
117             }
118           }
119           else
120             NSLog(@"Could not select ..");
121         }
122         
123         /* do some update */
124
125         if ((e = [m entityNamed:@"Person"])) {
126           attrs = [e attributes];
127           q = [[EOSQLQualifier alloc]
128                                initWithEntity:e
129                                qualifierFormat:@"%A='helge'", @"login"];
130           AUTORELEASE(q);
131
132           if ([ch selectAttributes:attrs
133                   describedByQualifier:q
134                   fetchOrder:nil
135                   lock:NO]) {
136             NSDictionary *record;
137
138             record = [ch fetchAttributes:attrs withZone:nil];
139           }
140           else
141             NSLog(@"Could not select ..");
142         }
143
144         RELEASE(pool);
145       }
146       
147       NSLog(@"committing tx ..");
148       if ([ctx commitTransaction])
149         NSLog(@"  could commit.");
150       else
151         NSLog(@"  commit failed.");
152     }
153     
154     NSLog(@"closing channel ..");
155     [ch closeChannel];
156   }
157   }
158   NS_HANDLER {
159     fprintf(stderr, "exception: %s\n", [[localException description] cString]);
160     abort();
161   }
162   NS_ENDHANDLER;
163
164   return 0;
165 }