4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
9 This file is part of the GNUstep Database Library.
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.
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.
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.
27 #import <Foundation/NSUtilities.h>
28 #import <Foundation/NSValue.h>
29 #import <Foundation/NSArray.h>
33 #import "EOAdaptorContext.h"
34 #import "EOAdaptorChannel.h"
36 @implementation EOAdaptorContext
38 - (id)initWithAdaptor :(EOAdaptor*)_adaptor
40 ASSIGN(self->adaptor, _adaptor);
41 self->channels = [[NSMutableArray alloc] initWithCapacity:2];
42 [self->adaptor contextDidInit:self];
48 [self->adaptor contextWillDealloc:self];
49 RELEASE(self->adaptor);
50 RELEASE(self->channels);
54 - (EOAdaptorChannel*)createAdaptorChannel
56 return AUTORELEASE([[[adaptor adaptorChannelClass] alloc]
57 initWithAdaptorContext:self]);
64 if ((count = [self->channels count]) == 0)
67 ma = [NSMutableArray arrayWithCapacity:count];
68 for (i = 0; i < count; i++)
69 [ma addObject:[[self->channels objectAtIndex:i] nonretainedObjectValue]];
74 - (void)channelDidInit:aChannel
76 [self->channels addObject:[NSValue valueWithNonretainedObject:aChannel]];
79 - (void)channelWillDealloc:(id)aChannel
83 for (i = [self->channels count] - 1; i >= 0; i--)
84 if ([[channels objectAtIndex:i] nonretainedObjectValue] == aChannel) {
85 [channels removeObjectAtIndex:i];
90 - (BOOL)hasOpenChannels
92 int i, count = [channels count];
94 for (i = 0; i < count; i++)
95 if ([[[channels objectAtIndex:i] nonretainedObjectValue] isOpen])
101 - (BOOL)hasBusyChannels
103 int i, count = [channels count];
105 for (i = 0; i < count; i++)
106 if ([[[channels objectAtIndex:i] nonretainedObjectValue]
113 - (BOOL)beginTransaction
115 if (transactionNestingLevel && ![self canNestTransactions])
118 if ([self->channels count] == 0)
121 if(delegateRespondsTo.willBegin) {
122 EODelegateResponse response = [delegate adaptorContextWillBegin:self];
123 if(response == EODelegateRejects)
125 else if(response == EODelegateOverrides)
128 if([self primaryBeginTransaction] == NO)
131 [self transactionDidBegin];
133 if(delegateRespondsTo.didBegin)
134 [delegate adaptorContextDidBegin:self];
138 - (BOOL)commitTransaction
140 if(!transactionNestingLevel || [self hasBusyChannels])
143 if (![channels count])
146 if(delegateRespondsTo.willCommit) {
147 EODelegateResponse response = [delegate adaptorContextWillCommit:self];
148 if(response == EODelegateRejects)
150 else if(response == EODelegateOverrides)
154 if([self primaryCommitTransaction] == NO)
157 [self transactionDidCommit];
159 if(delegateRespondsTo.didCommit)
160 [delegate adaptorContextDidCommit:self];
164 - (BOOL)rollbackTransaction
166 if(!transactionNestingLevel || [self hasBusyChannels])
169 if (![channels count])
172 if(delegateRespondsTo.willRollback) {
173 EODelegateResponse response
174 = [delegate adaptorContextWillRollback:self];
175 if(response == EODelegateRejects)
177 else if(response == EODelegateOverrides)
181 if([self primaryRollbackTransaction] == NO)
184 [self transactionDidRollback];
186 if(delegateRespondsTo.didRollback)
187 [delegate adaptorContextDidRollback:self];
191 - (void)transactionDidBegin
193 /* Increment the transaction scope */
194 transactionNestingLevel++;
197 - (void)transactionDidCommit
199 /* Decrement the transaction scope */
200 transactionNestingLevel--;
203 - (void)transactionDidRollback
205 /* Decrement the transaction scope */
206 transactionNestingLevel--;
209 - (void)setDelegate:(id)_delegate {
210 self->delegate = _delegate;
212 delegateRespondsTo.willBegin =
213 [delegate respondsToSelector:@selector(adaptorContextWillBegin:)];
214 delegateRespondsTo.didBegin =
215 [delegate respondsToSelector:@selector(adaptorContextDidBegin:)];
216 delegateRespondsTo.willCommit =
217 [delegate respondsToSelector:@selector(adaptorContextWillCommit:)];
218 delegateRespondsTo.didCommit =
219 [delegate respondsToSelector:@selector(adaptorContextDidCommit:)];
220 delegateRespondsTo.willRollback =
221 [delegate respondsToSelector:@selector(adaptorContextWillRollback:)];
222 delegateRespondsTo.didRollback =
223 [delegate respondsToSelector:@selector(adaptorContextDidRollback:)];
226 return self->delegate;
229 - (EOAdaptor *)adaptor {
230 return self->adaptor;
232 - (BOOL)canNestTransactions {
233 /* deprecated in WO 4.5 */
236 - (unsigned)transactionNestingLevel {
237 /* deprecated in WO 4.5 */
238 return self->transactionNestingLevel;
240 - (BOOL)hasOpenTransaction {
242 return self->transactionNestingLevel > 0 ? YES : NO;
245 - (BOOL)primaryBeginTransaction {
249 - (BOOL)primaryCommitTransaction {
253 - (BOOL)primaryRollbackTransaction {
257 @end /* EOAdaptorContext */