]> err.no Git - sope/blob - sope-gdl1/FrontBase2/fbtest.m
renamed PostgreSQL72 to PostgreSQL, install in Library/GDLAdaptors-1.1
[sope] / sope-gdl1 / FrontBase2 / fbtest.m
1 // $Id: fbtest.m 1 2004-08-20 10:38:46Z znek $
2
3 #import <Foundation/Foundation.h>
4 #import <NGExtensions/NGExtensions.h>
5 #import <GDLAccess/EOAccess.h>
6 //#include "FBAdaptor.h"
7
8 #define ADAPTOR_LEVEL 1
9
10 unsigned txRepeatCount = 1;
11
12 static int doAdPersTest1(EOModel *m,EOAdaptorContext *ctx, EOAdaptorChannel *ch) {
13   NSAutoreleasePool *pool = [NSAutoreleasePool new];
14   EOEntity       *e;
15   EOSQLQualifier *q;
16   NSArray        *attrs;
17   id             record;
18
19   /* fetch some person records */
20           
21   e     = [m entityNamed:@"Person"];
22   q     = [e qualifier];
23   attrs = [e attributes];
24 #if 0
25   NSLog(@"entity: %@", e);
26   NSLog(@"qual:   %@", q);
27   NSLog(@"attrs:  %@", attrs);
28 #endif
29           
30   if (attrs == nil) {
31     NSLog(@"missing Person attributes in entity %@ !!!", e);
32   }
33
34   {
35     NSCalendarDate *start;
36     unsigned count, i;
37             
38     q = [[EOSQLQualifier alloc] initWithEntity:e
39                                 qualifierFormat:@"%A=5010", @"personId"];
40             
41     start = [NSCalendarDate date];
42
43     count = 1;
44     for (i = 0; i < count; i++) {
45       //NSCAssert(attrs, @"missing attributes !!!");
46       
47       if ([ch selectAttributes:attrs
48               describedByQualifier:q
49                fetchOrder:nil
50                lock:NO]) {            
51         while ((record = [ch fetchAttributes:attrs withZone:nil])) {
52 #if 1
53            NSLog(@"fetched %@ id %@",
54                  [record valueForKey:@"pname"],
55                  [record valueForKey:@"personId"]);
56 #endif
57         }
58       }
59       else {
60         NSLog(@"select failed ...");
61         break;
62       }
63     }
64     NSLog(@"duration: count %i %.3fs", count,
65           [[NSCalendarDate date] timeIntervalSinceDate:start]);
66   }
67   
68   RELEASE(pool);
69   return 1;
70 }
71
72 static int doAdTest1(EOModel *m,EOAdaptorContext *ctx, EOAdaptorChannel *ch) {
73 {
74 #if !LIB_FOUNDATION_BOEHM_GC
75   NSAutoreleasePool *pool = [NSAutoreleasePool new];
76 #endif
77   EOEntity       *e;
78   EOSQLQualifier *q;
79   NSArray        *attrs;
80
81   /* fetch some team records */
82
83   e = [m entityNamed:@"Team"];
84     q = [e qualifier];
85     attrs = [e attributes];
86     
87     if ([ch selectAttributes:attrs
88             describedByQualifier:q
89             fetchOrder:nil
90             lock:NO]) {
91         NSDictionary *record;
92     
93         while ((record = [ch fetchAttributes:attrs withZone:nil])) {
94         NSLog(@"fetched %@ birthday %@",
95                 [record valueForKey:@"description"],
96                 [record valueForKey:@"companyId"]);
97         }
98     }
99     
100     /* do some update */
101     
102     e = [m entityNamed:@"Person"];
103     attrs = [e attributes];
104     q = [[EOSQLQualifier alloc] initWithEntity:e
105                                 qualifierFormat:@"%A='helge'", @"login"];
106                                 
107     q = AUTORELEASE(q);
108     
109     if ([ch selectAttributes:attrs
110             describedByQualifier:q
111             fetchOrder:nil
112             lock:NO]) {
113         NSDictionary *record;
114     
115         while ((record = [ch fetchAttributes:attrs withZone:nil]))
116         ;
117     }
118     #if 0
119     /* fetch some expr */
120     
121     if (expr) {
122         if ([ch evaluateExpression:expr]) {
123         NSDictionary *record;
124     
125         attrs = [ch describeResults];
126         //NSLog(@"results: %@", attrs);
127     
128         while ((record = [ch fetchAttributes:attrs withZone:nil]))
129             NSLog(@"fetched %@", record);
130         }
131     }
132     #endif
133     
134     RELEASE(pool);
135     }
136   return 1;
137 }
138
139 static void runAdTests(NSDictionary *conDict) {
140   EOModel          *m;
141   EOAdaptor        *a;
142   EOAdaptorContext *ctx;
143   EOAdaptorChannel *ch;
144   NSString         *expr;
145
146   a = [EOAdaptor adaptorWithName:@"FrontBase2"];
147   //a = [[FrontBaseAdaptor alloc] initWithName:@"FrontBase"];
148   [a setConnectionDictionary:conDict];
149   NSLog(@"got adaptor[%@] %@", NSStringFromClass([a class]), a);
150   
151   ctx = [a   createAdaptorContext];
152   ch  = [ctx createAdaptorChannel];
153
154   m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]);
155   [a setModel:m];
156   [a setConnectionDictionary:conDict];
157   
158   expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"];
159
160   NSLog(@"opening channel ..");
161
162   //[ch setDebugEnabled:NO];
163   
164   if ([ch openChannel]) {
165     int txi;
166     NSLog(@"channel is open");
167
168     for (txi = 0; txi < txRepeatCount; txi++) {
169       if ([ctx beginTransaction]) {
170         NSLog(@"began tx (%i) ..", txi);
171
172         /* do something */
173         if (!doAdPersTest1(m, ctx, ch))
174             break;
175
176         NSLog(@"committing tx ..");
177         if ([ctx commitTransaction])
178           NSLog(@"  could commit.");
179         else
180           NSLog(@"  commit failed.");
181       }
182       else {
183         NSLog(@"tx open failed ..");
184       }
185     }
186     
187     NSLog(@"closing channel ..");
188     [ch closeChannel];
189   }
190   else {
191     NSLog(@"open channel failed ...");
192   }
193 }
194
195 static void runDbTests(NSDictionary *conDict) {
196   EOModel           *m;
197   EOAdaptor         *a;
198   EODatabase        *db;
199   EODatabaseContext *ctx;
200   EODatabaseChannel *ch;
201   NSString          *expr;
202
203   a = [EOAdaptor adaptorWithName:@"FrontBase2"];
204   //a = [[FrontBaseAdaptor alloc] initWithName:@"FrontBase"];
205   [a setConnectionDictionary:conDict];
206   NSLog(@"got adaptor[%@] %@", NSStringFromClass([a class]), a);
207   
208   db  = [[EODatabase alloc] initWithAdaptor:a];
209   ctx = [db  createContext];
210   ch  = [ctx createChannel];
211
212   m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]);
213   [a setModel:m];
214   [a setConnectionDictionary:conDict];
215   
216   expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"];
217
218   NSLog(@"opening channel ..");
219
220   if ([ch openChannel]) {
221     int txi;
222     NSLog(@"channel is open");
223
224     for (txi = 0; txi < txRepeatCount; txi++) {
225       if ([(EOAdaptorContext *)ctx beginTransaction]) {
226         NSLog(@"began tx ..");
227
228         /* do something */
229         {
230 #if !LIB_FOUNDATION_BOEHM_GC
231           NSAutoreleasePool *pool = [NSAutoreleasePool new];
232 #endif
233           EOEntity       *e;
234           EOSQLQualifier *q;
235           NSArray        *attrs;
236           id             record;
237
238           /* fetch some person records */
239           
240           e     = [m entityNamed:@"Person"];
241           q     = [e qualifier];
242           attrs = [e attributes];
243
244           if ([ch selectObjectsDescribedByQualifier:q fetchOrder:nil]) {
245             while ((record = [ch fetchWithZone:nil])) {
246               NSLog(@"fetched %@ birthday %@",
247                     [record valueForKey:@"description"],
248                     [record valueForKey:@"companyId"]);
249             }
250           }
251
252           /* fetch some team records */
253
254           e = [m entityNamed:@"Team"];
255           q = [e qualifier];
256           attrs = [e attributes];
257
258           /* do some update */
259           
260           e = [m entityNamed:@"Person"];
261           attrs = [e attributes];
262           q = [[EOSQLQualifier alloc] initWithEntity:e
263                                       qualifierFormat:@"%A='helge'", @"login"];
264                                     
265           q = AUTORELEASE(q);
266         
267           RELEASE(pool);
268         }
269
270         NSLog(@"committing tx ..");
271         if ([ctx commitTransaction])
272           NSLog(@"  could commit.");
273         else
274           NSLog(@"  commit failed.");
275       }
276     }
277     
278     NSLog(@"closing channel ..");
279     [ch closeChannel];
280   }
281   else {
282     NSLog(@"open channel failed ...");
283   }
284 }
285
286 int main(int argc, char **argv, char **env) {
287   NSAutoreleasePool *pool;
288   NSDictionary      *conDict;
289   
290   pool = [[NSAutoreleasePool alloc] init];
291   
292 #if LIB_FOUNDATION_LIBRARY
293   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
294 #endif
295
296   conDict = [NSDictionary dictionaryWithContentsOfFile:@"condict.plist"];
297
298   runAdTests(conDict);
299
300   RELEASE(pool);
301   return 0;
302 }