Merge pull request #205 from m3rlinez/master
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcTransaction.cs
1 //
2 // System.Data.Odbc.OdbcTransaction
3 //
4 // Authors:
5 //  Brian Ritchie (brianlritchie@hotmail.com) 
6 //
7 // Copyright (C) Brian Ritchie, 2002
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Data;
32 using System.Data.Common;
33 using System.Globalization;
34
35 namespace System.Data.Odbc
36 {
37 #if NET_2_0
38         public sealed class OdbcTransaction : DbTransaction, IDisposable
39 #else
40         public sealed class OdbcTransaction : MarshalByRefObject, IDbTransaction
41 #endif
42         {
43                 private bool disposed;
44                 private OdbcConnection connection;
45                 private IsolationLevel isolationlevel;
46                 private bool isOpen;
47
48                 internal OdbcTransaction (OdbcConnection conn, IsolationLevel isolationlevel)
49                 {
50                         // Set Auto-commit (102) to false
51                         SetAutoCommit (conn, false);
52                         // Handle isolation level
53                         OdbcIsolationLevel lev = OdbcIsolationLevel.ReadCommitted;
54                         OdbcConnectionAttribute attr = OdbcConnectionAttribute.TransactionIsolation;
55                         switch (isolationlevel) {
56                         case IsolationLevel.ReadUncommitted:
57                                 lev = OdbcIsolationLevel.ReadUncommitted;
58                                 break;
59                         case IsolationLevel.ReadCommitted:
60                                 lev = OdbcIsolationLevel.ReadCommitted;
61                                 break;
62                         case IsolationLevel.RepeatableRead:
63                                 lev = OdbcIsolationLevel.RepeatableRead;
64                                 break;
65                         case IsolationLevel.Serializable:
66                                 lev = OdbcIsolationLevel.Serializable;
67                                 break;
68 #if NET_2_0
69                         case IsolationLevel.Snapshot:
70                                 // badly broken on MS:
71                                 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=305736
72                                 lev = OdbcIsolationLevel.Snapshot;
73
74                                 // SQL_ATTR_TXN_ISOLATION can be used to set all other isolation
75                                 // levels except for SQL_TXN_SS_SNAPSHOT. If you want to use snapshot
76                                 // isolation, you must set SQL_TXN_SS_SNAPSHOT through
77                                 // SQL_COPT_SS_TXN_ISOLATION. However, you can retrieve the
78                                 // isolation level by using either SQL_ATTR_TXN_ISOLATION or
79                                 // SQL_COPT_SS_TXN_ISOLATION.
80                                 // Source:
81                                 // http://msdn2.microsoft.com/en-us/library/ms131709.aspx
82                                 attr = OdbcConnectionAttribute.CoptTransactionIsolation;
83                                 break;
84 #endif
85                         case IsolationLevel.Unspecified:
86                                 // when isolationlevel is not specified, then use
87                                 // default isolation level of the driver and
88                                 // lazy initialize it in the IsolationLevel property
89                                 break;
90 #if NET_2_0
91                         case IsolationLevel.Chaos:
92                                 throw new ArgumentOutOfRangeException ("IsolationLevel",
93                                         string.Format (CultureInfo.CurrentCulture,
94                                                 "The IsolationLevel enumeration " +
95                                                 "value, {0}, is not supported by " +
96                                                 "the .Net Framework Odbc Data " +
97                                                 "Provider.", (int) isolationlevel));
98 #endif
99                         default:
100 #if NET_2_0
101                                 throw new ArgumentOutOfRangeException ("IsolationLevel",
102                                         string.Format (CultureInfo.CurrentCulture,
103                                                 "The IsolationLevel enumeration value, {0}, is invalid.",
104                                                 (int) isolationlevel));
105 #else
106                                 throw new ArgumentException (string.Format (
107                                         CultureInfo.InvariantCulture,
108                                         "Not supported isolationlevel - {0}",
109                                         isolationlevel));
110 #endif
111                         }
112
113                         // only change isolation level if it was explictly set
114                         if (isolationlevel != IsolationLevel.Unspecified) {
115                                 // mbd: Getting the return code of the second call to SQLSetConnectAttr is missing from original code!
116                                 OdbcReturn ret = libodbc.SQLSetConnectAttr (conn.hDbc,
117                                         attr, (IntPtr) lev, 0);
118                                 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
119                                         throw conn.CreateOdbcException (OdbcHandleType.Dbc, conn.hDbc);
120                         }
121                         this.isolationlevel = isolationlevel;
122                         connection = conn;
123                         isOpen = true;
124                 }
125
126                 // Set Auto-commit (102) connection attribute
127                 // [MonoTODO]: nice to have before svn: define libodbc.SQL_IS_UINTEGER = -5
128                 private static void SetAutoCommit (OdbcConnection conn, bool isAuto)
129                 {
130                         OdbcReturn ret = libodbc.SQLSetConnectAttr (conn.hDbc,
131                                 OdbcConnectionAttribute.AutoCommit,
132                                 (IntPtr) (isAuto ? 1 : 0), -5);
133                         if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
134                                 throw conn.CreateOdbcException (OdbcHandleType.Dbc, conn.hDbc);
135                 }
136
137                 private static IsolationLevel GetIsolationLevel (OdbcConnection conn)
138                 {
139                         int lev;
140                         int length;
141                         OdbcReturn ret = libodbc.SQLGetConnectAttr (conn.hDbc,
142                                 OdbcConnectionAttribute.TransactionIsolation,
143                                 out lev, 0, out length);
144                         if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
145                                 throw conn.CreateOdbcException (OdbcHandleType.Dbc, conn.hDbc);
146                         return MapOdbcIsolationLevel ((OdbcIsolationLevel) lev);
147                 }
148
149                 private static IsolationLevel MapOdbcIsolationLevel (OdbcIsolationLevel odbcLevel)
150                 {
151                         IsolationLevel isoLevel = IsolationLevel.Unspecified;
152
153                         switch (odbcLevel) {
154                         case OdbcIsolationLevel.ReadUncommitted:
155                                 isoLevel = IsolationLevel.ReadUncommitted;
156                                 break;
157                         case OdbcIsolationLevel.ReadCommitted:
158                                 isoLevel = IsolationLevel.ReadCommitted;
159                                 break;
160                         case OdbcIsolationLevel.RepeatableRead:
161                                 isoLevel = IsolationLevel.RepeatableRead;
162                                 break;
163                         case OdbcIsolationLevel.Serializable:
164                                 isoLevel = IsolationLevel.Serializable;
165                                 break;
166 #if NET_2_0
167                         case OdbcIsolationLevel.Snapshot:
168                                 isoLevel = IsolationLevel.Snapshot;
169                                 break;
170 #else
171                         default:
172                                 throw new NotSupportedException (string.Format (
173                                         CultureInfo.InvariantCulture,
174                                         "Isolation level {0} is not supported.",
175                                         odbcLevel));
176 #endif
177                         }
178                         return isoLevel;
179                 }
180
181                 #region Implementation of IDisposable
182
183 #if NET_2_0
184                 protected override
185 #endif
186                 void Dispose (bool disposing)
187                 {
188                         if (!disposed) {
189                                 if (disposing && isOpen)
190                                         Rollback ();
191                                 disposed = true;
192                         }
193                 }
194
195                 void IDisposable.Dispose ()
196                 {
197                         Dispose (true);
198                         GC.SuppressFinalize (this);
199                 }
200
201                 #endregion Implementation of IDisposable
202
203                 #region Implementation of IDbTransaction
204
205                 public
206 #if NET_2_0
207                 override
208 #endif //NET_2_0
209                 void Commit ()
210                 {
211                         if (!isOpen)
212                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
213
214                         if (connection.transaction == this) {
215                                 OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 0);
216                                 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
217                                         throw connection.CreateOdbcException (OdbcHandleType.Dbc, connection.hDbc);
218                                 SetAutoCommit (connection, true); // restore default auto-commit
219                                 connection.transaction = null;
220                                 connection = null;
221                                 isOpen = false;
222                         } else
223                                 throw new InvalidOperationException ();
224                 }
225
226                 public
227 #if NET_2_0
228                 override
229 #endif //NET_2_0
230                 void Rollback()
231                 {
232                         if (!isOpen)
233                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
234
235                         if (connection.transaction == this) {
236                                 OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 1);
237                                 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
238                                         throw connection.CreateOdbcException (OdbcHandleType.Dbc, connection.hDbc);
239                                 SetAutoCommit (connection, true);    // restore default auto-commit
240                                 connection.transaction = null;
241                                 connection = null;
242                                 isOpen = false;
243                         } else
244                                 throw new InvalidOperationException ();
245                 }
246
247 #if NET_2_0
248                 protected override DbConnection DbConnection {
249                         get {
250                                 return Connection;
251                         }
252                 }
253 #else
254                 IDbConnection IDbTransaction.Connection {
255                         get {
256                                 return Connection;
257                         }
258                 }
259
260 #endif
261
262                 public
263 #if NET_2_0
264                 override
265 #endif
266                 IsolationLevel IsolationLevel {
267                         get {
268                                 if (!isOpen)
269                                         throw ExceptionHelper.TransactionNotUsable (GetType ());
270
271                                 if (isolationlevel == IsolationLevel.Unspecified)
272                                         isolationlevel = GetIsolationLevel (Connection);
273                                 return isolationlevel;
274                         }
275                 }
276
277                 #endregion Implementation of IDbTransaction
278
279                 #region Public Instance Properties
280
281                 public new OdbcConnection Connection {
282                         get {
283                                 return connection;
284                         }
285                 }
286
287                 #endregion Public Instance Properties
288         }
289 }