Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbConnection.cs
1 //
2 // System.Data.OleDb.OleDbConnection
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.ComponentModel;
36 using System.Data;
37 using System.Data.Common;
38 using System.EnterpriseServices;
39 #if NET_2_0
40 using System.Transactions;
41 #endif
42
43 namespace System.Data.OleDb
44 {
45         [DefaultEvent ("InfoMessage")]
46 #if NET_2_0
47         public sealed class OleDbConnection : DbConnection, ICloneable
48 #else
49         public sealed class OleDbConnection : Component, ICloneable, IDbConnection
50 #endif
51         {
52                 #region Fields
53
54                 string connectionString;
55                 int connectionTimeout;
56                 IntPtr gdaConnection;
57
58                 #endregion
59
60                 #region Constructors
61                 
62                 public OleDbConnection ()
63                 {
64                         gdaConnection = IntPtr.Zero;
65                         connectionTimeout = 15;
66                 }
67
68                 public OleDbConnection (string connectionString) : this ()
69                 {
70                         this.connectionString = connectionString;
71                 }
72
73                 #endregion // Constructors
74
75                 #region Properties
76                 
77                 [DataCategory ("Data")]
78                 [DefaultValue ("")]
79                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
80                 [RecommendedAsConfigurable (true)]
81                 [RefreshPropertiesAttribute (RefreshProperties.All)]
82                 public override string ConnectionString {
83                         get {
84                                 if (connectionString == null)
85                                         return string.Empty;
86                                 return connectionString;
87                         }
88                         set {
89                                 connectionString = value;
90                         }
91                 }
92
93                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
94                 public override int ConnectionTimeout {
95                         get {
96                                 return connectionTimeout;
97                         }
98                 }
99
100                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
101                 public override 
102                 string Database {
103                         get {
104                                 if (gdaConnection != IntPtr.Zero
105                                         && libgda.gda_connection_is_open (gdaConnection)) {
106                                         return libgda.gda_connection_get_database (gdaConnection);
107                                 }
108
109                                 return string.Empty;
110                         }
111                 }
112
113                 [BrowsableAttribute (true)]
114                 public override string DataSource {
115                         get {
116                                 if (gdaConnection != IntPtr.Zero
117                                         && libgda.gda_connection_is_open (gdaConnection)) {
118                                         return libgda.gda_connection_get_dsn (gdaConnection);
119                                 }
120
121                                 return string.Empty;
122                         }
123                 }
124
125                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
126                 [BrowsableAttribute (true)]
127                 public string Provider {
128                         get {
129                                 if (gdaConnection != IntPtr.Zero
130                                         && libgda.gda_connection_is_open (gdaConnection)) {
131                                         return libgda.gda_connection_get_provider (gdaConnection);
132                                 }
133
134                                 return string.Empty;
135                         }
136                 }
137
138                 public override string ServerVersion {
139                         get {
140                                 if (State == ConnectionState.Closed)
141                                         throw ExceptionHelper.ConnectionClosed ();
142                                 return libgda.gda_connection_get_server_version (gdaConnection);
143                         }
144                 }
145
146                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
147                 [BrowsableAttribute (false)]
148                 public override ConnectionState State {
149                         get {
150                                 if (gdaConnection != IntPtr.Zero) {
151                                         if (libgda.gda_connection_is_open (gdaConnection))
152                                                 return ConnectionState.Open;
153                                 }
154
155                                 return ConnectionState.Closed;
156                         }
157                 }
158
159                 internal IntPtr GdaConnection {
160                         get {
161                                 return gdaConnection;
162                         }
163                 }
164                 
165                 #endregion // Properties
166         
167                 #region Methods
168         
169                 public new OleDbTransaction BeginTransaction ()
170                 {
171                         if (State == ConnectionState.Closed)
172                                 throw ExceptionHelper.ConnectionClosed ();
173                         return new OleDbTransaction (this);
174                 }
175
176                 public new OleDbTransaction BeginTransaction (IsolationLevel isolationLevel)
177                 {
178                         if (State == ConnectionState.Closed)
179                                 throw ExceptionHelper.ConnectionClosed ();
180                         return new OleDbTransaction (this, isolationLevel);
181                 }
182
183                 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
184                 {
185                         return BeginTransaction (isolationLevel);
186                 }
187
188                 protected override DbCommand CreateDbCommand()
189                 {
190                         return CreateCommand ();
191                 }
192
193                 public override void ChangeDatabase (string value)
194                 {
195                         if (State != ConnectionState.Open)
196                                 throw new InvalidOperationException ();
197
198                         if (!libgda.gda_connection_change_database (gdaConnection, value))
199                                 throw new OleDbException (this);
200                 }
201
202                 public override void Close ()
203                 {
204                         if (State == ConnectionState.Open) {
205                                 libgda.gda_connection_close (gdaConnection);
206                                 gdaConnection = IntPtr.Zero;
207                         }
208                 }
209
210                 public new OleDbCommand CreateCommand ()
211                 {
212                         if (State == ConnectionState.Open)
213                                 return new OleDbCommand (null, this);
214
215                         return null;
216                 }
217
218                 [MonoTODO]
219                 protected override void Dispose (bool disposing)
220                 {
221                         throw new NotImplementedException ();
222                 }
223
224                 [MonoTODO]
225                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
226                 {
227                         throw new NotImplementedException ();
228                 }
229
230                 [MonoTODO]
231                 object ICloneable.Clone ()
232                 {
233                         throw new NotImplementedException();
234                 }
235
236                 public
237 #if NET_2_0
238                 override
239 #endif
240                 void Open ()
241                 {
242 //                      string provider = "Default";
243 //                      string gdaCncStr = string.Empty;
244 //                      string[] args;
245 //                      int len;
246 //                      char [] separator = { ';' };
247                         
248                         if (State == ConnectionState.Open)
249                                 throw new InvalidOperationException ();
250
251                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
252
253                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
254                                 ConnectionString, string.Empty, string.Empty, 0);
255
256                         if (gdaConnection == IntPtr.Zero)
257                                 throw new OleDbException (this);
258                         /* convert the connection string to its GDA equivalent */
259                         //args = connectionString.Split (';');
260                         //len = args.Length;
261                         //for (int i = 0; i < len; i++) {
262                         //      string[] values = args[i].Split (separator, 2);
263                         //      if (values[0] == "Provider") {
264                         //              if (values[1] == "SQLOLEDB")
265                         //                      provider = "FreeTDS";
266                         //              else if (values[1] == "MSDAORA")
267                         //                      provider = "Oracle";
268                         //              else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
269                         //                      provider = "MS Access";
270                         //              else
271                         //                      provider = values[2];
272                         //      }
273                         //      else if (values[0] == "Addr" || values[0] == "Address")
274                         //              gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
275                         //      else if (values[0] == "Database")
276                         //              gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
277                         //      else if (values[0] == "Connection Lifetime")
278                         //              connectionTimeout = System.Convert.ToInt32 (values[1]);
279                         //      else if (values[0] == "File Name")
280                         //              gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
281                         //      else if (values[0] == "Password" || values[0] == "Pwd")
282                         //              gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
283                         //      else if (values[0] == "User ID")
284                         //              gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
285                         //}
286
287                         /* open the connection */
288                         //System.Console.WriteLine ("Opening connection for provider " +
289                         //                provider + " with " + gdaCncStr);
290                         //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
291                         //                                                             provider,
292                         //                                                             gdaCncStr);
293                 }
294
295                 [MonoTODO]
296                 public static void ReleaseObjectPool ()
297                 {
298                         throw new NotImplementedException ();
299                 }
300
301                 [MonoTODO]
302                 public void EnlistDistributedTransaction (ITransaction transaction)
303                 {
304                         throw new NotImplementedException ();
305                 }
306
307 #if NET_2_0
308                 [MonoTODO]
309                 public override void EnlistTransaction (Transaction transaction)
310                 {
311                         throw new NotImplementedException ();
312                 }
313
314                 [MonoTODO]
315                 public override DataTable GetSchema ()
316                 {
317                         if (State == ConnectionState.Closed)
318                                 throw ExceptionHelper.ConnectionClosed ();
319                         throw new NotImplementedException ();
320                 }
321
322                 [MonoTODO]
323                 public override DataTable GetSchema(string collectionName)
324                 {
325                         return GetSchema (collectionName, null);
326                 }
327
328                 [MonoTODO]
329                 public override DataTable GetSchema (String collectionName, string [] restrictionValues)
330                 {
331                         if (State == ConnectionState.Closed)
332                                 throw ExceptionHelper.ConnectionClosed ();
333                         throw new NotImplementedException ();
334                 }
335
336                 [MonoTODO]
337                 [EditorBrowsable (EditorBrowsableState.Advanced)]
338                 public void ResetState ()
339                 {
340                         throw new NotImplementedException ();
341                 }
342 #endif
343
344                 #endregion
345
346                 #region Events and Delegates
347
348 #if !NET_2_0
349                 [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
350 #endif
351                 [DataCategory ("DataCategory_InfoMessage")]
352                 public event OleDbInfoMessageEventHandler InfoMessage;
353
354 #if !NET_2_0
355                 [DataSysDescription ("Event triggered when the connection changes state.")]
356                 [DataCategory ("DataCategory_StateChange")]
357                 public event StateChangeEventHandler StateChange;
358 #endif
359
360                 #endregion
361         }
362 }