2003-02-09 Rodrigo Moya <rodrigo@ximian.com>
[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
16 namespace System.Data.OleDb
17 {
18         public sealed class OleDbConnection : Component, ICloneable, IDbConnection
19         {
20                 #region Fields
21
22                 string connectionString;
23                 int connectionTimeout;
24                 IntPtr gdaConnection;
25
26                 #endregion
27
28                 #region Constructors
29                 
30                 public OleDbConnection ()
31                 {
32                         libgda.gda_init ("System.Data.OleDb", "1.0", 0, new string [0]);
33                         gdaConnection = IntPtr.Zero;
34                         connectionTimeout = 15;
35                         connectionString = null;
36                 }
37
38                 public OleDbConnection (string connectionString) : this ()
39                 {
40                         this.connectionString = connectionString;
41                 }
42
43                 #endregion // Constructors
44
45                 #region Properties
46
47                 public string ConnectionString {
48                         get {
49                                 return connectionString;
50                         }
51                         set {
52                                 connectionString = value;
53                         }
54                 }
55
56                 public int ConnectionTimeout {
57                         get {
58                                 return connectionTimeout;
59                         }
60                 }
61
62                 public string Database { 
63                         get {
64                                 if (gdaConnection != IntPtr.Zero
65                                     && libgda.gda_connection_is_open (gdaConnection)) {
66                                         return libgda.gda_connection_get_database (gdaConnection);
67                                 }
68
69                                 return null;
70                         }
71                 }
72
73                 public string DataSource {
74                         get {
75                                 if (gdaConnection != IntPtr.Zero
76                                     && libgda.gda_connection_is_open (gdaConnection)) {
77                                         return libgda.gda_connection_get_dsn (gdaConnection);
78                                 }
79
80                                 return null;
81                         }
82                 }
83
84                 public string Provider {
85                         get {
86                                 if (gdaConnection != IntPtr.Zero
87                                     && libgda.gda_connection_is_open (gdaConnection)) {
88                                         return libgda.gda_connection_get_provider (gdaConnection);
89                                 }
90
91                                 return null;
92                         }
93                 }
94
95                 public string ServerVersion {
96                         get {
97                                 if (gdaConnection != IntPtr.Zero
98                                     && libgda.gda_connection_is_open (gdaConnection)) {
99                                         return libgda.gda_connection_get_server_version (gdaConnection);
100                                 }
101
102                                 return null;
103                         }
104                 }
105
106                 public ConnectionState State
107                 {
108                         get {
109                                 if (gdaConnection != IntPtr.Zero) {
110                                         if (libgda.gda_connection_is_open (gdaConnection))
111                                                 return ConnectionState.Open;
112                                 }
113
114                                 return ConnectionState.Closed;
115                         }
116                 }
117
118                 internal IntPtr GdaConnection
119                 {
120                         get {
121                                 return gdaConnection;
122                         }
123                 }
124                 
125                 #endregion // Properties
126         
127                 #region Methods
128         
129                 public OleDbTransaction BeginTransaction ()
130                 {
131                         if (gdaConnection != IntPtr.Zero)
132                                 return new OleDbTransaction (this);
133
134                         return null;
135                 }
136
137                 IDbTransaction IDbConnection.BeginTransaction ()
138                 {
139                         return BeginTransaction ();
140                 }
141                 
142                 public OleDbTransaction BeginTransaction (IsolationLevel level)
143                 {
144                         if (gdaConnection != IntPtr.Zero)
145                                 return new OleDbTransaction (this, level);
146
147                         return null;
148                 }
149
150                 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel level)
151                 {
152                         return BeginTransaction (level);
153                 }
154
155                 public void ChangeDatabase (string name)
156                 {
157                         if (gdaConnection == IntPtr.Zero)
158                                 throw new ArgumentException ();
159                         if (State != ConnectionState.Open)
160                                 throw new InvalidOperationException ();
161
162                         if (!libgda.gda_connection_change_database (gdaConnection, name))
163                                 throw new OleDbException (this);
164                 }
165
166                 public void Close ()
167                 {
168                         if (State == ConnectionState.Open) {
169                                 libgda.gda_connection_close (gdaConnection);
170                                 gdaConnection = IntPtr.Zero;
171                         }
172                 }
173
174                 public OleDbCommand CreateCommand ()
175                 {
176                         if (State == ConnectionState.Open)
177                                 return new OleDbCommand (null, this);
178
179                         return null;
180                 }
181
182                 [MonoTODO]
183                 protected override void Dispose (bool disposing)
184                 {
185                         throw new NotImplementedException ();
186                 }
187
188                 [MonoTODO]
189                 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
190                 {
191                         throw new NotImplementedException ();
192                 }
193
194                 [MonoTODO]
195                 object ICloneable.Clone ()
196                 {
197                         throw new NotImplementedException();
198                 }
199
200                 IDbCommand IDbConnection.CreateCommand ()
201                 {
202                         return CreateCommand ();
203                 }
204
205                 public void Open ()
206                 {
207                         string provider = "Default";
208                         string gdaCncStr = "";
209                         string[] args;
210                         int len;
211                         char [] separator = { ';' };
212                         
213                         if (State == ConnectionState.Open)
214                                 throw new InvalidOperationException ();
215
216                         gdaConnection = libgda.gda_client_open_connection (libgda.GdaClient,
217                                                                           connectionString,
218                                                                           "", "", 0);
219                         
220                         /* convert the connection string to its GDA equivalent */
221                         //args = connectionString.Split (';');
222                         //len = args.Length;
223                         //for (int i = 0; i < len; i++) {
224                         //      string[] values = args[i].Split (separator, 2);
225                         //      if (values[0] == "Provider") {
226                         //              if (values[1] == "SQLOLEDB")
227                         //                      provider = "FreeTDS";
228                         //              else if (values[1] == "MSDAORA")
229                         //                      provider = "Oracle";
230                         //              else if (values[2] == "Microsoft.Jet.OLEDB.4.0")
231                         //                      provider = "MS Access";
232                         //              else
233                         //                      provider = values[2];
234                         //      }
235                         //      else if (values[0] == "Addr" || values[0] == "Address")
236                         //              gdaCncStr = String.Concat (gdaCncStr, "HOST=", values[1], ";");
237                         //      else if (values[0] == "Database")
238                         //              gdaCncStr = String.Concat (gdaCncStr, "DATABASE=", values[1], ";");
239                         //      else if (values[0] == "Connection Lifetime")
240                         //              connectionTimeout = System.Convert.ToInt32 (values[1]);
241                         //      else if (values[0] == "File Name")
242                         //              gdaCncStr = String.Concat (gdaCncStr, "FILENAME=", values[1], ";");
243                         //      else if (values[0] == "Password" || values[0] == "Pwd")
244                         //              gdaCncStr = String.Concat (gdaCncStr, "PASSWORD=", values[1], ";");
245                         //      else if (values[0] == "User ID")
246                         //              gdaCncStr = String.Concat (gdaCncStr, "USERNAME=", values[1], ";");
247                         //}
248
249                         /* open the connection */
250                         //System.Console.WriteLine ("Opening connection for provider " +
251                         //                provider + " with " + gdaCncStr);
252                         //gdaConnection = libgda.gda_client_open_connection_from_string (libgda.GdaClient,
253                         //                                                             provider,
254                         //                                                             gdaCncStr);
255                 }
256
257                 [MonoTODO]
258                 public static void ReleaseObjectPool ()
259                 {
260                         throw new NotImplementedException ();
261                 }
262
263                 #endregion
264
265                 #region Events and Delegates
266
267                 public event OleDbInfoMessageEventHandler InfoMessage;
268                 public event StateChangeEventHandler StateChange;
269
270                 #endregion
271         }
272 }