]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EODatabaseFault.m
more directory hierarchy reorganisations,
[sope] / sope-gdl1 / GDLAccess / EODatabaseFault.m
1 /* 
2    EODatabaseFault.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
7    Date: 1996
8
9    Author: Helge Hess <helge.hess@mdlink.de>
10    Date: 1999
11
12    This file is part of the GNUstep Database Library.
13
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.
18
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.
23
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.
28 */
29
30 #import "EODatabaseFault.h"
31 #import "EODatabase.h"
32 #import "EODatabaseChannel.h"
33 #import "EOEntity.h"
34 #import "EOFExceptions.h"
35 #import "EODatabaseFaultResolver.h"
36 #import "EOArrayProxy.h"
37 #import "common.h"
38
39 #if NeXT_RUNTIME || APPLE_RUNTIME
40 #  include <objc/objc-class.h>
41 #endif
42
43 typedef struct {
44     Class isa;
45 } *my_objc_object;
46
47 #define object_is_instance(object) \
48     ((object!=nil)&&CLS_ISCLASS(((my_objc_object)object)->isa))
49
50 /*
51  * EODatabaseFault class
52  */
53
54 @implementation EODatabaseFault
55
56 // Fault class methods
57
58 + (id)objectFaultWithPrimaryKey:(NSDictionary *)key
59   entity:(EOEntity *)entity 
60   databaseChannel:(EODatabaseChannel *)channel
61   zone:(NSZone *)zone
62 {
63     EODatabaseFault *fault = nil;
64     
65     fault = [channel allocateObjectForRow:key entity:entity zone:zone];
66     
67     if (fault == nil)
68         return nil;
69     
70     if ([fault class]->instance_size < ((Class)self)->instance_size) {
71         [fault autorelease];
72         [NSException raise:NSInvalidArgumentException
73                      format:
74                        @"Instances from class %@ must be at least %d in size "
75                        @"to fault",
76                        NSStringFromClass([fault class]), 
77                        ((Class)self)->instance_size];
78     }
79     fault->faultResolver = [[EOObjectFault alloc] initWithPrimaryKey:key
80         entity:entity databaseChannel:channel zone:zone 
81         targetClass:fault->isa];
82     fault->isa = self;
83     
84     return (EODatabaseFault *)AUTORELEASE(fault);
85 }
86
87 + (NSArray*)arrayFaultWithQualifier:(EOSQLQualifier*)qualifier 
88   fetchOrder:(NSArray*)fetchOrder 
89   databaseChannel:(EODatabaseChannel*)channel
90   zone:(NSZone*)zone
91 {
92   return [EOArrayProxy arrayProxyWithQualifier:qualifier
93                        fetchOrder:fetchOrder
94                        channel:channel];
95 #if 0
96     EODatabaseFault* fault;
97     
98     fault = [NSMutableArray allocWithZone:zone];
99
100     if ([fault class]->instance_size < ((Class)(self))->instance_size) {
101         (void)AUTORELEASE(fault);
102         THROW([[InvalidArgumentException alloc]
103                 initWithFormat:
104                     @"Instances from class %s must be at least %d "
105                     @"in size to fault",
106                     NSStringFromClass([fault class]),
107                     ((Class)self)->instance_size]);
108     }
109     fault->faultResolver = [[EOArrayFault alloc] initWithQualifier:qualifier
110         fetchOrder:fetchOrder databaseChannel:channel zone:zone 
111         targetClass:fault->isa fault:fault];
112     fault->isa = self;
113
114     return (NSArray*)AUTORELEASE(fault);
115 #endif
116 }
117
118 // no more garbage collecting
119 + (NSArray *)gcArrayFaultWithQualifier:(EOSQLQualifier *)qualifier 
120   fetchOrder:(NSArray *)fetchOrder 
121   databaseChannel:(EODatabaseChannel *)channel
122   zone:(NSZone *)zone
123 {
124   EODatabaseFault *fault;
125     
126   fault = [NSMutableArray allocWithZone:zone];
127
128   if ([fault class]->instance_size < ((Class)(self))->instance_size) {
129         (void)[fault autorelease];
130         [NSException raise:NSInvalidArgumentException
131                      format:
132                     @"Instances from class %s must be at least %d "
133                     @"in size to fault",
134                     NSStringFromClass([fault class]),
135                     ((Class)self)->instance_size];
136   }
137   fault->faultResolver = [[EOArrayFault alloc] initWithQualifier:qualifier
138         fetchOrder:fetchOrder databaseChannel:channel zone:zone 
139         targetClass:fault->isa];
140   fault->isa = self;
141
142   return (NSArray *)AUTORELEASE(fault);
143 }
144
145 + (NSDictionary *)primaryKeyForFault:(id)fault {
146   EODatabaseFault *aFault = (EODatabaseFault *)fault;
147
148   // Check that argument is fault
149   if (aFault->isa != self)
150     return nil;
151     
152   return [(EODatabaseFaultResolver *)aFault->faultResolver primaryKey];
153 }
154
155 + (EOEntity *)entityForFault:(id)fault {
156   EODatabaseFault *aFault = (EODatabaseFault *)fault;
157
158   // Check that argument is fault
159   if (aFault->isa != self)
160     return nil;
161
162   return [(EODatabaseFaultResolver *)aFault->faultResolver entity];
163 }
164
165 + (EOSQLQualifier *)qualifierForFault:(id)fault {
166   EODatabaseFault *aFault = (EODatabaseFault *)fault;
167
168   // Check that argument is fault
169   if (aFault->isa != self)
170     return nil;
171     
172   return [(EODatabaseFaultResolver *)aFault->faultResolver qualifier];
173 }
174
175 + (NSArray *)fetchOrderForFault:(id)fault {
176   EODatabaseFault *aFault = (EODatabaseFault *)fault;
177
178   // Check that argument is fault
179   if (aFault->isa != self)
180     return nil;
181     
182   return [(EODatabaseFaultResolver *)aFault->faultResolver fetchOrder];
183 }
184
185 + (EODatabaseChannel *)databaseChannelForFault:fault {
186   EODatabaseFault *aFault = (EODatabaseFault *)fault;
187
188   // Check that argument is fault
189   if (aFault->isa != self)
190     return nil;
191     
192   return [(EODatabaseFaultResolver *)aFault->faultResolver databaseChannel];
193 }
194
195 - (void)dealloc {
196   [EODatabase forgetObject:self];
197   [super dealloc];
198 }
199
200 // Forwarding stuff
201
202 + (void)initialize {
203   // Must be here as initialize is called for each root class
204   // without asking if it responds to it !
205 }
206
207 - (EOEntity *)entity {
208   return [EODatabaseFault entityForFault:self];
209 }
210
211 @end /* EODatabaseFault */
212
213 /*
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. 
217  */
218
219 @implementation NSObject(EOUnableToFaultToOne)
220
221 - (void)unableToFaultWithPrimaryKey:(NSDictionary*)key 
222   entity:(EOEntity*)entity 
223   databaseChannel:(EODatabaseChannel*)channel
224 {
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];
229 }
230
231 @end /* NSObject(EOUnableToFaultToOne) */
232
233 @implementation EOFault(EOUnableToFaultToOne)
234
235 - (void)unableToFaultWithPrimaryKey:(NSDictionary*)key 
236   entity:(EOEntity *)entity 
237   databaseChannel:(EODatabaseChannel*)channel
238 {
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]] 
243      raise];
244 }
245
246 @end /* EOFault(EOUnableToFaultToOne) */