* OleDbDataAdapter.cs: added stub for missing
[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 using System.ComponentModel;
13 using System.Data;
14 using System.Data.Common;
15 using System.EnterpriseServices;
16
17 namespace System.Data.OleDb
18 {
19         [DefaultEvent ("InfoMessage")]  
20         public sealed class OleDbConnection : Component, ICloneable, IDbConnection
21         {
22                 #region Fields
23
24                 string connectionString;
25                 int connectionTimeout;
26                 IntPtr gdaConnection;
27
28                 #endregion
29
30                 #region Constructors
31                 
32                 public OleDbConnection ()
33                 {
34                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
35                         gdaConnection = IntPtr.Zero;
36                         connectionTimeout = 15;
37                         connectionString = null;
38                 }
39
40                 public OleDbConnection (string connectionString) : this ()
41                 {
42                         this.connectionString = connectionString;
43                 }
44
45                 #endregion // Constructors
46
47                 #region Properties
48                 
49                 [DataCategory ("Data")]
50                 [DefaultValue ("")]
51                 [DataSysDescriptionAttribute ("Information used to connect to a Data Source")]
52                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
53                 [RecommendedAsConfigurableAttribute (true)]
54                 [RefreshPropertiesAttribute (RefreshProperties.All)]
55                 public string ConnectionString {
56                         get {
57                                 return connectionString;
58                         }
59                         set {
60                                 connectionString = value;
61                         }
62                 }
63
64                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
65                 [DataSysDescriptionAttribute ("Current connection timeout value 'Connect TimeOut=X' in the ConnectionString")]
66                 public int ConnectionTimeout {
67                         get {
68                                 return connectionTimeout;
69                         }
70                 }
71
72                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
73                 [DataSysDescriptionAttribute ("Current data source Catlog value, 'Initial Catalog=X' in the ConnectionString")]
74                 public string Database { 
75                         get {
76                                 if (gdaConnection != IntPtr.Zero
77                                     && libgda.gda_connection_is_open (gdaConnection)) {
78                                         return libgda.gda_connection_get_database (gdaConnection);
79                                 }
80
81                                 return null;
82                         }
83                 }
84
85                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
86                 [DataSysDescriptionAttribute ("Current data source, 'Data Source=X' in the ConnectionString")]
87                 public string DataSource {
88                         get {
89                                 if (gdaConnection != IntPtr.Zero
90                                     && libgda.gda_connection_is_open (gdaConnection)) {
91                                         return libgda.gda_connection_get_dsn (gdaConnection);
92                                 }
93
94                                 return null;
95                         }
96                 }
97
98                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
99                 [DataSysDescriptionAttribute ("Current OLE DB provider progid, 'Provider=X' in the ConnectionString")]
100                 public string Provider {
101                         get {
102                                 if (gdaConnection != IntPtr.Zero
103                                     && libgda.gda_connection_is_open (gdaConnection)) {
104                                         return libgda.gda_connection_get_provider (gdaConnection);
105                                 }
106
107                                 return null;
108                         }
109                 }
110
111                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
112                 [DataSysDescriptionAttribute ("Version of the product accessed by the OLE DB Provider")]
113                 [BrowsableAttribute (false)]
114                 public string ServerVersion {
115                         get {
116                                 if (gdaConnection != IntPtr.Zero
117                                     && libgda.gda_connection_is_open (gdaConnection)) {
118                                         return libgda.gda_connection_get_server_version (gdaConnection);
119                                 }
120
121                                 return null;
122                         }
123                 }
124
125                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
126                 [DataSysDescriptionAttribute ("The ConnectionState indicating whether the connection is open or closed")]
127                 [BrowsableAttribute (false)]
128                 public ConnectionState State
129                 {
130                         get {
131                                 if (gdaConnection != IntPtr.Zero) {
132                                         if (libgda.gda_connection_is_open (gdaConnection))
133                                                 return ConnectionState.Open;
134                                 }
135
136                                 return ConnectionState.Closed;
137                         }
138                 }
139
140                 internal IntPtr GdaConnection
141                 {
142                         get {
143                                 return gdaConnection;
144                         }
145                 }
146                 
147                 #endregion // Properties
148         
149                 #region Methods
150         
151                 public OleDbTransaction BeginTransaction ()
152                 {
153                         if (gdaConnection != IntPtr.Zero)
154                                 return new OleDbTransaction (this);
155
156                         return null;
157                 }
158
159                 IDbTransaction IDbConnection.BeginTransaction ()
160                 {
161                         return BeginTransaction ();
162                 }
163                 
164                 public OleDbTransaction BeginTransaction (IsolationLevel level)
165                 {
166                         if (gdaConnection != IntPtr.Zero)
167                                 return new OleDbTransaction (this, level);
168
169                         return null;
170                 }
171
172                 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
173                 {
174                         return BeginTransaction (level);
175                 }
176
177                 public void ChangeDatabase (string name)
178                 {
179                         if (gdaConnection == IntPtr.Zero)
180                                 throw new ArgumentException ();
181                         if (State != ConnectionState.Open)
182                                 throw new InvalidOperationException ();
183
184                         if (!libgda.gda_connection_change_database (gdaConnection, name))
185                                 throw new OleDbException (this);
186                 }
187
188                 public void Close ()
189                 {
190                         if (State == ConnectionState.Open) {
191                                 libgda.gda_connection_close (gdaConnection);
192                                 gdaConnection = IntPtr.Zero;
193                         }
194                 }
195
196                 public OleDbCommand CreateCommand ()
197                 {
198                         if (State == ConnectionState.Open)
199                                 return new OleDbCommand (null, this);
200
201                         return null;
202                 }
203
204                 [MonoTODO]
205                 protected override void Dispose (bool disposing)
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 [MonoTODO]
211                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
212                 {
213                         throw new NotImplementedException ();
214                 }
215
216                 [MonoTODO]
217                 object ICloneable.Clone ()
218                 {
219                         throw new NotImplementedException();
220                 }
221
222                 IDbCommand IDbConnection.CreateCommand ()
223                 {
224                         return CreateCommand ();
225                 }
226
227                 public void Open ()
228                 {
229                         string provider = "Default";
230                         string gdaCncStr = "";
231                         string[] args;
232                         int len;
233                         char [] separator = { ';' };
234                         
235                         if (State == ConnectionState.Open)
236                                 throw new InvalidOperationException ();
237
238                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
239                                                                           connectionString,
240                                                                           "", "", 0);
241                         
242                         /* convert the connection string to its GDA equivalent */
243                         //args = connectionString.Split (';');
244                         //len = args.Length;
245                         //for (int i = 0; i < len; i++) {
246                         //      string[] values = args[i].Split (separator, 2);
247                         //      if (values[0] == "Provider") {
248                         //              if (values[1] == "SQLOLEDB")
249                         //                      provider = "FreeTDS";
250                         //              else if (values[1] == "MSDAORA")
251                         //                      provider = "Oracle";
252                         //              else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
253                         //                      provider = "MS Access";
254                         //              else
255                         //                      provider = values[2];
256                         //      }
257                         //      else if (values[0] == "Addr" || values[0] == "Address")
258                         //              gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
259                         //      else if (values[0] == "Database")
260                         //              gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
261                         //      else if (values[0] == "Connection Lifetime")
262                         //              connectionTimeout = System.Convert.ToInt32 (values[1]);
263                         //      else if (values[0] == "File Name")
264                         //              gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
265                         //      else if (values[0] == "Password" || values[0] == "Pwd")
266                         //              gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
267                         //      else if (values[0] == "User ID")
268                         //              gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
269                         //}
270
271                         /* open the connection */
272                         //System.Console.WriteLine ("Opening connection for provider " +
273                         //                provider + " with " + gdaCncStr);
274                         //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
275                         //                                                             provider,
276                         //                                                             gdaCncStr);
277                 }
278
279                 [MonoTODO]
280                 public static void ReleaseObjectPool ()
281                 {
282                         throw new NotImplementedException ();
283                 }
284
285                 [MonoTODO]
286                 public void EnlistDistributedTransaction (ITransaction transaction)
287                 {
288                         throw new NotImplementedException ();
289                 }
290
291                 #endregion
292
293                 #region Events and Delegates
294
295                 [DataSysDescription ("DbConnection_InfoMessage")]
296                 [DataCategory ("DataCategory_InfoMessage")]
297                 public event OleDbInfoMessageEventHandler InfoMessage;
298
299                 [DataSysDescription ("DbConnection_StateChange")]
300                 [DataCategory ("DataCategory_StateChange")]
301                 public event StateChangeEventHandler StateChange;
302
303                 #endregion
304         }
305 }