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