2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Data.PostgreSqlClient / Mono.Data.PostgreSqlClient / PgSqlTransaction.cs
1 //
2 // Mono.Data.PostgreSqlClient.PgSqlTransaction.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //
8 // (C) Ximian, Inc. 2002
9 //
10
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 // use #define DEBUG_SqlTransaction if you want to spew debug messages
33 // #define DEBUG_SqlTransaction
34
35
36 using System;
37 using System.Data;
38 using System.Data.Common;
39
40 namespace Mono.Data.PostgreSqlClient
41 {
42         /// <summary>
43         /// Represents a transaction to be performed on a SQL database.
44         /// </summary>
45         // public sealed class PgSqlTransaction : MarshalByRefObject,
46         //      IDbTransaction, IDisposable
47         public sealed class PgSqlTransaction : IDbTransaction
48         {
49                 #region Fields
50
51                 private bool doingTransaction = false;
52                 private PgSqlConnection conn = null;
53                 private IsolationLevel isolationLevel = 
54                         IsolationLevel.ReadCommitted;
55                 // There are only two IsolationLevel's for PostgreSQL:
56                 //    ReadCommitted and Serializable, 
57                 // but ReadCommitted is the default 
58                 
59                 #endregion
60                
61                 #region Public Methods
62
63                 [MonoTODO]
64                 public void Commit ()
65                 {
66                         if(doingTransaction == false)
67                                 throw new InvalidOperationException(
68                                         "Begin transaction was not " +
69                                         "done earlier " +
70                                         "thus PostgreSQL can not " +
71                                         "Commit transaction.");
72                         
73                         PgSqlCommand cmd = new PgSqlCommand("COMMIT", conn);
74                         cmd.ExecuteNonQuery();
75                                                 
76                         doingTransaction = false;
77                 }               
78
79                 [MonoTODO]
80                 public void Rollback()
81                 {
82                         if(doingTransaction == false)
83                                 throw new InvalidOperationException(
84                                         "Begin transaction was not " +
85                                         "done earlier " +
86                                         "thus PostgreSQL can not " +
87                                         "Rollback transaction.");
88                         
89                         PgSqlCommand cmd = new PgSqlCommand("ROLLBACK", conn);
90                         cmd.ExecuteNonQuery();
91                                                 
92                         doingTransaction = false;
93                 }
94
95                 // For PostgreSQL, Rollback(string) will not be implemented
96                 // because PostgreSQL does not support Savepoints
97                 [Obsolete]
98                 public void Rollback(string transactionName) {
99                         // throw new NotImplementedException ();
100                         Rollback();
101                 }
102
103                 // For PostgreSQL, Save(string) will not be implemented
104                 // because PostgreSQL does not support Savepoints
105                 [Obsolete]
106                 public void Save (string savePointName) {
107                         // throw new NotImplementedException ();
108                 }
109
110                 #endregion // Public Methods
111
112                 #region Internal Methods to Mono.Data.PostgreSqlClient.dll Assembly
113
114                 internal void Begin()
115                 {
116                         if(doingTransaction == true)
117                                 throw new InvalidOperationException(
118                                         "Transaction has begun " +
119                                         "and PostgreSQL does not " +
120                                         "support nested transactions.");
121                         
122                         PgSqlCommand cmd = new PgSqlCommand("BEGIN", conn);
123                         cmd.ExecuteNonQuery();
124                                                 
125                         doingTransaction = true;
126                 }
127
128                 internal void SetIsolationLevel(IsolationLevel isoLevel)
129                 {
130                         String sSql = "SET TRANSACTION ISOLATION LEVEL ";
131  
132                         switch (isoLevel) \r
133                         {
134                                 case IsolationLevel.ReadCommitted:
135                                         sSql += "READ COMMITTED";
136                                         break;
137
138                                 case IsolationLevel.Serializable:
139                                         sSql += "SERIALIZABLE";
140                                         break;
141
142                                 default:
143                                         // FIXME: generate exception here
144                                         // PostgreSQL only supports:
145                                         //   ReadCommitted or Serializable
146                                         break;
147                         }
148                         PgSqlCommand cmd = new PgSqlCommand(sSql, conn);
149                         cmd.ExecuteNonQuery();
150
151                         this.isolationLevel = isoLevel;
152                 }
153
154                 internal void SetConnection(PgSqlConnection connection)
155                 {
156                         this.conn = connection;
157                 }
158
159                 #endregion // Internal Methods to System.Data.dll Assembly
160
161                 #region Properties
162
163                 IDbConnection IDbTransaction.Connection {
164                         get { 
165                                 return Connection; 
166                         }
167                 }
168
169                 public PgSqlConnection Connection       {
170                         get { 
171                                 return conn; 
172                         }
173                 }
174
175                 public IsolationLevel IsolationLevel {
176                         get { 
177                                 return isolationLevel; 
178                         }
179                 }
180
181                 internal bool DoingTransaction {
182                         get {
183                                 return doingTransaction;
184                         }
185                 }
186
187                 #endregion Properties
188
189                 #region Destructors
190
191                 // Destructors aka Finalize and Dispose
192
193                 [MonoTODO]
194                 public void Dispose()
195                 {
196                         // FIXME: need to properly release resources
197                         // Dispose(true);
198                 }
199
200                 // Destructor 
201                 [MonoTODO]
202                 // [Serializable]\r
203                 // [ClassInterface(ClassInterfaceType.AutoDual)]
204                 ~PgSqlTransaction() {
205                         // FIXME: need to properly release resources
206                         // Dispose(false);
207                 }
208
209                 #endregion // Destructors
210
211         }
212 }