]> err.no Git - sope/blob - sope-gdl1/SQLite3/gdltest.m
fixed a error log
[sope] / sope-gdl1 / SQLite3 / gdltest.m
1 /* 
2    gdltest.m
3
4    Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess
5
6    Author: Helge Hess (helge@mdlink.de)
7
8    This file is part of the SQLite Adaptor Library
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Library General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Library General Public License for more details.
19
20    You should have received a copy of the GNU Library General Public
21    License along with this library; see the file COPYING.LIB.
22    If not, write to the Free Software Foundation,
23    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #import <Foundation/Foundation.h>
27 #import <GDLAccess/GDLAccess.h>
28 #include <NGExtensions/NGExtensions.h>
29
30 static void fetchExprInChannel(NSString *expr, EOAdaptorChannel *ch) {
31   NSArray      *attrs;
32   NSDictionary *record;
33   
34   if (![ch evaluateExpression:expr]) {
35     NSLog(@"ERROR: failed to evaluate: %@", expr);
36     return;
37   }
38
39   attrs = [ch describeResults];
40   NSLog(@"results: %@", attrs);
41     
42   while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil)
43     NSLog(@"fetched %@", record);
44 }
45
46 static void fetchSomePersonRecord(EOEntity *e, EOAdaptorChannel *ch) {
47   EOSQLQualifier *q;
48   NSArray *attrs;
49
50     attrs = [e attributes];
51     q = [[EOSQLQualifier alloc]
52           initWithEntity:e
53           qualifierFormat:@"%A='helge'", @"login"];
54     [q autorelease];
55
56     if ([ch selectAttributes:attrs
57             describedByQualifier:q
58             fetchOrder:nil
59             lock:NO]) {
60       NSDictionary *record;
61
62       record = [ch fetchAttributes:attrs withZone:nil];
63     }
64     else
65       NSLog(@"Could not select ..");
66 }
67
68 static void fetchSomeTeamRecords(EOEntity *e, EOAdaptorChannel *ch) {
69   EOSQLQualifier *q;
70   NSArray *attrs;
71
72   q     = [e qualifier];
73   attrs = [e attributes];
74
75   if ([ch selectAttributes:attrs describedByQualifier:q fetchOrder:nil
76           lock:NO]) {
77     NSDictionary *record;
78       
79     while ((record = [ch fetchAttributes:attrs withZone:NULL]) != nil) {
80         NSLog(@"fetched %@ birthday %@",
81               [record valueForKey:@"description"],
82               [record valueForKey:@"companyId"]);
83     }
84   }
85   else
86     NSLog(@"Could not select team records ..");
87 }
88
89 static void runtestInOpenChannel(EOAdaptorChannel *ch) {
90   NSAutoreleasePool *pool = [NSAutoreleasePool new];
91   EOEntity *e;
92   EOSQLQualifier *q;
93   NSArray *attrs;
94   EOAdaptorContext *ctx;
95   EOModel  *m;
96   NSString *expr;
97
98   ctx = [ch adaptorContext];
99   m   = [[ctx adaptor] model];
100
101   expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"];
102
103   NSLog(@"channel is open");
104   
105   if (![ctx beginTransaction]) {
106     NSLog(@"ERROR: could not begin transaction ...");
107     return;
108   }
109
110   NSLog(@"began tx ..");
111
112   /* do something */
113   pool = [[NSAutoreleasePool alloc] init];
114 #if 1
115   if (expr) fetchExprInChannel(expr, ch);
116 #endif
117   
118   /* fetch some MyEntity records */
119   
120   e = [m entityNamed:@"MyEntity"];
121   NSLog(@"entity: %@", e);
122   if (e == nil)
123     exit(1);
124         
125   q     = [e qualifier];
126   attrs = [e attributes];
127   
128   // NSLog(@"ATTRS: %@", attrs);
129   
130   if ([ch selectAttributes:attrs
131           describedByQualifier:q
132           fetchOrder:nil
133           lock:NO]) {
134     NSDictionary *record;
135
136     while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil)
137       NSLog(@"fetched record: %@", record);
138   }
139   else
140     NSLog(@"Could not select ..");
141
142   /* some OGo fetches */
143   
144   if ((e = [m entityNamed:@"Team"]) != nil)
145     fetchSomeTeamRecords(e, ch);
146   
147   if ((e = [m entityNamed:@"Person"]) != nil)
148     fetchSomePersonRecord(e, ch);
149
150   /* tear down */
151   
152   [pool release];
153       
154   NSLog(@"committing tx ..");
155   if ([ctx commitTransaction])
156     NSLog(@"  could commit.");
157   else
158     NSLog(@"  commit failed.");
159 }
160
161 static void runtest(void) {
162   EOModel          *m = nil;
163   EOAdaptor        *a;
164   EOAdaptorContext *ctx;
165   EOAdaptorChannel *ch;
166   NSDictionary     *conDict;
167   
168   NS_DURING {
169   
170   conDict = [NSDictionary dictionaryWithContentsOfFile:@"condict.plist"];
171   NSLog(@"condict is %@", conDict);
172   
173   if ((a = [EOAdaptor adaptorWithName:@"SQLite3"]) == nil) {
174     NSLog(@"found no SQLite3 adaptor ..");
175     exit(1);
176   }
177   
178   NSLog(@"got adaptor %@", a);
179   [a setConnectionDictionary:conDict];
180   NSLog(@"got adaptor with condict %@", a);
181   
182   ctx = [a   createAdaptorContext];
183   ch  = [ctx createAdaptorChannel];
184
185 #if 1
186   m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]);
187   if (m) {
188     [a setModel:m];
189     [a setConnectionDictionary:conDict];
190   }
191 #endif
192   
193   NSLog(@"opening channel ..");
194
195   [ch setDebugEnabled:YES];
196   
197   if ([ch openChannel]) {
198     runtestInOpenChannel(ch);
199     
200     NSLog(@"closing channel ..");
201     [ch closeChannel];
202   }
203   }
204   NS_HANDLER {
205     fprintf(stderr, "exception: %s\n", [[localException description] cString]);
206     abort();
207   }
208   NS_ENDHANDLER;
209 }
210
211 int main(int argc, char **argv, char **env) {
212   NSAutoreleasePool *pool;
213
214   pool = [[NSAutoreleasePool alloc] init];
215   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
216
217   runtest();
218   [pool release];
219   return 0;
220 }