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/NSValue.h>
28 #import <Foundation/NSArray.h>
32 #import "EOAdaptorContext.h"
33 #import "EOAdaptorChannel.h"
35 @implementation EOAdaptorContext
37 - (id)initWithAdaptor:(EOAdaptor *)_adaptor {
38 ASSIGN(self->adaptor, _adaptor);
39 self->channels = [[NSMutableArray alloc] initWithCapacity:2];
40 [self->adaptor contextDidInit:self];
45 [self->adaptor contextWillDealloc:self];
46 [self->adaptor release];
47 [self->channels release];
53 - (EOAdaptorChannel *)createAdaptorChannel {
54 return [[[[adaptor adaptorChannelClass] alloc]
55 initWithAdaptorContext:self] autorelease];
57 - (NSArray *)channels {
61 if ((count = [self->channels count]) == 0)
64 ma = [NSMutableArray arrayWithCapacity:count];
65 for (i = 0; i < count; i++)
66 [ma addObject:[[self->channels objectAtIndex:i] nonretainedObjectValue]];
71 - (void)channelDidInit:aChannel {
72 [self->channels addObject:[NSValue valueWithNonretainedObject:aChannel]];
75 - (void)channelWillDealloc:(id)aChannel {
78 for (i = [self->channels count] - 1; i >= 0; i--)
79 if ([[channels objectAtIndex:i] nonretainedObjectValue] == aChannel) {
80 [channels removeObjectAtIndex:i];
85 - (BOOL)hasOpenChannels {
86 int i, count = [channels count];
88 for (i = 0; i < count; i++)
89 if ([[[channels objectAtIndex:i] nonretainedObjectValue] isOpen])
95 - (BOOL)hasBusyChannels {
96 int i, count = [channels count];
98 for (i = 0; i < count; i++)
99 if ([[[channels objectAtIndex:i] nonretainedObjectValue]
108 - (BOOL)beginTransaction {
109 if (transactionNestingLevel && ![self canNestTransactions])
112 if ([self->channels count] == 0)
115 if (delegateRespondsTo.willBegin) {
116 EODelegateResponse response = [delegate adaptorContextWillBegin:self];
117 if (response == EODelegateRejects)
119 else if (response == EODelegateOverrides)
122 if ([self primaryBeginTransaction] == NO)
125 [self transactionDidBegin];
127 if (delegateRespondsTo.didBegin)
128 [delegate adaptorContextDidBegin:self];
132 - (BOOL)commitTransaction {
133 if (!transactionNestingLevel || [self hasBusyChannels])
136 if (![channels count])
139 if (delegateRespondsTo.willCommit) {
140 EODelegateResponse response = [delegate adaptorContextWillCommit:self];
141 if (response == EODelegateRejects)
143 else if (response == EODelegateOverrides)
147 if ([self primaryCommitTransaction] == NO)
150 [self transactionDidCommit];
152 if (delegateRespondsTo.didCommit)
153 [delegate adaptorContextDidCommit:self];
157 - (BOOL)rollbackTransaction {
158 if (!transactionNestingLevel || [self hasBusyChannels])
161 if (![channels count])
164 if (delegateRespondsTo.willRollback) {
165 EODelegateResponse response
166 = [delegate adaptorContextWillRollback:self];
167 if (response == EODelegateRejects)
169 else if (response == EODelegateOverrides)
173 if ([self primaryRollbackTransaction] == NO)
176 [self transactionDidRollback];
178 if (delegateRespondsTo.didRollback)
179 [delegate adaptorContextDidRollback:self];
183 - (void)transactionDidBegin {
184 /* Increment the transaction scope */
185 transactionNestingLevel++;
188 - (void)transactionDidCommit {
189 /* Decrement the transaction scope */
190 transactionNestingLevel--;
193 - (void)transactionDidRollback {
194 /* Decrement the transaction scope */
195 transactionNestingLevel--;
200 - (void)setDelegate:(id)_delegate {
201 self->delegate = _delegate;
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:)];
217 return self->delegate;
222 - (EOAdaptor *)adaptor {
223 return self->adaptor;
228 - (BOOL)canNestTransactions {
229 /* deprecated in WO 4.5 */
232 - (unsigned)transactionNestingLevel {
233 /* deprecated in WO 4.5 */
234 return self->transactionNestingLevel;
236 - (BOOL)hasOpenTransaction {
238 return self->transactionNestingLevel > 0 ? YES : NO;
241 - (BOOL)primaryBeginTransaction {
245 - (BOOL)primaryCommitTransaction {
249 - (BOOL)primaryRollbackTransaction {
253 @end /* EOAdaptorContext */