New test.
[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         public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
37         {
38                 #region Fields
39
40                 OleDbConnection connection;
41                 IntPtr gdaTransaction;
42                 int depth;
43
44                 #endregion // Fields
45
46                 #region Constructors
47
48                 internal OleDbTransaction (OleDbConnection connection, int depth)
49                         : this (connection, depth, IsolationLevel.ReadCommitted)
50                 {
51                 }
52
53                 internal OleDbTransaction (OleDbConnection connection)
54                         : this (connection, 1)
55                 {
56                 }
57
58                 internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel) 
59                 {
60                         this.connection = connection;
61
62                         gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
63                         
64                         switch (isolevel) {
65                         case IsolationLevel.ReadCommitted :
66                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
67                                                                             GdaTransactionIsolation.ReadCommitted);
68                                 break;
69                         case IsolationLevel.ReadUncommitted :
70                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
71                                                                             GdaTransactionIsolation.ReadUncommitted);
72                                 break;
73                         case IsolationLevel.RepeatableRead :
74                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
75                                                                             GdaTransactionIsolation.RepeatableRead);
76                                 break;
77                         case IsolationLevel.Serializable :
78                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
79                                                                             GdaTransactionIsolation.Serializable);
80                                 break;
81                         }
82                         
83                         libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
84                 }
85
86                 internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel) 
87                         : this (connection, 1, isolevel)
88                 {
89                 }
90
91
92                 #endregion // Constructors
93
94                 #region Properties
95
96                 public OleDbConnection Connection {
97                         get {
98                                 return connection;
99                         }
100                 }
101
102                 IDbConnection IDbTransaction.Connection {
103                         get {
104                                 return connection;
105                         }
106                 }
107                 
108                 public IsolationLevel IsolationLevel {
109                         get {
110                                 switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
111                                 case GdaTransactionIsolation.ReadCommitted :
112                                         return IsolationLevel.ReadCommitted;
113                                 case GdaTransactionIsolation.ReadUncommitted :
114                                         return IsolationLevel.ReadUncommitted;
115                                 case GdaTransactionIsolation.RepeatableRead :
116                                         return IsolationLevel.RepeatableRead;
117                                 case GdaTransactionIsolation.Serializable :
118                                         return IsolationLevel.Serializable;
119                                 }
120
121                                 return IsolationLevel.Unspecified;
122                         }
123                 }
124
125                 #endregion // Properties
126
127                 #region Methods
128
129                 public OleDbTransaction Begin () 
130                 {
131                         return new OleDbTransaction (connection, depth + 1);
132                 }
133
134                 public OleDbTransaction Begin (IsolationLevel isolevel) 
135                 {
136                         return new OleDbTransaction (connection, depth + 1, isolevel);
137                 }
138
139                 public void Commit ()
140                 {
141                         if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
142                                                                        gdaTransaction))
143                                 throw new InvalidOperationException ();
144                 }
145
146                 [MonoTODO]
147                 ~OleDbTransaction ()
148                 {
149                         libgda.FreeObject (gdaTransaction);
150                         gdaTransaction = IntPtr.Zero;
151                 }
152
153                 [MonoTODO]
154                 void IDisposable.Dispose ()
155                 {
156                         GC.SuppressFinalize (this);
157                         throw new NotImplementedException ();
158                 }
159
160                 public void Rollback ()
161                 {
162                         if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
163                                                                          gdaTransaction))
164                                 throw new InvalidOperationException ();
165                 }
166
167                 #endregion // Methods
168         }
169 }