]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOAdaptorContext.m
renamed PostgreSQL72 to PostgreSQL, install in Library/GDLAdaptors-1.1
[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/NSUtilities.h>
28 #import <Foundation/NSValue.h>
29 #import <Foundation/NSArray.h>
30
31 #import "common.h"
32 #import "EOAdaptor.h"
33 #import "EOAdaptorContext.h"
34 #import "EOAdaptorChannel.h"
35
36 @implementation EOAdaptorContext
37
38 - (id)initWithAdaptor :(EOAdaptor*)_adaptor
39 {
40     ASSIGN(self->adaptor, _adaptor);
41     self->channels = [[NSMutableArray alloc] initWithCapacity:2];
42     [self->adaptor contextDidInit:self];
43     return self;
44 }
45
46 - (void)dealloc
47 {
48     [self->adaptor contextWillDealloc:self];
49     RELEASE(self->adaptor);
50     RELEASE(self->channels);
51     [super dealloc];
52 }
53
54 - (EOAdaptorChannel*)createAdaptorChannel
55 {
56     return AUTORELEASE([[[adaptor adaptorChannelClass] alloc]
57                            initWithAdaptorContext:self]);
58 }
59 - (NSArray *)channels
60 {
61   NSMutableArray *ma;
62   unsigned i, count;
63   
64   if ((count = [self->channels count]) == 0)
65     return nil;
66
67   ma = [NSMutableArray arrayWithCapacity:count];
68   for (i = 0; i < count; i++)
69     [ma addObject:[[self->channels objectAtIndex:i] nonretainedObjectValue]];
70   
71   return ma;
72 }
73
74 - (void)channelDidInit:aChannel
75 {
76     [self->channels addObject:[NSValue valueWithNonretainedObject:aChannel]];
77 }
78
79 - (void)channelWillDealloc:(id)aChannel
80 {
81     int i;
82     
83     for (i = [self->channels count] - 1; i >= 0; i--)
84         if ([[channels objectAtIndex:i] nonretainedObjectValue] == aChannel) {
85             [channels removeObjectAtIndex:i];
86             break;
87         }
88 }
89
90 - (BOOL)hasOpenChannels
91 {
92     int i, count = [channels count];
93
94     for (i = 0; i < count; i++)
95         if ([[[channels objectAtIndex:i] nonretainedObjectValue] isOpen])
96             return YES;
97
98     return NO;
99 }
100
101 - (BOOL)hasBusyChannels
102 {
103     int i, count = [channels count];
104
105     for (i = 0; i < count; i++)
106         if ([[[channels objectAtIndex:i] nonretainedObjectValue]
107                 isFetchInProgress])
108             return YES;
109
110     return NO;
111 }
112
113 - (BOOL)beginTransaction
114 {
115     if (transactionNestingLevel && ![self canNestTransactions])
116         return NO;
117
118     if ([self->channels count] == 0)
119         return NO;
120
121     if(delegateRespondsTo.willBegin) {
122         EODelegateResponse response = [delegate adaptorContextWillBegin:self];
123         if(response == EODelegateRejects)
124             return NO;
125         else if(response == EODelegateOverrides)
126             return YES;
127     }
128     if([self primaryBeginTransaction] == NO)
129         return NO;
130
131     [self transactionDidBegin];
132
133     if(delegateRespondsTo.didBegin)
134         [delegate adaptorContextDidBegin:self];
135     return YES;
136 }
137
138 - (BOOL)commitTransaction
139 {
140     if(!transactionNestingLevel || [self hasBusyChannels])
141         return NO;
142
143     if (![channels count])
144         return NO;
145
146     if(delegateRespondsTo.willCommit) {
147         EODelegateResponse response = [delegate adaptorContextWillCommit:self];
148         if(response == EODelegateRejects)
149             return NO;
150         else if(response == EODelegateOverrides)
151             return YES;
152     }
153
154     if([self primaryCommitTransaction] == NO)
155         return NO;
156
157     [self transactionDidCommit];
158
159     if(delegateRespondsTo.didCommit)
160         [delegate adaptorContextDidCommit:self];
161     return YES;
162 }
163
164 - (BOOL)rollbackTransaction
165 {
166     if(!transactionNestingLevel || [self hasBusyChannels])
167         return NO;
168
169     if (![channels count])
170         return NO;
171
172     if(delegateRespondsTo.willRollback) {
173         EODelegateResponse response
174                 = [delegate adaptorContextWillRollback:self];
175         if(response == EODelegateRejects)
176             return NO;
177         else if(response == EODelegateOverrides)
178             return YES;
179     }
180
181     if([self primaryRollbackTransaction] == NO)
182         return NO;
183
184     [self transactionDidRollback];
185
186     if(delegateRespondsTo.didRollback)
187         [delegate adaptorContextDidRollback:self];
188     return YES;
189 }
190
191 - (void)transactionDidBegin
192 {
193     /* Increment the transaction scope */
194     transactionNestingLevel++;
195 }
196
197 - (void)transactionDidCommit
198 {
199     /* Decrement the transaction scope */
200     transactionNestingLevel--;
201 }
202
203 - (void)transactionDidRollback
204 {
205     /* Decrement the transaction scope */
206     transactionNestingLevel--;
207 }
208
209 - (void)setDelegate:(id)_delegate {
210     self->delegate = _delegate;
211
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:)];
224 }
225 - (id)delegate {
226     return self->delegate;
227 }
228
229 - (EOAdaptor *)adaptor {
230   return self->adaptor;
231 }
232 - (BOOL)canNestTransactions {
233   /* deprecated in WO 4.5 */
234   return NO;
235 }
236 - (unsigned)transactionNestingLevel {
237   /* deprecated in WO 4.5 */
238   return self->transactionNestingLevel;
239 }
240 - (BOOL)hasOpenTransaction {
241   /* new in WO 4.5 */
242   return self->transactionNestingLevel > 0 ? YES : NO;
243 }
244
245 - (BOOL)primaryBeginTransaction {
246     return NO;
247 }
248
249 - (BOOL)primaryCommitTransaction {
250     return NO;
251 }
252
253 - (BOOL)primaryRollbackTransaction {
254     return NO;
255 }
256
257 @end /* EOAdaptorContext */