File: CompiledAdaptorTest.m

package info (click to toggle)
gstep-core 0.5.0.981229-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 22,284 kB
  • ctags: 4,330
  • sloc: objc: 127,559; ansic: 26,207; sh: 8,362; exp: 3,588; makefile: 674; lisp: 654; lex: 437; perl: 406; cpp: 145; yacc: 128; csh: 66
file content (391 lines) | stat: -rw-r--r-- 11,316 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* 
   CompiledAdaptorTest.m

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
   Date: 1996

   This file is part of the GNUstep Database Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSUtilities.h>
#include <Foundation/NSObjCRuntime.h>

#include <extensions/support.h>

#include <eoaccess/EOModel.h>
#include <eoaccess/EOEntity.h>
#include <eoaccess/EOAttribute.h>
#include <eoaccess/EORelationship.h>

#include <eoaccess/EOQualifier.h>
#include <eoaccess/EONull.h>

#include <eoaccess/EOAdaptor.h>
#include <eoaccess/EOAdaptorContext.h>
#include <eoaccess/EOAdaptorChannel.h>

#include "CompiledTest.h"

/*
 * Adaptor level operations
 */

@implementation CompiledTest(Adaptor)

- (void)initAdaptorObjects
{
    adaptor = [EOAdaptor adaptorWithModel:model];
    adaptorContext = [adaptor createAdaptorContext];
    adaptorChannel = [[adaptorContext createAdaptorChannel] retain];
    
    [self check:[adaptorChannel openChannel]
	format:@"could not open adaptor channel and connect to SQL server"];
}

- (void)doneAdaptorObjects
{
    [adaptorChannel closeChannel];
    [adaptorChannel release];
    adaptor = nil; adaptorContext = nil; adaptorChannel = nil;
}

- (void)enableAdaptorVerbose
{
    [adaptorChannel setDebugEnabled:YES];    
}

- (void)disableAdaptorVerbose
{
    [adaptorChannel setDebugEnabled:NO];
}

/*
 * Adaptor test helper methods
 */

- (void)beginAdaptorTransaction
{
    [self check:[adaptorContext beginTransaction]
	format:@"cannot begin transaction"];
}

- (void)commitAdaptorTransaction
{
    [self check:[adaptorContext commitTransaction]
	format:@"cannot commit transaction"];
}

/*
 *  Run adaptor test
 */

/*
	entity = entityName			- entity to modify
	keyAttributes = (attrName, ...)		- attributes for primary key
 */

- (void)adaptorModifyEntity
{
    id obj;
    EOEntity* entity;
    
    obj = [testInfo objectForKey:@"entity"];
    [self check:(obj != nil)
	format:@"adaptorModifyEntity `entity' key undefined"];
    entity = [model entityNamed:obj];
    [self check:(entity != nil)
	format:@"adaptorModifyEntity `entity' key `%@' is unknown", obj];
    
    obj = [testInfo objectForKey:@"keyAttributes"];
    if (obj) {
	[entity setPrimaryKeyAttributes:
	    [self attributesForEntity:entity names:obj]];
	[self logMessage:@"set primary key for entity"];
    }
}

/*
	clearEntities = (entityName, ...)
 */

- (void)adaptorClearEntities
{
    id obj;
    EOEntity* entity;
    NSArray* list;
    NSEnumerator* en;
    NSString* sql;
    
    list = [testInfo objectForKey:@"clearEntities"];
    [self check:(list != nil)
	format:@"adaptorClearEntities `clearEntities' key undefined"];
    
    [self beginAdaptorTransaction];
    en = [list objectEnumerator];
    while ((obj = [en nextObject])) {
	entity = [model entityNamed:obj];
	[self check:(entity != nil)
	    format:@"adaptorClearEntities: entity `%@' is unknown", obj];
    	sql = [NSString stringWithFormat:@"DELETE FROM %@", 
	    [entity externalName]];
	[self check:[adaptorChannel evaluateExpression:sql]
	    format:@"cannot execute sql `%@'", sql];
    }
    [self commitAdaptorTransaction];
    [self logMessage:@"entities cleared"];
}

/*
	entity = entityName			- entity to insert in
	insertAttributes = (attrName, ...)	- attributes to insert
	insertValues = (...)			- list of rows to insert
	insertShouldFail = YES			- if insertion should fail
 */

- (void)adaptorInsertEntity
{
    NSArray* atr_list;
    NSArray* row_list;
    EOEntity* entity;
    id obj;
    int i,n;
    BOOL shouldFail;
    
    // Get entity
    obj = [testInfo objectForKey:@"entity"];
    [self check:(obj != nil)
	format:@"adaptorInsertEntity `entity' key undefined"];
    entity = [model entityNamed:obj];
    [self check:(entity != nil)
	format:@"adaptorInsertEntity `entity' key `%@' is unknown", obj];

    // Get attribute list
    atr_list = [testInfo objectForKey:@"insertAttributes"];
    [self check:(obj != nil)
	format:@"adaptorInsertEntity `insertAttributes' key undefined"];
    atr_list = [self attributesForEntity:entity names:atr_list];
    
    // Get values list
    obj = [testInfo objectForKey:@"insertValues"];
    [self check:(obj != nil)
	format:@"adaptorInsertEntity `insertValues' key undefined"];
    
    // Make row list
    row_list = [self rowsForEntity:entity
	attributes:atr_list values:obj];
    
    // Should fail while inserting
    obj = [testInfo objectForKey:@"insertShouldFail"];
    shouldFail = (obj && [obj isEqual:@"YES"]);

    [self beginAdaptorTransaction];
    for (i = 0, n = [row_list count]; i < n; i++) {
	NSDictionary* row = [row_list objectAtIndex:i];
	if ([adaptorChannel insertRow:row forEntity:entity])
	    [self check:(!shouldFail) format:@"insert should have failed"];
	else
	    [self check:(shouldFail) format:@"insert failed"];
    }
    [self commitAdaptorTransaction];
    [self logMessage:@"rows inserted"];
}

/*
	entity = entityName			- entity to select
	selectAttributes = (attrName, ...)	- attributes to select
	selectValues = (...)			- list of rows to come
	selectQualifier = qualifier		- extra qualifier for entity
	selectOrder = ((atr, A/D), ...)		- select order
	selectLock = YES			- select with locking
 */

- (void)adaptorSelectEntity
{
    EOQualifier* qualifier;
    NSString* qualifierString;
    NSArray* atr_list;
    NSArray* row_list;
    NSMutableArray* fetched_list;
    NSArray* order_list;
    EOEntity* entity;
    id obj;
    NSDictionary* row;
    id pool;
    BOOL lock;
    int i, n;
    
    pool = [[NSAutoreleasePool alloc] init];
    
    // Get entity
    obj = [testInfo objectForKey:@"entity"];
    [self check:(obj != nil)
	format:@"adaptorSelectEntity `entity' key undefined"];
    entity = [model entityNamed:obj];
    [self check:(entity != nil)
	format:@"adaptorSelectEntity `entity' key `%@' is unknown", obj];

    // Get attribute list
    obj = [testInfo objectForKey:@"selectAttributes"];
    [self check:(obj != nil)
	format:@"adaptorSelectEntity `selectAttributes' key undefined"];
    atr_list = [self attributesForEntity:entity names:obj];
    
    // Get values list
    obj = [testInfo objectForKey:@"selectValues"];
    [self check:(obj != nil)
	format:@"adaptorSelectEntity `selectValues' key undefined"];
    row_list = [self rowsForEntity:entity
	attributes:atr_list values:obj];

    // Get locking
    obj = [testInfo objectForKey:@"selectLock"];
    lock = (obj && [obj isEqual:@"YES"]);
   
    // Get fetch order
    order_list = nil;
    obj = [testInfo objectForKey:@"selectOrder"];
    if (obj)
	order_list = [self sortOrderingForEntity:entity order:obj];

    // Get qualifier
    qualifierString = [testInfo objectForKey:@"selectQualifier"];
    qualifier = [[[EOQualifier alloc] initWithEntity:entity
	qualifierFormat:qualifierString] autorelease];
    fetched_list = [[[NSMutableArray alloc] init] autorelease];
    
    // Perform select and fetch
    [self beginAdaptorTransaction];
    [self check:[adaptorChannel selectAttributes:atr_list 
	    describedByQualifier:qualifier
	    fetchOrder:order_list
	    lock:lock]
	format:@"cannot select entity with qualifier `%@'", qualifierString];
    while ((row = [adaptorChannel fetchAttributes:atr_list withZone:NULL]))
	[fetched_list addObject:row];
    [adaptorChannel cancelFetch];
    [self commitAdaptorTransaction];
    
    // Compare rows
    n = [row_list count];
    i = [fetched_list count];
    
    [self check:(n == i)
	format:@"select got %d rows while expecting %d", i, n];
    
    for (i = 0; i < n; i++) {
	id must, got;
	
	got = [fetched_list objectAtIndex:i];
	must = [row_list objectAtIndex:i];
	
	[self check:[got isEqual:must]
	    format:@"row `%@' differs from expected `%@'", got, must];
    }
    
    [pool release];
    [self logMessage:@"rows selected"];
}

/*
	entity = entityName			- entity to update in
	updateAttributes = (attrName, ...)	- attributes to update
	updateValues = (...)			- list of rows to update
	updateKeys   = (...)			- attributes for key
	updateKeyValues = (...)			- list of key values
 */

- (void)adaptorUpdateEntity
{
    EOEntity* entity;
    NSArray* atr_list;
    NSArray* row_list;
    NSArray* key_atr;
    NSArray* key_val;
    id obj;
    id pool;
    int i, n;
    
    pool = [[NSAutoreleasePool alloc] init];
    
    // Get entity
    obj = [testInfo objectForKey:@"entity"];
    [self check:(obj != nil)
	format:@"adaptorUpdateEntity `entity' key undefined"];
    entity = [model entityNamed:obj];
    [self check:(entity != nil)
	format:@"adaptorUpdateEntity `entity' key `%@' is unknown", obj];

    // Get attribute list
    obj = [testInfo objectForKey:@"updateAttributes"];
    [self check:(obj != nil)
	format:@"adaptorUpdateEntity `updateAttributes' key undefined"];
    atr_list = [self attributesForEntity:entity names:obj];
    
    // Get values list
    obj = [testInfo objectForKey:@"updateValues"];
    [self check:(obj != nil)
	format:@"adaptorUpdateEntity `updateValues' key undefined"];
    row_list = [self rowsForEntity:entity
	attributes:atr_list values:obj];

    // Get key attributes list
    obj = [testInfo objectForKey:@"updateKeys"];
    [self check:(obj != nil)
	format:@"adaptorUpdateEntity `updateKeys' key undefined"];
    key_atr = [self attributesForEntity:entity names:obj];
    
    // Get key values list
    obj = [testInfo objectForKey:@"updateKeysValues"];
    [self check:(obj != nil)
	format:@"adaptorUpdateEntity `updateKeysValues' key undefined"];
    key_val = [self rowsForEntity:entity
	attributes:key_atr values:obj];

    [self check:[row_list count] == [key_val count]
	format:@"adaptorUpdateEntity must have matching counts in val & key"];
    
    [self beginAdaptorTransaction];
    for (i = 0, n = [row_list count]; i < n; i++) {
	NSDictionary* row = [row_list objectAtIndex:i];
	NSDictionary* key = [key_val objectAtIndex:i];
	EOQualifier* qualifier = [EOQualifier
		    qualifierForPrimaryKey:key entity:entity];
	
	[self check:(qualifier != nil)
	    format:@"adaptorUpdateEntity: nil qualifier to update row `%@'", 
		row];
	[self check:
	    [adaptorChannel updateRow:row describedByQualifier:qualifier]
	    format:@"cannot update row `%@' with qualifier `%@'",
		row, [qualifier expressionValueForContext:nil]];
    }
    [self commitAdaptorTransaction];
    
    [pool release];
    [self logMessage:@"rows updated"];
}

@end

void __compiled_adaptor_linking(void) {}