82d148ea0b06568a842a04ed4a0d0bc094bb26ba
[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 #if NET_1_0 || ONLY_1_1
80                 [DataSysDescriptionAttribute ("Information used to connect to a Data Source.")]
81 #endif
82                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
83                 [RecommendedAsConfigurable (true)]
84                 [RefreshPropertiesAttribute (RefreshProperties.All)]
85                 public
86 #if NET_2_0
87                 override
88 #endif
89                 string ConnectionString {
90                         get {
91                                 if (connectionString == null)
92                                         return string.Empty;
93                                 return connectionString;
94                         }
95                         set {
96                                 connectionString = value;
97                         }
98                 }
99
100                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
101 #if !NET_2_0
102                 [DataSysDescriptionAttribute ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")]
103 #endif
104                 public
105 #if NET_2_0
106                 override
107 #endif
108                 int ConnectionTimeout {
109                         get {
110                                 return connectionTimeout;
111                         }
112                 }
113
114                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
115 #if !NET_2_0
116                 [DataSysDescriptionAttribute ("Current data source catalog value, 'Initial Catalog=X' in the connection string.")]
117 #endif
118                 public
119 #if NET_2_0
120                 override
121 #endif
122                 string Database {
123                         get {
124                                 if (gdaConnection != IntPtr.Zero
125                                         && libgda.gda_connection_is_open (gdaConnection)) {
126                                         return libgda.gda_connection_get_database (gdaConnection);
127                                 }
128
129                                 return string.Empty;
130                         }
131                 }
132
133 #if NET_2_0
134                 [BrowsableAttribute (true)]
135 #else
136                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
137                 [DataSysDescriptionAttribute ("Current data source, 'Data Source=X' in the connection string.")]
138 #endif
139                 public
140 #if NET_2_0
141                 override
142 #endif
143                 string DataSource {
144                         get {
145                                 if (gdaConnection != IntPtr.Zero
146                                         && libgda.gda_connection_is_open (gdaConnection)) {
147                                         return libgda.gda_connection_get_dsn (gdaConnection);
148                                 }
149
150                                 return string.Empty;
151                         }
152                 }
153
154                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
155 #if NET_2_0
156                 [BrowsableAttribute (true)]
157 #else
158                 [DataSysDescriptionAttribute ("Current OLE DB provider progid, 'Provider=X' in the connection string.")]
159 #endif
160                 public string Provider {
161                         get {
162                                 if (gdaConnection != IntPtr.Zero
163                                         && libgda.gda_connection_is_open (gdaConnection)) {
164                                         return libgda.gda_connection_get_provider (gdaConnection);
165                                 }
166
167                                 return string.Empty;
168                         }
169                 }
170
171 #if !NET_2_0
172                 [DataSysDescriptionAttribute ("Version of the product accessed by the OLE DB Provider.")]
173                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
174                 [BrowsableAttribute (false)]
175 #endif
176                 public
177 #if NET_2_0
178                 override
179 #endif
180                 string ServerVersion {
181                         get {
182                                 if (State == ConnectionState.Closed)
183                                         throw ExceptionHelper.ConnectionClosed ();
184                                 return libgda.gda_connection_get_server_version (gdaConnection);
185                         }
186                 }
187
188                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
189 #if !NET_2_0
190                 [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed.")]
191 #endif
192                 [BrowsableAttribute (false)]
193                 public
194 #if NET_2_0
195                 override
196 #endif
197                 ConnectionState State {
198                         get {
199                                 if (gdaConnection != IntPtr.Zero) {
200                                         if (libgda.gda_connection_is_open (gdaConnection))
201                                                 return ConnectionState.Open;
202                                 }
203
204                                 return ConnectionState.Closed;
205                         }
206                 }
207
208                 internal IntPtr GdaConnection {
209                         get {
210                                 return gdaConnection;
211                         }
212                 }
213                 
214                 #endregion // Properties
215         
216                 #region Methods
217         
218                 public new OleDbTransaction BeginTransaction ()
219                 {
220                         if (State == ConnectionState.Closed)
221                                 throw ExceptionHelper.ConnectionClosed ();
222                         return new OleDbTransaction (this);
223                 }
224
225                 public new OleDbTransaction BeginTransaction (IsolationLevel isolationLevel)
226                 {
227                         if (State == ConnectionState.Closed)
228                                 throw ExceptionHelper.ConnectionClosed ();
229                         return new OleDbTransaction (this, isolationLevel);
230                 }
231
232 #if NET_2_0
233                 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
234                 {
235                         return BeginTransaction (isolationLevel);
236                 }
237
238                 protected override DbCommand CreateDbCommand()
239                 {
240                         return CreateCommand ();
241                 }
242 #else
243                 IDbTransaction IDbConnection.BeginTransaction ()
244                 {
245                         return BeginTransaction ();
246                 }
247
248                 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel isolationLevel)
249                 {
250                         return BeginTransaction (isolationLevel);
251                 }
252
253                 IDbCommand IDbConnection.CreateCommand ()
254                 {
255                         return CreateCommand ();
256                 }
257 #endif
258
259                 public
260 #if NET_2_0
261                 override
262 #endif
263                 void ChangeDatabase (string value)
264                 {
265                         if (State != ConnectionState.Open)
266                                 throw new InvalidOperationException ();
267
268                         if (!libgda.gda_connection_change_database (gdaConnection, value))
269                                 throw new OleDbException (this);
270                 }
271
272                 public
273 #if NET_2_0
274                 override
275 #endif
276                 void Close ()
277                 {
278                         if (State == ConnectionState.Open) {
279                                 libgda.gda_connection_close (gdaConnection);
280                                 gdaConnection = IntPtr.Zero;
281                         }
282                 }
283
284                 public new OleDbCommand CreateCommand ()
285                 {
286                         if (State == ConnectionState.Open)
287                                 return new OleDbCommand (null, this);
288
289                         return null;
290                 }
291
292                 [MonoTODO]
293                 protected override void Dispose (bool disposing)
294                 {
295                         throw new NotImplementedException ();
296                 }
297
298                 [MonoTODO]
299                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
300                 {
301                         throw new NotImplementedException ();
302                 }
303
304                 [MonoTODO]
305                 object ICloneable.Clone ()
306                 {
307                         throw new NotImplementedException();
308                 }
309
310                 public
311 #if NET_2_0
312                 override
313 #endif
314                 void Open ()
315                 {
316 //                      string provider = "Default";
317 //                      string gdaCncStr = string.Empty;
318 //                      string[] args;
319 //                      int len;
320 //                      char [] separator = { ';' };
321                         
322                         if (State == ConnectionState.Open)
323                                 throw new InvalidOperationException ();
324
325                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
326
327                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
328                                 ConnectionString, string.Empty, string.Empty, 0);
329
330                         if (gdaConnection == IntPtr.Zero)
331                                 throw new OleDbException (this);
332                         /* convert the connection string to its GDA equivalent */
333                         //args = connectionString.Split (';');
334                         //len = args.Length;
335                         //for (int i = 0; i < len; i++) {
336                         //      string[] values = args[i].Split (separator, 2);
337                         //      if (values[0] == "Provider") {
338                         //              if (values[1] == "SQLOLEDB")
339                         //                      provider = "FreeTDS";
340                         //              else if (values[1] == "MSDAORA")
341                         //                      provider = "Oracle";
342                         //              else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
343                         //                      provider = "MS Access";
344                         //              else
345                         //                      provider = values[2];
346                         //      }
347                         //      else if (values[0] == "Addr" || values[0] == "Address")
348                         //              gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
349                         //      else if (values[0] == "Database")
350                         //              gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
351                         //      else if (values[0] == "Connection Lifetime")
352                         //              connectionTimeout = System.Convert.ToInt32 (values[1]);
353                         //      else if (values[0] == "File Name")
354                         //              gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
355                         //      else if (values[0] == "Password" || values[0] == "Pwd")
356                         //              gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
357                         //      else if (values[0] == "User ID")
358                         //              gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
359                         //}
360
361                         /* open the connection */
362                         //System.Console.WriteLine ("Opening connection for provider " +
363                         //                provider + " with " + gdaCncStr);
364                         //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
365                         //                                                             provider,
366                         //                                                             gdaCncStr);
367                 }
368
369                 [MonoTODO]
370                 public static void ReleaseObjectPool ()
371                 {
372                         throw new NotImplementedException ();
373                 }
374
375                 [MonoTODO]
376                 public void EnlistDistributedTransaction (ITransaction transaction)
377                 {
378                         throw new NotImplementedException ();
379                 }
380
381 #if NET_2_0
382                 [MonoTODO]
383                 public override void EnlistTransaction (Transaction transaction)
384                 {
385                         throw new NotImplementedException ();
386                 }
387
388                 [MonoTODO]
389                 public override DataTable GetSchema ()
390                 {
391                         if (State == ConnectionState.Closed)
392                                 throw ExceptionHelper.ConnectionClosed ();
393                         throw new NotImplementedException ();
394                 }
395
396                 [MonoTODO]
397                 public override DataTable GetSchema(string collectionName)
398                 {
399                         return GetSchema (collectionName, null);
400                 }
401
402                 [MonoTODO]
403                 public override DataTable GetSchema (String collectionName, string [] restrictionValues)
404                 {
405                         if (State == ConnectionState.Closed)
406                                 throw ExceptionHelper.ConnectionClosed ();
407                         throw new NotImplementedException ();
408                 }
409
410                 [MonoTODO]
411                 [EditorBrowsable (EditorBrowsableState.Advanced)]
412                 public void ResetState ()
413                 {
414                         throw new NotImplementedException ();
415                 }
416 #endif
417
418                 #endregion
419
420                 #region Events and Delegates
421
422 #if !NET_2_0
423                 [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
424 #endif
425                 [DataCategory ("DataCategory_InfoMessage")]
426                 public event OleDbInfoMessageEventHandler InfoMessage;
427
428 #if !NET_2_0
429                 [DataSysDescription ("Event triggered when the connection changes state.")]
430                 [DataCategory ("DataCategory_StateChange")]
431                 public event StateChangeEventHandler StateChange;
432 #endif
433
434                 #endregion
435         }
436 }