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