2005-08-16 Marek Safar <marek.safar@seznam.cz>
[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 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Data;
36 using System.Data.Common;
37
38 namespace System.Data.OleDb
39 {
40         public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
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 OleDbConnection Connection {
101                         get {
102                                 return connection;
103                         }
104                 }
105
106                 IDbConnection IDbTransaction.Connection {
107                         get {
108                                 return connection;
109                         }
110                 }
111                 
112                 public IsolationLevel IsolationLevel {
113                         get {
114                                 switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
115                                 case GdaTransactionIsolation.ReadCommitted :
116                                         return IsolationLevel.ReadCommitted;
117                                 case GdaTransactionIsolation.ReadUncommitted :
118                                         return IsolationLevel.ReadUncommitted;
119                                 case GdaTransactionIsolation.RepeatableRead :
120                                         return IsolationLevel.RepeatableRead;
121                                 case GdaTransactionIsolation.Serializable :
122                                         return IsolationLevel.Serializable;
123                                 }
124
125                                 return IsolationLevel.Unspecified;
126                         }
127                 }
128
129                 #endregion // Properties
130
131                 #region Methods
132
133                 public OleDbTransaction Begin () 
134                 {
135                         return new OleDbTransaction (connection, depth + 1);
136                 }
137
138                 public OleDbTransaction Begin (IsolationLevel isolevel) 
139                 {
140                         return new OleDbTransaction (connection, depth + 1, isolevel);
141                 }
142
143                 public void Commit ()
144                 {
145                         if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
146                                                                        gdaTransaction))
147                                 throw new InvalidOperationException ();
148                 }
149
150                 [MonoTODO]
151                 ~OleDbTransaction ()
152                 {
153                         libgda.FreeObject (gdaTransaction);
154                         gdaTransaction = IntPtr.Zero;
155                 }
156
157                 [MonoTODO]
158                 void IDisposable.Dispose ()
159                 {
160                         throw new NotImplementedException ();
161                 }
162
163                 public void Rollback ()
164                 {
165                         if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
166                                                                          gdaTransaction))
167                                 throw new InvalidOperationException ();
168                 }
169
170                 #endregion // Methods
171         }
172 }