4 Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess
6 Author: Helge Hess (helge@mdlink.de)
8 This file is part of the MySQL4 Adaptor Library
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.
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.
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.
26 #import <Foundation/Foundation.h>
27 #import <GDLAccess/GDLAccess.h>
28 #include <NGExtensions/NGExtensions.h>
30 static void fetchExprInChannel(NSString *expr, EOAdaptorChannel *ch) {
34 if (![ch evaluateExpression:expr]) {
35 NSLog(@"ERROR: failed to evaluate: %@", expr);
39 attrs = [ch describeResults];
40 NSLog(@"results: %@", attrs);
42 while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil)
43 NSLog(@"fetched %@", record);
46 static void fetchSomePersonRecord(EOEntity *e, EOAdaptorChannel *ch) {
50 attrs = [e attributes];
51 q = [[EOSQLQualifier alloc]
53 qualifierFormat:@"%A='helge'", @"login"];
56 if ([ch selectAttributes:attrs
57 describedByQualifier:q
62 record = [ch fetchAttributes:attrs withZone:nil];
65 NSLog(@"Could not select ..");
68 static void fetchSomeTeamRecords(EOEntity *e, EOAdaptorChannel *ch) {
73 attrs = [e attributes];
75 if ([ch selectAttributes:attrs describedByQualifier:q fetchOrder:nil
79 while ((record = [ch fetchAttributes:attrs withZone:NULL]) != nil) {
80 NSLog(@"fetched %@ birthday %@",
81 [record valueForKey:@"description"],
82 [record valueForKey:@"companyId"]);
86 NSLog(@"Could not select team records ..");
89 static void runtestInOpenChannel(EOAdaptorChannel *ch) {
90 NSAutoreleasePool *pool = [NSAutoreleasePool new];
94 EOAdaptorContext *ctx;
98 ctx = [ch adaptorContext];
99 m = [[ctx adaptor] model];
101 expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"];
103 NSLog(@"channel is open");
105 if (![ctx beginTransaction]) {
106 NSLog(@"ERROR: could not begin transaction ...");
110 NSLog(@"began tx ..");
113 pool = [[NSAutoreleasePool alloc] init];
115 if (expr) fetchExprInChannel(expr, ch);
118 /* fetch some MyEntity records */
120 e = [m entityNamed:@"MyEntity"];
121 NSLog(@"entity: %@", e);
126 attrs = [e attributes];
128 // NSLog(@"ATTRS: %@", attrs);
130 if ([ch selectAttributes:attrs
131 describedByQualifier:q
134 NSDictionary *record;
136 while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil)
137 NSLog(@"fetched record: %@", record);
140 NSLog(@"Could not select ..");
142 /* some OGo fetches */
144 if ((e = [m entityNamed:@"Team"]) != nil)
145 fetchSomeTeamRecords(e, ch);
147 if ((e = [m entityNamed:@"Person"]) != nil)
148 fetchSomePersonRecord(e, ch);
154 NSLog(@"committing tx ..");
155 if ([ctx commitTransaction])
156 NSLog(@" could commit.");
158 NSLog(@" commit failed.");
161 static void runtest(void) {
164 EOAdaptorContext *ctx;
165 EOAdaptorChannel *ch;
166 NSDictionary *conDict;
170 conDict = [NSDictionary dictionaryWithContentsOfFile:@"condict.plist"];
171 NSLog(@"condict is %@", conDict);
173 if ((a = [EOAdaptor adaptorWithName:@"MySQL4"]) == nil) {
174 NSLog(@"found no MySQL4 adaptor ..");
178 NSLog(@"got adaptor %@", a);
179 [a setConnectionDictionary:conDict];
180 NSLog(@"got adaptor with condict %@", a);
182 ctx = [a createAdaptorContext];
183 ch = [ctx createAdaptorChannel];
186 m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]);
189 [a setConnectionDictionary:conDict];
193 NSLog(@"opening channel ..");
195 [ch setDebugEnabled:YES];
197 if ([ch openChannel]) {
198 runtestInOpenChannel(ch);
200 NSLog(@"closing channel ..");
205 fprintf(stderr, "exception: %s\n", [[localException description] cString]);
211 int main(int argc, char **argv, char **env) {
212 NSAutoreleasePool *pool;
214 pool = [[NSAutoreleasePool alloc] init];
215 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];