* OleDbDataReader.cs: Removed bogus MonoTODO.
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbTransaction.cs
1 //
2 // System.Data.OleDb.OleDbTransaction
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Data.Common;
33
34 namespace System.Data.OleDb
35 {
36 #if NET_2_0
37         public sealed class OleDbTransaction : DbTransaction, IDbTransaction
38 #else
39         public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
40 #endif
41         {
42                 #region Fields
43
44                 OleDbConnection connection;
45                 IntPtr gdaTransaction;
46                 int depth;
47
48                 #endregion // Fields
49
50                 #region Constructors
51
52                 internal OleDbTransaction (OleDbConnection connection, int depth)
53                         : this (connection, depth, IsolationLevel.ReadCommitted)
54                 {
55                 }
56
57                 internal OleDbTransaction (OleDbConnection connection)
58                         : this (connection, 1)
59                 {
60                 }
61
62                 internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel) 
63                 {
64                         this.connection = connection;
65
66                         gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
67                         
68                         switch (isolevel) {
69                         case IsolationLevel.ReadCommitted :
70                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
71                                                                             GdaTransactionIsolation.ReadCommitted);
72                                 break;
73                         case IsolationLevel.ReadUncommitted :
74                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
75                                                                             GdaTransactionIsolation.ReadUncommitted);
76                                 break;
77                         case IsolationLevel.RepeatableRead :
78                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
79                                                                             GdaTransactionIsolation.RepeatableRead);
80                                 break;
81                         case IsolationLevel.Serializable :
82                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
83                                                                             GdaTransactionIsolation.Serializable);
84                                 break;
85                         }
86                         
87                         libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
88                 }
89
90                 internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel) 
91                         : this (connection, 1, isolevel)
92                 {
93                 }
94
95
96                 #endregion // Constructors
97
98                 #region Properties
99
100                 public new OleDbConnection Connection {
101                         get {
102                                 return connection;
103                         }
104                 }
105
106 #if NET_2_0
107                 protected override DbConnection DbConnection {
108                         get { return connection; }
109                 }
110 #endif
111
112                 IDbConnection IDbTransaction.Connection {
113                         get {
114                                 return connection;
115                         }
116                 }
117                 
118                 public
119 #if NET_2_0
120                 override
121 #endif
122                 IsolationLevel IsolationLevel {
123                         get {
124                                 switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
125                                 case GdaTransactionIsolation.ReadCommitted :
126                                         return IsolationLevel.ReadCommitted;
127                                 case GdaTransactionIsolation.ReadUncommitted :
128                                         return IsolationLevel.ReadUncommitted;
129                                 case GdaTransactionIsolation.RepeatableRead :
130                                         return IsolationLevel.RepeatableRead;
131                                 case GdaTransactionIsolation.Serializable :
132                                         return IsolationLevel.Serializable;
133                                 }
134                                 return IsolationLevel.Unspecified;
135                         }
136                 }
137
138                 #endregion // Properties
139
140                 #region Methods
141
142                 public OleDbTransaction Begin ()
143                 {
144                         return new OleDbTransaction (connection, depth + 1);
145                 }
146
147                 public OleDbTransaction Begin (IsolationLevel isolevel) 
148                 {
149                         return new OleDbTransaction (connection, depth + 1, isolevel);
150                 }
151
152                 public
153 #if NET_2_0
154                 override
155 #endif
156                 void Commit ()
157                 {
158                         if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
159                                                                                                                         gdaTransaction))
160                                 throw new InvalidOperationException ();
161                 }
162
163 #if ONLY_1_1
164                 ~OleDbTransaction ()
165                 {
166                         libgda.FreeObject (gdaTransaction);
167                         gdaTransaction = IntPtr.Zero;
168                 }
169 #endif
170
171 #if NET_2_0
172                 protected override void Dispose (bool disposing)
173                 {
174                         base.Dispose (disposing);
175                 }
176 #else
177                 void IDisposable.Dispose ()
178                 {
179                         GC.SuppressFinalize (this);
180                 }
181 #endif
182
183                 public
184 #if NET_2_0
185                 override
186 #endif
187                 void Rollback ()
188                 {
189                         if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
190                                                                                                                                 gdaTransaction))
191                                 throw new InvalidOperationException ();
192                 }
193
194                 #endregion // Methods
195         }
196 }