4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
9 Author: Helge Hess <helge.hess@mdlink.de>
12 This file is part of the GNUstep Database Library.
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Library General Public
16 License as published by the Free Software Foundation; either
17 version 2 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Library General Public License for more details.
24 You should have received a copy of the GNU Library General Public
25 License along with this library; see the file COPYING.LIB.
26 If not, write to the Free Software Foundation,
27 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #import "EODatabaseFault.h"
31 #import "EODatabase.h"
32 #import "EODatabaseChannel.h"
34 #import "EOFExceptions.h"
35 #import "EODatabaseFaultResolver.h"
36 #import "EOArrayProxy.h"
39 #if NeXT_RUNTIME || APPLE_RUNTIME
40 # include <objc/objc-class.h>
47 #define object_is_instance(object) \
48 ((object!=nil)&&CLS_ISCLASS(((my_objc_object)object)->isa))
51 * EODatabaseFault class
54 @implementation EODatabaseFault
56 // Fault class methods
58 + (id)objectFaultWithPrimaryKey:(NSDictionary *)key
59 entity:(EOEntity *)entity
60 databaseChannel:(EODatabaseChannel *)channel
63 EODatabaseFault *fault = nil;
65 fault = [channel allocateObjectForRow:key entity:entity zone:zone];
70 if ([fault class]->instance_size < ((Class)self)->instance_size) {
72 [NSException raise:NSInvalidArgumentException
74 @"Instances from class %@ must be at least %d in size "
76 NSStringFromClass([fault class]),
77 ((Class)self)->instance_size];
79 fault->faultResolver = [[EOObjectFault alloc] initWithPrimaryKey:key
80 entity:entity databaseChannel:channel zone:zone
81 targetClass:fault->isa];
84 return (EODatabaseFault *)AUTORELEASE(fault);
87 + (NSArray*)arrayFaultWithQualifier:(EOSQLQualifier*)qualifier
88 fetchOrder:(NSArray*)fetchOrder
89 databaseChannel:(EODatabaseChannel*)channel
92 return [EOArrayProxy arrayProxyWithQualifier:qualifier
96 EODatabaseFault* fault;
98 fault = [NSMutableArray allocWithZone:zone];
100 if ([fault class]->instance_size < ((Class)(self))->instance_size) {
101 (void)AUTORELEASE(fault);
102 THROW([[InvalidArgumentException alloc]
104 @"Instances from class %s must be at least %d "
106 NSStringFromClass([fault class]),
107 ((Class)self)->instance_size]);
109 fault->faultResolver = [[EOArrayFault alloc] initWithQualifier:qualifier
110 fetchOrder:fetchOrder databaseChannel:channel zone:zone
111 targetClass:fault->isa fault:fault];
114 return (NSArray*)AUTORELEASE(fault);
118 // no more garbage collecting
119 + (NSArray *)gcArrayFaultWithQualifier:(EOSQLQualifier *)qualifier
120 fetchOrder:(NSArray *)fetchOrder
121 databaseChannel:(EODatabaseChannel *)channel
124 EODatabaseFault *fault;
126 fault = [NSMutableArray allocWithZone:zone];
128 if ([fault class]->instance_size < ((Class)(self))->instance_size) {
129 (void)[fault autorelease];
130 [NSException raise:NSInvalidArgumentException
132 @"Instances from class %s must be at least %d "
134 NSStringFromClass([fault class]),
135 ((Class)self)->instance_size];
137 fault->faultResolver = [[EOArrayFault alloc] initWithQualifier:qualifier
138 fetchOrder:fetchOrder databaseChannel:channel zone:zone
139 targetClass:fault->isa];
142 return (NSArray *)AUTORELEASE(fault);
145 + (NSDictionary *)primaryKeyForFault:(id)fault {
146 EODatabaseFault *aFault = (EODatabaseFault *)fault;
148 // Check that argument is fault
149 if (aFault->isa != self)
152 return [(EODatabaseFaultResolver *)aFault->faultResolver primaryKey];
155 + (EOEntity *)entityForFault:(id)fault {
156 EODatabaseFault *aFault = (EODatabaseFault *)fault;
158 // Check that argument is fault
159 if (aFault->isa != self)
162 return [(EODatabaseFaultResolver *)aFault->faultResolver entity];
165 + (EOSQLQualifier *)qualifierForFault:(id)fault {
166 EODatabaseFault *aFault = (EODatabaseFault *)fault;
168 // Check that argument is fault
169 if (aFault->isa != self)
172 return [(EODatabaseFaultResolver *)aFault->faultResolver qualifier];
175 + (NSArray *)fetchOrderForFault:(id)fault {
176 EODatabaseFault *aFault = (EODatabaseFault *)fault;
178 // Check that argument is fault
179 if (aFault->isa != self)
182 return [(EODatabaseFaultResolver *)aFault->faultResolver fetchOrder];
185 + (EODatabaseChannel *)databaseChannelForFault:fault {
186 EODatabaseFault *aFault = (EODatabaseFault *)fault;
188 // Check that argument is fault
189 if (aFault->isa != self)
192 return [(EODatabaseFaultResolver *)aFault->faultResolver databaseChannel];
196 [EODatabase forgetObject:self];
203 // Must be here as initialize is called for each root class
204 // without asking if it responds to it !
207 - (EOEntity *)entity {
208 return [EODatabaseFault entityForFault:self];
211 @end /* EODatabaseFault */
214 * Informal protocol that informs an instance that a to-one
215 * relationship could not be resoved to get data for self.
216 * Its implementation in NSObject raises NSObjectNotAvailableException.
219 @implementation NSObject(EOUnableToFaultToOne)
221 - (void)unableToFaultWithPrimaryKey:(NSDictionary*)key
222 entity:(EOEntity*)entity
223 databaseChannel:(EODatabaseChannel*)channel
225 // TODO - throw exception form derived class
226 [[[ObjectNotAvailableException alloc]
227 initWithFormat:@"cannot fault to-one for primary key %@ entity %@",
228 [key description], [entity name]] raise];
231 @end /* NSObject(EOUnableToFaultToOne) */
233 @implementation EOFault(EOUnableToFaultToOne)
235 - (void)unableToFaultWithPrimaryKey:(NSDictionary*)key
236 entity:(EOEntity *)entity
237 databaseChannel:(EODatabaseChannel*)channel
239 // TODO - throw exception from derived class
240 [[[ObjectNotAvailableException alloc]
241 initWithFormat:@"cannot fault to-one for primary key %@ entity %@",
242 [key description], [entity name]]
246 @end /* EOFault(EOUnableToFaultToOne) */