]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOAdaptorContext.m
added missing inline pathes
[sope] / sope-gdl1 / GDLAccess / EOAdaptorContext.m
1 /* 
2    EOAdaptorContext.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7    Date: October 1996
8
9    This file is part of the GNUstep Database Library.
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #import <Foundation/NSValue.h>
28 #import <Foundation/NSArray.h>
29
30 #import "common.h"
31 #import "EOAdaptor.h"
32 #import "EOAdaptorContext.h"
33 #import "EOAdaptorChannel.h"
34
35 @implementation EOAdaptorContext
36
37 - (id)initWithAdaptor:(EOAdaptor *)_adaptor {
38   ASSIGN(self->adaptor, _adaptor);
39   self->channels = [[NSMutableArray alloc] initWithCapacity:2];
40   [self->adaptor contextDidInit:self];
41   return self;
42 }
43
44 - (void)dealloc {
45   [self->adaptor contextWillDealloc:self];
46   [self->adaptor  release];
47   [self->channels release];
48   [super dealloc];
49 }
50
51 /* channels */
52
53 - (EOAdaptorChannel *)createAdaptorChannel {
54   return [[[[adaptor adaptorChannelClass] alloc]
55             initWithAdaptorContext:self] autorelease];
56 }
57 - (NSArray *)channels {
58   NSMutableArray *ma;
59   unsigned i, count;
60   
61   if ((count = [self->channels count]) == 0)
62     return nil;
63
64   ma = [NSMutableArray arrayWithCapacity:count];
65   for (i = 0; i < count; i++)
66     [ma addObject:[[self->channels objectAtIndex:i] nonretainedObjectValue]];
67   
68   return ma;
69 }
70
71 - (void)channelDidInit:aChannel {
72   [self->channels addObject:[NSValue valueWithNonretainedObject:aChannel]];
73 }
74
75 - (void)channelWillDealloc:(id)aChannel {
76     int i;
77     
78     for (i = [self->channels count] - 1; i >= 0; i--)
79         if ([[channels objectAtIndex:i] nonretainedObjectValue] == aChannel) {
80             [channels removeObjectAtIndex:i];
81             break;
82         }
83 }
84
85 - (BOOL)hasOpenChannels {
86     int i, count = [channels count];
87
88     for (i = 0; i < count; i++)
89         if ([[[channels objectAtIndex:i] nonretainedObjectValue] isOpen])
90             return YES;
91
92     return NO;
93 }
94
95 - (BOOL)hasBusyChannels {
96     int i, count = [channels count];
97
98     for (i = 0; i < count; i++)
99         if ([[[channels objectAtIndex:i] nonretainedObjectValue]
100                 isFetchInProgress])
101             return YES;
102
103     return NO;
104 }
105
106 /* transactions */
107
108 - (BOOL)beginTransaction {
109     if (transactionNestingLevel && ![self canNestTransactions])
110         return NO;
111
112     if ([self->channels count] == 0)
113         return NO;
114
115     if (delegateRespondsTo.willBegin) {
116         EODelegateResponse response = [delegate adaptorContextWillBegin:self];
117         if (response == EODelegateRejects)
118             return NO;
119         else if (response == EODelegateOverrides)
120             return YES;
121     }
122     if ([self primaryBeginTransaction] == NO)
123         return NO;
124
125     [self transactionDidBegin];
126
127     if (delegateRespondsTo.didBegin)
128         [delegate adaptorContextDidBegin:self];
129     return YES;
130 }
131
132 - (BOOL)commitTransaction {
133     if (!transactionNestingLevel || [self hasBusyChannels])
134         return NO;
135
136     if (![channels count])
137         return NO;
138
139     if (delegateRespondsTo.willCommit) {
140         EODelegateResponse response = [delegate adaptorContextWillCommit:self];
141         if (response == EODelegateRejects)
142             return NO;
143         else if (response == EODelegateOverrides)
144             return YES;
145     }
146
147     if ([self primaryCommitTransaction] == NO)
148         return NO;
149
150     [self transactionDidCommit];
151
152     if (delegateRespondsTo.didCommit)
153         [delegate adaptorContextDidCommit:self];
154     return YES;
155 }
156
157 - (BOOL)rollbackTransaction {
158     if (!transactionNestingLevel || [self hasBusyChannels])
159         return NO;
160
161     if (![channels count])
162         return NO;
163
164     if (delegateRespondsTo.willRollback) {
165         EODelegateResponse response
166                 = [delegate adaptorContextWillRollback:self];
167         if (response == EODelegateRejects)
168             return NO;
169         else if (response == EODelegateOverrides)
170             return YES;
171     }
172
173     if ([self primaryRollbackTransaction] == NO)
174         return NO;
175
176     [self transactionDidRollback];
177
178     if (delegateRespondsTo.didRollback)
179         [delegate adaptorContextDidRollback:self];
180     return YES;
181 }
182
183 - (void)transactionDidBegin {
184     /* Increment the transaction scope */
185     transactionNestingLevel++;
186 }
187
188 - (void)transactionDidCommit {
189     /* Decrement the transaction scope */
190     transactionNestingLevel--;
191 }
192
193 - (void)transactionDidRollback {
194     /* Decrement the transaction scope */
195     transactionNestingLevel--;
196 }
197
198 /* delegate */
199
200 - (void)setDelegate:(id)_delegate {
201     self->delegate = _delegate;
202
203     delegateRespondsTo.willBegin = 
204         [delegate respondsToSelector:@selector(adaptorContextWillBegin:)];
205     delegateRespondsTo.didBegin = 
206         [delegate respondsToSelector:@selector(adaptorContextDidBegin:)];
207     delegateRespondsTo.willCommit = 
208         [delegate respondsToSelector:@selector(adaptorContextWillCommit:)];
209     delegateRespondsTo.didCommit = 
210         [delegate respondsToSelector:@selector(adaptorContextDidCommit:)];
211     delegateRespondsTo.willRollback = 
212         [delegate respondsToSelector:@selector(adaptorContextWillRollback:)];
213     delegateRespondsTo.didRollback =
214         [delegate respondsToSelector:@selector(adaptorContextDidRollback:)];
215 }
216 - (id)delegate {
217     return self->delegate;
218 }
219
220 /* adaptor */
221
222 - (EOAdaptor *)adaptor {
223   return self->adaptor;
224 }
225
226 /* transactions */
227
228 - (BOOL)canNestTransactions {
229   /* deprecated in WO 4.5 */
230   return NO;
231 }
232 - (unsigned)transactionNestingLevel {
233   /* deprecated in WO 4.5 */
234   return self->transactionNestingLevel;
235 }
236 - (BOOL)hasOpenTransaction {
237   /* new in WO 4.5 */
238   return self->transactionNestingLevel > 0 ? YES : NO;
239 }
240
241 - (BOOL)primaryBeginTransaction {
242     return NO;
243 }
244
245 - (BOOL)primaryCommitTransaction {
246     return NO;
247 }
248
249 - (BOOL)primaryRollbackTransaction {
250     return NO;
251 }
252
253 @end /* EOAdaptorContext */